diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-04-09 19:51:35 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 19:51:35 +0300 |
| commit | 233f38915ba0079079233eff55434ef349c05c45 (patch) | |
| tree | 6c5f69017865bcab87355e910c87339453da1406 /src/Web/DevHive.Web/Controllers | |
| parent | f4a70c6430db923af9fa9958a11c2d6612cb52cc (diff) | |
| parent | a992357efcf1bc1ece81b95ecee5e05a0b73bfdc (diff) | |
| download | DevHive-heroku/main.tar DevHive-heroku/main.tar.gz DevHive-heroku/main.zip | |
Merge pull request #28 from Team-Kaleidoscope/devHEADv0.2mainheroku/main
Second stage: Complete
Diffstat (limited to 'src/Web/DevHive.Web/Controllers')
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/CommentController.cs | 116 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/FeedController.cs | 69 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/FriendsController.cs | 54 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/LanguageController.cs | 115 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/PostController.cs | 123 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/ProfilePictureController.cs | 66 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/RatingController.cs | 98 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/RoleController.cs | 101 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/TechnologyController.cs | 115 | ||||
| -rw-r--r-- | src/Web/DevHive.Web/Controllers/UserController.cs | 172 |
10 files changed, 1029 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Controllers/CommentController.cs b/src/Web/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..8fa3577 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,116 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the comments layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class CommentController + { + private readonly ICommentService _commentService; + private readonly IMapper _commentMapper; + private readonly IJwtService _jwtService; + + public CommentController(ICommentService commentService, IMapper commentMapper, IJwtService jwtService) + { + this._commentService = commentService; + this._commentMapper = commentMapper; + this._jwtService = jwtService; + } + + /// <summary> + /// Create a comment and attach it to a post + /// </summary> + /// <param name="userId">The useer's Id</param> + /// <param name="createCommentWebModel">The new comment's parametars</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>The comment's Id</returns> + [HttpPost] + public async Task<IActionResult> AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + if (!await this._commentService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateCommentServiceModel createCommentServiceModel = + this._commentMapper.Map<CreateCommentServiceModel>(createCommentWebModel); + createCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.AddComment(createCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create comment!") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Query comment's data by it's Id + /// </summary> + /// <param name="commentId">The comment's Id</param> + /// <returns>Full data model of the comment</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetCommentById(Guid commentId) + { + ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(commentId); + ReadCommentWebModel readCommentWebModel = this._commentMapper.Map<ReadCommentWebModel>(readCommentServiceModel); + + return new OkObjectResult(readCommentWebModel); + } + + /// <summary> + /// Update comment's parametars. Comment creator only! + /// </summary> + /// <param name="userId">The comment creator's Id</param> + /// <param name="updateCommentWebModel">New comment's parametars</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpPut] + public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + UpdateCommentServiceModel updateCommentServiceModel = + this._commentMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.UpdateComment(updateCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Delete a comment. Comment creator only! + /// </summary> + /// <param name="commentId">Comment's Id</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpDelete] + public async Task<IActionResult> DeleteComment(Guid commentId, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(commentId, authorization)) + return new UnauthorizedResult(); + + return await this._commentService.DeleteComment(commentId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); + } + } +} + diff --git a/src/Web/DevHive.Web/Controllers/FeedController.cs b/src/Web/DevHive.Web/Controllers/FeedController.cs new file mode 100644 index 0000000..37532a9 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/FeedController.cs @@ -0,0 +1,69 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Models.Comment; +using DevHive.Web.Models.Feed; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the feed layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class FeedController + { + private readonly IFeedService _feedService; + private readonly IMapper _mapper; + + public FeedController(IFeedService feedService, IMapper mapper) + { + this._feedService = feedService; + this._mapper = mapper; + } + + /// <summary> + /// Query posts for user's feed + /// </summary> + /// <param name="userId">The user's Id, whose feed is begin queried</param> + /// <param name="getPageWebModel">Page parametars</param> + /// <returns>A page of the feed</returns> + [HttpPost] + [Route("GetPosts")] + public async Task<IActionResult> GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map<GetPageServiceModel>(getPageWebModel); + getPageServiceModel.UserId = userId; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map<ReadPageWebModel>(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + + /// <summary> + /// Query a user profile's posts + /// </summary> + /// <param name="username">The user's username, whose posts are being queried</param> + /// <param name="getPageWebModel">Page parametars</param> + /// <returns>A page of the user's posts</returns> + [HttpPost] + [Route("GetUserPosts")] + [AllowAnonymous] + public async Task<IActionResult> GetUserPosts(string username, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map<GetPageServiceModel>(getPageWebModel); + getPageServiceModel.Username = username; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetUserPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map<ReadPageWebModel>(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/FriendsController.cs b/src/Web/DevHive.Web/Controllers/FriendsController.cs new file mode 100644 index 0000000..318ae64 --- /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, 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(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/LanguageController.cs b/src/Web/DevHive.Web/Controllers/LanguageController.cs new file mode 100644 index 0000000..665fb66 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/LanguageController.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Language; +using DevHive.Web.Models.Language; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the language layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class LanguageController + { + private readonly ILanguageService _languageService; + private readonly IMapper _languageMapper; + + public LanguageController(ILanguageService languageService, IMapper mapper) + { + this._languageService = languageService; + this._languageMapper = mapper; + } + + /// <summary> + /// Create a new language, so users can have a choice. Admin only! + /// </summary> + /// <param name="createLanguageWebModel">The new language's parametars</param> + /// <returns>The new language's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateLanguageWebModel createLanguageWebModel) + { + CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map<CreateLanguageServiceModel>(createLanguageWebModel); + + Guid id = await this._languageService.CreateLanguage(languageServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create language {createLanguageWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Query full language data by Id + /// </summary> + /// <param name="id">The language's Id</param> + /// <returns>Full language data</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadLanguageServiceModel languageServiceModel = await this._languageService.GetLanguageById(id); + ReadLanguageWebModel languageWebModel = this._languageMapper.Map<ReadLanguageWebModel>(languageServiceModel); + + return new OkObjectResult(languageWebModel); + } + + /// <summary> + /// Query all languages in the database + /// </summary> + /// <returns>All languages in the database</returns> + [HttpGet] + [Route("GetLanguages")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingLanguages() + { + HashSet<ReadLanguageServiceModel> languageServiceModels = this._languageService.GetLanguages(); + HashSet<ReadLanguageWebModel> languageWebModels = this._languageMapper.Map<HashSet<ReadLanguageWebModel>>(languageServiceModels); + + return new OkObjectResult(languageWebModels); + } + + /// <summary> + /// Alter language's properties. Admin only! + /// </summary> + /// <param name="id">The language's Id</param> + /// <param name="updateModel">The langauge's new parametars</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) + { + UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map<UpdateLanguageServiceModel>(updateModel); + updatelanguageServiceModel.Id = id; + + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Language"); + + return new OkResult(); + } + + /// <summary> + /// Delete a language. Admin only! + /// </summary> + /// <param name="langaugeId">The language's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid langaugeId) + { + bool result = await this._languageService.DeleteLanguage(langaugeId); + + if (!result) + return new BadRequestObjectResult("Could not delete Language"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/PostController.cs b/src/Web/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..44b291d --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,123 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post; +using DevHive.Services.Models.Post; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the post layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class PostController + { + private readonly IPostService _postService; + private readonly IMapper _postMapper; + private readonly IJwtService _jwtService; + + public PostController(IPostService postService, IMapper postMapper, IJwtService jwtService) + { + this._postService = postService; + this._postMapper = postMapper; + this._jwtService = jwtService; + } + + #region Create + /// <summary> + /// Create a new post + /// </summary> + /// <param name="userId">The user's Id</param> + /// <param name="createPostWebModel">The new post's data</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>New post's Id</returns> + [HttpPost] + public async Task<IActionResult> Create(Guid userId, [FromForm] CreatePostWebModel createPostWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + CreatePostServiceModel createPostServiceModel = + this._postMapper.Map<CreatePostServiceModel>(createPostWebModel); + createPostServiceModel.CreatorId = userId; + + Guid id = await this._postService.CreatePost(createPostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Read + /// <summary> + /// Query full post's data by it's Id + /// </summary> + /// <param name="id">The post's Id</param> + /// <returns>Full data model of the post</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); + ReadPostWebModel postWebModel = this._postMapper.Map<ReadPostWebModel>(postServiceModel); + + return new OkObjectResult(postWebModel); + } + #endregion + + #region Update + /// <summary> + /// Update post's data. Creator only! + /// </summary> + /// <param name="userId">The post creator's Id</param> + /// <param name="updatePostWebModel">The new params of the post</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>The post's Id</returns> + [HttpPut] + public async Task<IActionResult> Update(Guid userId, [FromForm] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) + return new UnauthorizedResult(); + + UpdatePostServiceModel updatePostServiceModel = + this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel); + updatePostServiceModel.CreatorId = userId; + + Guid id = await this._postService.UpdatePost(updatePostServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not update post!") : + new OkObjectResult(new { Id = id }); + } + #endregion + + #region Delete + /// <summary> + /// Delete a post. Creator only! + /// </summary> + /// <param name="postId">Post's Id</param> + /// <param name="authorization">JWT Bearer token</param> + /// <returns>Ok result</returns> + [HttpDelete] + public async Task<IActionResult> Delete(Guid postId, [FromHeader] string authorization) + { + if (!await this._postService.ValidateJwtForPost(postId, authorization)) + return new UnauthorizedResult(); + + return await this._postService.DeletePost(postId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Post"); + } + #endregion + } +} diff --git a/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs new file mode 100644 index 0000000..8474df5 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs @@ -0,0 +1,66 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.ProfilePicture; +using DevHive.Common.Jwt.Interfaces; +using AutoMapper; +using DevHive.Web.Models.ProfilePicture; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the profile picture layer + /// </summary> + [ApiController] + [Route("api/[controller]")] + public class ProfilePictureController + { + private readonly IProfilePictureService _profilePictureService; + private readonly IJwtService _jwtService; + private readonly IMapper _profilePictureMapper; + + public ProfilePictureController(IProfilePictureService profilePictureService, IJwtService jwtService, IMapper profilePictureMapper) + { + this._profilePictureService = profilePictureService; + this._jwtService = jwtService; + this._profilePictureMapper = profilePictureMapper; + } + + /// <summary> + /// Get the URL of user's profile picture + /// </summary> + /// <param name="profilePictureId">The profile picture's Id</param> + /// <param name="authorization">JWT Bearer Token</param> + /// <returns>The URL of the profile picture</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> ReadProfilePicture(Guid profilePictureId) + { + string profilePicURL = await this._profilePictureService.GetProfilePictureById(profilePictureId); + return new OkObjectResult(new { ProfilePictureURL = profilePicURL} ); + } + + /// <summary> + /// Alter the profile picture of a user + /// </summary> + /// <param name="userId">The user's Id</param> + /// <param name="profilePictureWebModel">The new profile picture</param> + /// <param name="authorization">JWT Bearer Token</param> + /// <returns>The URL of the new profile picture</returns> + [HttpPut] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] ProfilePictureWebModel profilePictureWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + ProfilePictureServiceModel profilePictureServiceModel = this._profilePictureMapper.Map<ProfilePictureServiceModel>(profilePictureWebModel); + profilePictureServiceModel.UserId = userId; + + string url = await this._profilePictureService.UpdateProfilePicture(profilePictureServiceModel); + return new OkObjectResult(new { URL = url }); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs new file mode 100644 index 0000000..7d21795 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.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 IMapper _mapper; + private readonly IJwtService _jwtService; + + public RatingController(IRatingService rateService, IMapper mapper, IJwtService jwtService) + { + this._rateService = rateService; + this._mapper = mapper; + this._jwtService = jwtService; + } + + [HttpPost] + public async Task<IActionResult> RatePost(Guid userId, [FromBody] CreateRatingWebModel createRatingWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(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); + } + + [HttpGet] + [Route("GetByUserAndPost")] + public async Task<IActionResult> GetRatingByUserAndPost(Guid userId, Guid postId) + { + ReadRatingServiceModel readRatingServiceModel = await this._rateService.GetRatingByPostAndUser(userId, postId); + ReadRatingWebModel readPostRatingWebModel = this._mapper.Map<ReadRatingWebModel>(readRatingServiceModel); + + return new OkObjectResult(readPostRatingWebModel); + } + + [HttpPut] + public async Task<IActionResult> UpdateRating(Guid userId, Guid postId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + UpdateRatingServiceModel updateRatingServiceModel = + this._mapper.Map<UpdateRatingServiceModel>(updateRatingWebModel); + updateRatingServiceModel.UserId = userId; + updateRatingServiceModel.PostId = postId; + + 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> DeleteRating(Guid userId, Guid ratingId, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(userId, authorization)) + return new UnauthorizedResult(); + + return await this._rateService.DeleteRating(ratingId) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Rating"); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/RoleController.cs b/src/Web/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..ebb305e --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/RoleController.cs @@ -0,0 +1,101 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using DevHive.Web.Models.Role; +using AutoMapper; +using System; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Role; +using Microsoft.AspNetCore.Authorization; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the roles layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class RoleController + { + private readonly IRoleService _roleService; + private readonly IMapper _roleMapper; + + public RoleController(IRoleService roleService, IMapper mapper) + { + this._roleService = roleService; + this._roleMapper = mapper; + } + + /// <summary> + /// Create a new role for the roles hierarchy. Admin only! + /// </summary> + /// <param name="createRoleWebModel">The new role's parametars</param> + /// <returns>The new role's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel) + { + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map<CreateRoleServiceModel>(createRoleWebModel); + + Guid id = await this._roleService.CreateRole(roleServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Get a role's full data, querying it by it's Id + /// </summary> + /// <param name="id">The role's Id</param> + /// <returns>Full info of the role</returns> + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> GetById(Guid id) + { + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map<RoleWebModel>(roleServiceModel); + + return new OkObjectResult(roleWebModel); + } + + /// <summary> + /// Update a role's parametars. Admin only! + /// </summary> + /// <param name="id">The role's Id</param> + /// <param name="updateRoleWebModel">The new parametrats for that role</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + { + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map<UpdateRoleServiceModel>(updateRoleWebModel); + updateRoleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); + } + + /// <summary> + /// Delete a role. Admin only! + /// </summary> + /// <param name="id">The role's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could not delete role!"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/TechnologyController.cs b/src/Web/DevHive.Web/Controllers/TechnologyController.cs new file mode 100644 index 0000000..ecf2bd7 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/TechnologyController.cs @@ -0,0 +1,115 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Technology; +using DevHive.Web.Models.Technology; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for interacting with the technology layer + /// </summary> + [ApiController] + [Route("/api/[controller]")] + public class TechnologyController + { + private readonly ITechnologyService _technologyService; + private readonly IMapper _technologyMapper; + + public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper) + { + this._technologyService = technologyService; + this._technologyMapper = technologyMapper; + } + + /// <summary> + /// Create a new technology, so users can have a choice. Admin only! + /// </summary> + /// <param name="createTechnologyWebModel">Data for the new technology</param> + /// <returns>The new technology's Id</returns> + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel) + { + CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map<CreateTechnologyServiceModel>(createTechnologyWebModel); + + Guid id = await this._technologyService.CreateTechnology(technologyServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + /// <summary> + /// Get technology's data by it's Id + /// </summary> + /// <param name="id">The technology's Id</param> + /// <returns>The technology's full data</returns> + [HttpGet] + [AllowAnonymous] + public async Task<IActionResult> GetById(Guid id) + { + ReadTechnologyServiceModel readTechnologyServiceModel = await this._technologyService.GetTechnologyById(id); + ReadTechnologyWebModel readTechnologyWebModel = this._technologyMapper.Map<ReadTechnologyWebModel>(readTechnologyServiceModel); + + return new OkObjectResult(readTechnologyWebModel); + } + + /// <summary> + /// Get all technologies from our database + /// </summary> + /// <returns>All technologies</returns> + [HttpGet] + [Route("GetTechnologies")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingTechnologies() + { + HashSet<ReadTechnologyServiceModel> technologyServiceModels = this._technologyService.GetTechnologies(); + HashSet<ReadTechnologyWebModel> languageWebModels = this._technologyMapper.Map<HashSet<ReadTechnologyWebModel>>(technologyServiceModels); + + return new OkObjectResult(languageWebModels); + } + + /// <summary> + /// Alter a technology's parameters. Admin only! + /// </summary> + /// <param name="id">Technology's Id</param> + /// <param name="updateModel">The new parametars</param> + /// <returns>Ok result</returns> + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) + { + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map<UpdateTechnologyServiceModel>(updateModel); + updateTechnologyServiceModel.Id = id; + + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Technology"); + + return new OkResult(); + } + + /// <summary> + /// Delete a etchnology from the database. Admin only! + /// </summary> + /// <param name="id">The technology's Id</param> + /// <returns>Ok result</returns> + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._technologyService.DeleteTechnology(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Technology"); + + return new OkResult(); + } + } +} diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs new file mode 100644 index 0000000..4d01447 --- /dev/null +++ b/src/Web/DevHive.Web/Controllers/UserController.cs @@ -0,0 +1,172 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Models.User; +using DevHive.Web.Models.User; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using DevHive.Common.Models.Identity; +using DevHive.Services.Interfaces; +using DevHive.Common.Jwt.Interfaces; +using NSwag.Annotations; + +namespace DevHive.Web.Controllers +{ + /// <summary> + /// All endpoints for integration with the User + /// </summary> + [ApiController] + [Route("/api/[controller]")] + [OpenApiController("User Controller")] + public class UserController : ControllerBase + { + private readonly IUserService _userService; + private readonly IMapper _userMapper; + private readonly IJwtService _jwtService; + + public UserController(IUserService userService, IMapper mapper, IJwtService jwtService) + { + this._userService = userService; + this._userMapper = mapper; + this._jwtService = jwtService; + } + + #region Authentication + /// <summary> + /// Login endpoint for the DevHive Social Platform + /// </summary> + /// <param name="loginModel">Login model with username and password</param> + /// <returns>A JWT Token for further validation</returns> + [HttpPost] + [AllowAnonymous] + [Route("Login")] + [OpenApiTags("Authorization")] + public async Task<IActionResult> Login([FromBody] LoginWebModel loginModel) + { + LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel); + + TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel); + + return new OkObjectResult(tokenWebModel); + } + + /// <summary> + /// Register a new User in the DevHive Social Platform + /// </summary> + /// <param name="registerModel">Register model with the new data to provide</param> + /// <returns>A JWT Token for further validation</returns> + [HttpPost] + [AllowAnonymous] + [Route("Register")] + [OpenApiTag("Authorization")] + public async Task<IActionResult> Register([FromBody] RegisterWebModel registerModel) + { + RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel); + + TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel); + + return new CreatedResult("Register", tokenWebModel); + } + #endregion + + #region Read + /// <summary> + /// Get a User's information using the Guid + /// </summary> + /// <param name="id">User's Id</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>A full User's read model</returns> + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> GetById(Guid id, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + UserServiceModel userServiceModel = await this._userService.GetUserById(id); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new OkObjectResult(userWebModel); + } + + /// <summary> + /// Get a User's profile using his username. Does NOT require authorization + /// </summary> + /// <param name="username">User's username</param> + /// <returns>A trimmed version of the full User's read model</returns> + [HttpGet] + [Route("GetUser")] + [AllowAnonymous] + public async Task<IActionResult> GetUser(string username) + { + UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username); + UserWebModel friend = this._userMapper.Map<UserWebModel>(friendServiceModel); + + return new OkObjectResult(friend); + } + #endregion + + #region Update + /// <summary> + /// Full update on User's data. A PUSTINQK can only edit his account + /// </summary> + /// <param name="id">The User's Id</param> + /// <param name="updateUserWebModel">A full User update model</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>A full User's read model</returns> + [HttpPut] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateUserWebModel); + updateUserServiceModel.Id = id; + + UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); + UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel); + + return new AcceptedResult("UpdateUser", userWebModel); + } + #endregion + + #region Delete + /// <summary> + /// Delete a User with his Id. A PUSTINQK can only delete his account. An Admin can delete all accounts + /// </summary> + /// <param name="id">The User's Id</param> + /// <param name="authorization">The JWT Token, contained in the header and used for validation</param> + /// <returns>Ok, BadRequest or Unauthorized</returns> + [HttpDelete] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization) + { + if (!this._jwtService.ValidateToken(id, authorization)) + return new UnauthorizedResult(); + + bool result = await this._userService.DeleteUser(id); + if (!result) + return new BadRequestObjectResult("Could not delete User"); + + return new OkResult(); + } + #endregion + + /// <summary> + /// We don't talk about that, NIGGA! + /// </summary> + /// <param name="userId"></param> + /// <returns></returns> + [HttpPost] + [OpenApiIgnore] + [Authorize(Roles = "User,Admin")] + [Route("SuperSecretPromotionToAdmin")] + public async Task<IActionResult> SuperSecretPromotionToAdmin(Guid userId) + { + return new OkObjectResult(await this._userService.SuperSecretPromotionToAdmin(userId)); + } + } +} |
