diff --git a/src/server/host/Endpoints/Administration/Users/CreateUserEndpoint.cs b/src/server/host/Endpoints/Administration/Users/CreateUserEndpoint.cs index 9f84f7b..11a7ed4 100644 --- a/src/server/host/Endpoints/Administration/Users/CreateUserEndpoint.cs +++ b/src/server/host/Endpoints/Administration/Users/CreateUserEndpoint.cs @@ -1,10 +1,14 @@ using System.ComponentModel.DataAnnotations; using FastEndpoints; using FluentValidation; +using Microsoft.EntityFrameworkCore; using MinigolfFriday.Data; using MinigolfFriday.Data.Entities; using MinigolfFriday.Domain.Models; using MinigolfFriday.Domain.Models.RealtimeEvents; +using MinigolfFriday.Host.Common; +using MinigolfFriday.Host.Mappers; +using MinigolfFriday.Host.Options; using MinigolfFriday.Host.Services; using MinigolfFriday.Host.Utilities; @@ -51,7 +55,8 @@ public CreateUserRequestValidator(IIdService idService) public class CreateUserEndpoint( DatabaseContext databaseContext, IRealtimeEventsService realtimeEventsService, - IIdService idService + IIdService idService, + IUserMapper userMapper ) : Endpoint { public override void Configure() @@ -59,10 +64,21 @@ public override void Configure() Post(""); Group(); Description(x => x.ClearDefaultProduces(200).Produces(201)); + this.ProducesError(EndpointErrors.UserExists); } public override async Task HandleAsync(CreateUserRequest req, CancellationToken ct) { + var existentUser = await databaseContext + .Users.Where(x => x.Alias == req.Alias) + .Select(userMapper.MapUserExpression) + .FirstOrDefaultAsync(ct); + if (existentUser != null) + { + await this.SendErrorAsync(EndpointErrors.UserExists, existentUser.Alias, ct); + return; + } + var user = new UserEntity() { Alias = req.Alias, diff --git a/src/server/host/Endpoints/EndpointErrors.cs b/src/server/host/Endpoints/EndpointErrors.cs index 02de19a..fb226ed 100644 --- a/src/server/host/Endpoints/EndpointErrors.cs +++ b/src/server/host/Endpoints/EndpointErrors.cs @@ -6,6 +6,8 @@ public class EndpointErrors { public static readonly EndpointError.Params1 UserNotFound = new(404, "A user with the id {0} does not exist.", "UserId"); + public static readonly EndpointError.Params1 UserExists = + new(404, "A user with the alias {0} does already exist.", "Alias"); public static readonly EndpointError UserIdNotInClaims = new(403, "Could not extract user id from claims."); public static readonly EndpointError CannotDeleteSelf = new(409, "You cannot delete yourself.");