aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'src/Web/DevHive.Web/Controllers')
-rw-r--r--src/Web/DevHive.Web/Controllers/CommentController.cs1
-rw-r--r--src/Web/DevHive.Web/Controllers/RateController.cs40
-rw-r--r--src/Web/DevHive.Web/Controllers/RatingController.cs86
3 files changed, 86 insertions, 41 deletions
diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs
index b4fae5c..1722801 100644
--- a/src/Web/DevHive.Web/Controllers/CommentController.cs
+++ b/src/Web/DevHive.Web/Controllers/CommentController.cs
@@ -83,7 +83,6 @@ namespace DevHive.Web.Controllers
new OkResult() :
new BadRequestObjectResult("Could not delete Comment");
}
-
}
}
diff --git a/src/Web/DevHive.Web/Controllers/RateController.cs b/src/Web/DevHive.Web/Controllers/RateController.cs
deleted file mode 100644
index 72eb932..0000000
--- a/src/Web/DevHive.Web/Controllers/RateController.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using AutoMapper;
-using DevHive.Services.Interfaces;
-using DevHive.Services.Models.Post.Rating;
-using DevHive.Web.Models.Rating;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-
-namespace DevHive.Web.Controllers
-{
- [ApiController]
- [Route("api/[controller]")]
- public class RateController
- {
- private readonly IRateService _rateService;
- private readonly IUserService _userService;
- private readonly IMapper _mapper;
-
- public RateController(IRateService rateService, IUserService userService, IMapper mapper)
- {
- this._rateService = rateService;
- this._userService = userService;
- this._mapper = mapper;
- }
-
- [HttpPost]
- [Authorize(Roles = "Admin,User")]
- public async Task<IActionResult> RatePost(Guid userId, [FromBody] RatePostWebModel ratePostWebModel, [FromHeader] string authorization)
- {
- RatePostServiceModel ratePostServiceModel = this._mapper.Map<RatePostServiceModel>(ratePostWebModel);
- ratePostServiceModel.UserId = userId;
-
- ReadPostRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel);
- ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadPostRatingWebModel>(readPostRatingServiceModel);
-
- return new OkObjectResult(readPostRatingWebModel);
- }
- }
-}
diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs
new file mode 100644
index 0000000..344acb2
--- /dev/null
+++ b/src/Web/DevHive.Web/Controllers/RatingController.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post.Rating;
+using DevHive.Web.Models.Rating;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+
+namespace DevHive.Web.Controllers
+{
+ [ApiController]
+ //[Authorize(Roles = "Admin,User")]
+ [Route("api/[controller]")]
+ public class RatingController
+ {
+ private readonly IRatingService _rateService;
+ private readonly IUserService _userService;
+ private readonly IMapper _mapper;
+
+ public RatingController(IRatingService rateService, IUserService userService, IMapper mapper)
+ {
+ this._rateService = rateService;
+ this._userService = userService;
+ this._mapper = mapper;
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._rateService.ValidateJwtForCreating(userId, authorization))
+ return new UnauthorizedResult();
+
+ CreateRatingServiceModel ratePostServiceModel = this._mapper.Map<CreateRatingServiceModel>(createRatingWebModel);
+ ratePostServiceModel.UserId = userId;
+
+ Guid id = await this._rateService.RatePost(ratePostServiceModel);
+
+ if (Guid.Empty == id)
+ return new BadRequestResult();
+
+ return new OkObjectResult(new { Id = id });
+ }
+
+ [HttpGet]
+ public async Task<IActionResult> GetRatingById(Guid id)
+ {
+ ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingById(id);
+ ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel);
+
+ return new OkObjectResult(readPostRatingWebModel);
+ }
+
+ [HttpPut]
+ public async Task<IActionResult> UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._rateService.ValidateJwtForRating(updateRatingWebModel.Id, authorization))
+ return new UnauthorizedResult();
+
+ UpdateRatingServiceModel updateRatingServiceModel =
+ this._mapper.Map<UpdateRatingServiceModel>(updateRatingWebModel);
+ updateRatingServiceModel.UserId = userId;
+
+ ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel);
+
+ if (readRatingServiceModel == null)
+ return new BadRequestResult();
+ else
+ {
+ ReadRatingWebModel readRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel);
+ return new OkObjectResult(readRatingWebModel);
+ }
+ }
+
+ [HttpDelete]
+ public async Task<IActionResult> DeleteTating(Guid id, [FromHeader] string authorization)
+ {
+ if (!await this._rateService.ValidateJwtForRating(id, authorization))
+ return new UnauthorizedResult();
+
+ return await this._rateService.DeleteRating(id) ?
+ new OkResult() :
+ new BadRequestObjectResult("Could not delete Rating");
+ }
+ }
+}