Skip to content

Commit

Permalink
Merge pull request #222 from AnalogIO/217-new-admin-endpoints
Browse files Browse the repository at this point in the history
add a patch endpoint to update a user group of an account
  • Loading branch information
HubertWojcik10 authored Dec 7, 2023
2 parents 0789c28 + 613b776 commit 7b48f72
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
26 changes: 26 additions & 0 deletions coffeecard/CoffeeCard.Library/Services/v2/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -200,6 +201,31 @@ private async Task<User> 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<User> 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('<', '>', '{', '}');
Expand Down
9 changes: 8 additions & 1 deletion coffeecard/CoffeeCard.Library/Services/v2/IAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -53,8 +54,14 @@ public interface IAccountService
/// Resend invite e-mail if user account is not already verified
/// </summary>
/// <param name="request">Email request</param>
/// <exception cref="ConflictException">User account is already verified</exception>
/// <exception cref="EntityNotFoundException">Email account not found</exception>
Task ResendAccountVerificationEmail(ResendAccountVerificationEmailRequest request);

/// <summary>
/// Update a userGroup of a user with a provided id
/// </summary>
/// <param name="userGroup"> The user group that will be updated </param>
/// <param name="id"> id of the user </param>
Task UpdateUserGroup(UserGroup userGroup, int id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
using CoffeeCard.Models.Entities;

namespace CoffeeCard.Models.DataTransferObjects.v2.User
{
/// <summary>
/// Update the UserGroup property of a user
/// </summary>
/// <example>
/// {
/// "UserGroup": "Barista"
/// }
/// </example>
public class UpdateUserGroupRequest
{
/// <summary>
/// The UserGroup of a user
/// </summary>
/// <value> UserGroup object </value>
/// <example> UserGroup.Barista </example>
[Required]
public UserGroup UserGroup { get; set; }
}
}
24 changes: 23 additions & 1 deletion coffeecard/CoffeeCard.WebApi/Controllers/v2/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -136,6 +136,28 @@ public async Task<ActionResult<EmailExistsResponse>> EmailExists([FromBody] Emai
});
}

/// <summary>
/// Updates the user group of a user
/// </summary>
/// <param name="id"> id of the user whose userGroup will be updated </param>
/// <param name="updateUserGroupRequest"> Update User Group information request </param>
/// <returns> no content result </returns>
/// <response code="204"> The update was processed </response>
/// <response code="401"> Invalid credentials </response>
/// <response code="404"> User not found </response>
[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<ActionResult> UpdateAccountUserGroup([FromRoute] int id, [FromBody] UpdateUserGroupRequest updateUserGroupRequest)
{
await _accountService.UpdateUserGroup(updateUserGroupRequest.UserGroup, id);

return new NoContentResult();
}

/// <summary>
/// Resend account verification email if account is not already verified
/// </summary>
Expand Down

0 comments on commit 7b48f72

Please sign in to comment.