aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/RateController.cs
blob: 7f8a95f5aba718a7b623cec75b31336951c3ca5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 IRatingService _rateService;
		private readonly IUserService _userService;
		private readonly IMapper _mapper;

		public RateController(IRatingService 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)
		{
			CreateRatingServiceModel ratePostServiceModel = this._mapper.Map<CreateRatingServiceModel>(ratePostWebModel);
			ratePostServiceModel.UserId = userId;

			ReadRatingServiceModel readPostRatingServiceModel = await this._rateService.RatePost(ratePostServiceModel);
			ReadPostRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadPostRatingWebModel>(readPostRatingServiceModel);

			return new OkObjectResult(readPostRatingWebModel);
		}
	}
}