diff --git a/Knots/Endpoints/User/CreateUserEndpoint.cs b/Knots/Endpoints/User/CreateUserEndpoint.cs index 6176133..54e562a 100644 --- a/Knots/Endpoints/User/CreateUserEndpoint.cs +++ b/Knots/Endpoints/User/CreateUserEndpoint.cs @@ -1,6 +1,36 @@ +using FastEndpoints; +using Knots.DTO.User; + namespace Knots.Endpoints.User; -public class CreateUserEndpoint +public class CreateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint { + public override void Configure() + { + Post("/users"); + AllowAnonymous(); + } + public override async Task HandleAsync(CreateUserDto req, CancellationToken ct) + { + Models.User user = new() + { + Username = req.Username, + Description = req.Description, + Password = req.Password, + Email = req.Email, + Tel = req.Tel, + ProfilePicture = req.ProfilePicture + }; + knotsDbContext.Add(user); + await knotsDbContext.SaveChangesAsync(ct); + + GetUserDto response = new() + { + Username = user.Username + }; + + await Send.OkAsync(response, ct); + + } } \ No newline at end of file diff --git a/Knots/Endpoints/User/DeleteUserEndpoint.cs b/Knots/Endpoints/User/DeleteUserEndpoint.cs index 7f76d8b..ceb6b70 100644 --- a/Knots/Endpoints/User/DeleteUserEndpoint.cs +++ b/Knots/Endpoints/User/DeleteUserEndpoint.cs @@ -1,6 +1,28 @@ +using FastEndpoints; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.User; -public class DeleteUserEndpoint +public class DeleteUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint { - + public override void Configure() + { + Delete ("/users/{@Id}", x => new { x.Username }); + } + + public override async Task HandleAsync(GetUserDto req, CancellationToken ct) + { + Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct); + + if (databaseUser == null) + { + await Send.NotFoundAsync(ct); + return; + } + knotsDbContext.Users.Remove(databaseUser); + await knotsDbContext.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/User/GetAllUsersEndpoint.cs b/Knots/Endpoints/User/GetAllUsersEndpoint.cs index 0ca85fd..20dd935 100644 --- a/Knots/Endpoints/User/GetAllUsersEndpoint.cs +++ b/Knots/Endpoints/User/GetAllUsersEndpoint.cs @@ -1,6 +1,24 @@ +using FastEndpoints; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.User; -public class GetAllUsersEndpoint +public class GetAllUsersEndpoint(KnotsDbContext knotsDbContext) : EndpointWithoutRequest> { - + public override void Configure() + { + Get ("/users"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CancellationToken ct) + { + List users= await knotsDbContext.Users.Select(x => new GetUserDto() + { + Username = x.Username, + }).ToListAsync(ct); + + await Send.OkAsync(users, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/User/GetUserEndpoint.cs b/Knots/Endpoints/User/GetUserEndpoint.cs index d6a6916..619a803 100644 --- a/Knots/Endpoints/User/GetUserEndpoint.cs +++ b/Knots/Endpoints/User/GetUserEndpoint.cs @@ -1,6 +1,37 @@ +using FastEndpoints; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.User; -public class GetUserEndpoint +public class GetUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint { - + public override void Configure() + { + Get ("/users/{@Id}", x => new { x.Username }); + AllowAnonymous(); + } + + public override async Task HandleAsync(GetUserDto req, CancellationToken ct) + { + Models.User? databaseLogin = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct); + + if (databaseLogin == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetUserDetailsDto dto = new() + { + Username = databaseLogin.Username, + Description = databaseLogin.Description, + Password = databaseLogin.Password, + Email = databaseLogin.Email, + Tel = databaseLogin.Tel, + ProfilePicture = databaseLogin.ProfilePicture + }; + + await Send.OkAsync(dto, ct); + } } \ No newline at end of file diff --git a/Knots/Endpoints/User/UpdateUserEndpoint.cs b/Knots/Endpoints/User/UpdateUserEndpoint.cs index c58ec6c..22c61b7 100644 --- a/Knots/Endpoints/User/UpdateUserEndpoint.cs +++ b/Knots/Endpoints/User/UpdateUserEndpoint.cs @@ -1,6 +1,46 @@ +using FastEndpoints; +using Knots.DTO.User; +using Microsoft.EntityFrameworkCore; + namespace Knots.Endpoints.User; -public class UpdateUserEndpoint +public class UpdateUserEndpoint(KnotsDbContext knotsDbContext) : Endpoint { - + public override void Configure() + { + Put ("/users/{@Id}", x => new { x.Username }); + } + + public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct) + { + Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Username == req.Username, cancellationToken: ct); + + if (databaseUser == null) + { + await Send.NotFoundAsync(ct); + return; + } + else + { + databaseUser.Username = req.Username; + databaseUser.Password = req.Password; + databaseUser.Description = req.Description; + databaseUser.Tel = req.Tel; + databaseUser.Email = req.Email; + databaseUser.ProfilePicture = req.ProfilePicture; + } + await knotsDbContext.SaveChangesAsync(ct); + + GetUserDetailsDto dto = new() + { + Username = databaseUser.Username, + Password = databaseUser.Password, + Description = databaseUser.Description, + Tel = databaseUser.Tel, + Email = databaseUser.Email, + ProfilePicture = databaseUser.ProfilePicture + }; + + await Send.OkAsync(dto, ct); + } } \ No newline at end of file