diff --git a/coffeecard/CoffeeCard.Library/Services/v2/AccountService.cs b/coffeecard/CoffeeCard.Library/Services/v2/AccountService.cs index 7bfdbd41..0b72d071 100644 --- a/coffeecard/CoffeeCard.Library/Services/v2/AccountService.cs +++ b/coffeecard/CoffeeCard.Library/Services/v2/AccountService.cs @@ -8,6 +8,7 @@ using CoffeeCard.Models.DataTransferObjects.v2.User; using CoffeeCard.Models.Entities; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Serilog; @@ -200,6 +201,31 @@ private async Task GetAccountByEmailAsync(string email) return user; } + public async Task UpdateUserGroup(UserGroup userGroup, int userId) + { + User user = await GetUserByIdAsync(userId); + + user.UserGroup = userGroup; + + await _context.SaveChangesAsync(); + } + + private async Task GetUserByIdAsync(int id) + { + var user = await _context.Users + .Where(u => u.Id == id) + .FirstOrDefaultAsync(); + + if (user == null) + { + Log.Error("No user was found by user id: {id}", id); + throw new EntityNotFoundException($"No user was found by user id: {id}"); + } + + return user; + } + + private static string EscapeName(string name) { return name.Trim('<', '>', '{', '}'); diff --git a/coffeecard/CoffeeCard.Library/Services/v2/IAccountService.cs b/coffeecard/CoffeeCard.Library/Services/v2/IAccountService.cs index 0c38a49a..9a35b4f2 100644 --- a/coffeecard/CoffeeCard.Library/Services/v2/IAccountService.cs +++ b/coffeecard/CoffeeCard.Library/Services/v2/IAccountService.cs @@ -4,6 +4,7 @@ using CoffeeCard.Common.Errors; using CoffeeCard.Models.DataTransferObjects.v2.User; using CoffeeCard.Models.Entities; +using Microsoft.AspNetCore.Mvc; namespace CoffeeCard.Library.Services.v2 { @@ -53,8 +54,14 @@ public interface IAccountService /// Resend invite e-mail if user account is not already verified /// /// Email request - /// User account is already verified /// Email account not found Task ResendAccountVerificationEmail(ResendAccountVerificationEmailRequest request); + + /// + /// Update a userGroup of a user with a provided id + /// + /// The user group that will be updated + /// id of the user + Task UpdateUserGroup(UserGroup userGroup, int id); } } \ No newline at end of file diff --git a/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/User/UpdateUserGroupRequest.cs b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/User/UpdateUserGroupRequest.cs new file mode 100644 index 00000000..49fb5c48 --- /dev/null +++ b/coffeecard/CoffeeCard.Models/DataTransferObjects/v2/User/UpdateUserGroupRequest.cs @@ -0,0 +1,24 @@ +using System.ComponentModel.DataAnnotations; +using CoffeeCard.Models.Entities; + +namespace CoffeeCard.Models.DataTransferObjects.v2.User +{ + /// + /// Update the UserGroup property of a user + /// + /// + /// { + /// "UserGroup": "Barista" + /// } + /// + public class UpdateUserGroupRequest + { + /// + /// The UserGroup of a user + /// + /// UserGroup object + /// UserGroup.Barista + [Required] + public UserGroup UserGroup { get; set; } + } +} \ No newline at end of file diff --git a/coffeecard/CoffeeCard.WebApi/Controllers/v2/AccountController.cs b/coffeecard/CoffeeCard.WebApi/Controllers/v2/AccountController.cs index 5713ebc4..3751d641 100644 --- a/coffeecard/CoffeeCard.WebApi/Controllers/v2/AccountController.cs +++ b/coffeecard/CoffeeCard.WebApi/Controllers/v2/AccountController.cs @@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Mvc; using CoffeeCard.Library.Services.v2; using CoffeeCard.Models.Entities; -using Serilog; +using CoffeeCard.WebApi.Helpers; namespace CoffeeCard.WebApi.Controllers.v2 { @@ -136,6 +136,28 @@ public async Task> EmailExists([FromBody] Emai }); } + /// + /// Updates the user group of a user + /// + /// id of the user whose userGroup will be updated + /// Update User Group information request + /// no content result + /// The update was processed + /// Invalid credentials + /// User not found + [HttpPatch] + [AuthorizeRoles(UserGroup.Board)] + [ProducesResponseType(typeof(void), StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(ApiError), StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ApiError), StatusCodes.Status404NotFound)] + [Route("{id:int}/user-group")] + public async Task UpdateAccountUserGroup([FromRoute] int id, [FromBody] UpdateUserGroupRequest updateUserGroupRequest) + { + await _accountService.UpdateUserGroup(updateUserGroupRequest.UserGroup, id); + + return new NoContentResult(); + } + /// /// Resend account verification email if account is not already verified ///