aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/FriendsController.cs
blob: 318ae646377dd8f8cd1f0a8c6e0b4ee9c5ac73f3 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Common.Jwt.Interfaces;
using DevHive.Common.Models.Identity;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.User;
using DevHive.Web.Models.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;

namespace DevHive.Web.Controllers
{
	[ApiController]
	[Route("api/[controller]")]
	public class FriendsController
	{
		private readonly IFriendsService _friendsService;
		private readonly IMapper _mapper;
		private readonly IJwtService _jwtService;

		public FriendsController(IFriendsService friendsService, IMapper mapper, IJwtService jwtService)
		{
			this._friendsService = friendsService;
			this._mapper = mapper;
			this._jwtService = jwtService;
		}

		[HttpPost]
		[Authorize(Roles = "User,Admin")]
		public async Task<IActionResult> AddFriend(Guid userId, string friendUsername, [FromHeader] string authorization)
		{
			if (!this._jwtService.ValidateToken(userId, authorization))
				return new UnauthorizedResult();

			return (await this._friendsService.AddFriend(userId, friendUsername)) ?
				new OkResult() :
				new BadRequestResult();
		}

		[HttpDelete]
		[Authorize(Roles = "User,Admin")]
		public async Task<IActionResult> RemoveFriend(Guid userId, string friendUsername, [FromHeader] string authorization)
		{
			if (!this._jwtService.ValidateToken(userId, authorization))
				return new UnauthorizedResult();

			return (await this._friendsService.RemoveFriend(userId, friendUsername)) ?
				new OkResult() :
				new BadRequestResult();
		}
	}
}