aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/FriendsController.cs
diff options
context:
space:
mode:
authorDanail Dimitrov <danaildimitrov321@gmail.com>2021-04-07 20:02:36 +0300
committerGitHub <noreply@github.com>2021-04-07 20:02:36 +0300
commitbc3d341d0390a7367a9f7b901dcc3cb93ac1247c (patch)
treee0663d2ece9f8124ff58de7a565944092f67003a /src/Web/DevHive.Web/Controllers/FriendsController.cs
parentda7d6223c261aac8e8f18458c11fb48cf9ca4cfe (diff)
parent5595bf9abe7b3d495f36ef39cfa5236dbde86039 (diff)
downloadDevHive-bc3d341d0390a7367a9f7b901dcc3cb93ac1247c.tar
DevHive-bc3d341d0390a7367a9f7b901dcc3cb93ac1247c.tar.gz
DevHive-bc3d341d0390a7367a9f7b901dcc3cb93ac1247c.zip
Merge pull request #26 from Team-Kaleidoscope/friends_functionality
Added "Add Friend" & "Remove Friend" functionality and endpoints
Diffstat (limited to 'src/Web/DevHive.Web/Controllers/FriendsController.cs')
-rw-r--r--src/Web/DevHive.Web/Controllers/FriendsController.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Controllers/FriendsController.cs b/src/Web/DevHive.Web/Controllers/FriendsController.cs
new file mode 100644
index 0000000..9f3ec13
--- /dev/null
+++ b/src/Web/DevHive.Web/Controllers/FriendsController.cs
@@ -0,0 +1,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, Guid friendId, [FromHeader] string authorization)
+ {
+ if (!this._jwtService.ValidateToken(userId, authorization))
+ return new UnauthorizedResult();
+
+ return (await this._friendsService.AddFriend(userId, friendId)) ?
+ new OkResult() :
+ new BadRequestResult();
+ }
+
+ [HttpDelete]
+ [Authorize(Roles = "User,Admin")]
+ public async Task<IActionResult> RemoveFriend(Guid userId, Guid friendId, [FromHeader] string authorization)
+ {
+ if (!this._jwtService.ValidateToken(userId, authorization))
+ return new UnauthorizedResult();
+
+ return (await this._friendsService.RemoveFriend(userId, friendId)) ?
+ new OkResult() :
+ new BadRequestResult();
+ }
+ }
+}