From d31dffb67eba973548a47e24dcbc9b2954bb85de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20D=C3=A1quina?= Date: Fri, 26 Jul 2024 18:14:59 +0200 Subject: [PATCH] Adding delete medication --- .../EndPoints/MedicationEndPoints.cs | 58 ++++++++++++++++--- .../Commands/DeleteMedicationCommand.cs | 6 ++ .../Handlers/MedicationHandler.cs | 9 ++- .../Interfaces/IMedicationHandler.cs | 2 + .../Interfaces/IMedicationRepository.cs | 3 +- .../Repositories/MedicationRepository.cs | 13 ++++- 6 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/SM.Medication.Application/Commands/DeleteMedicationCommand.cs diff --git a/src/SM.Medication.Api/EndPoints/MedicationEndPoints.cs b/src/SM.Medication.Api/EndPoints/MedicationEndPoints.cs index d0cc255..a192b2e 100644 --- a/src/SM.Medication.Api/EndPoints/MedicationEndPoints.cs +++ b/src/SM.Medication.Api/EndPoints/MedicationEndPoints.cs @@ -1,4 +1,3 @@ -using System.Runtime.Versioning; using SM.Medication.Api.Extensions; using SM.Medication.Application.Commands; @@ -12,18 +11,33 @@ public static void MapMedicationEndPoints(this WebApplication app) app.MapGet("/medications", async (IMedicationHandler handler) => { - return await handler.Handle(); - }).AddMetadata("Get List of Medications"); + try + { + var result = await handler.Handle(); + + return Results.Ok(result); + } + catch (Exception e) + { + //Log specific exception + return Results.Problem( + e.Message, + statusCode: StatusCodes.Status500InternalServerError); + } + + }) + .AddMetadata("Get List of Medications") + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status200OK, "Success!")); app.MapPost("/medications", - async (IMedicationHandler handler, CreateMedicationCommand request) => + async (IMedicationHandler handler, CreateMedicationCommand command) => { try { - request.Name.GuardString(nameof(request.Name)); + command.Name.GuardString(nameof(command.Name)); - var result = await handler.Handle(request); + var result = await handler.Handle(command); if (!result) return Results.Problem( "Failed to create Medication", @@ -38,15 +52,43 @@ public static void MapMedicationEndPoints(this WebApplication app) e.Message, statusCode: StatusCodes.Status500InternalServerError); } - }).AddMetadata("Create Medication"); + }) + .AddMetadata("Create Medication") + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status201Created, "Created!")); + app.MapDelete("/medications/{medicationName}", + async (string medicationName, IMedicationHandler handler) => + { + try + { + medicationName.GuardString(nameof(medicationName)); + + var command = new DeleteMedicationCommand { Name = medicationName }; + + var result = await handler.Handle(command); + if (!result) + return Results.Problem( + "Failed to delete Medication", + statusCode: StatusCodes.Status500InternalServerError); + + return Results.NoContent(); + } + catch (Exception e) + { + //Log specific exception + return Results.Problem( + e.Message, + statusCode: StatusCodes.Status500InternalServerError); + } + }) + .AddMetadata("Delete Medication") + .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status204NoContent, "Deleted!")); ; } public static RouteHandlerBuilder AddMetadata(this RouteHandlerBuilder builder, string description) { return builder.WithTags(MEDICATION_TAG) .WithMetadata(new SwaggerOperationAttribute(MEDICATION_TAG, description)) - .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status201Created, "Created!")) .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status400BadRequest, "Bad Request!")) .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status401Unauthorized, "You're not Authorized!")) .WithMetadata(new SwaggerResponseAttribute(StatusCodes.Status500InternalServerError, "Failed!")) diff --git a/src/SM.Medication.Application/Commands/DeleteMedicationCommand.cs b/src/SM.Medication.Application/Commands/DeleteMedicationCommand.cs new file mode 100644 index 0000000..df2c06e --- /dev/null +++ b/src/SM.Medication.Application/Commands/DeleteMedicationCommand.cs @@ -0,0 +1,6 @@ +namespace SM.Medication.Application.Commands; + +public class DeleteMedicationCommand +{ + public string? Name { get; set; } +} diff --git a/src/SM.Medication.Application/Handlers/MedicationHandler.cs b/src/SM.Medication.Application/Handlers/MedicationHandler.cs index 5a25c4e..c8ffa29 100644 --- a/src/SM.Medication.Application/Handlers/MedicationHandler.cs +++ b/src/SM.Medication.Application/Handlers/MedicationHandler.cs @@ -33,6 +33,13 @@ public async Task Handle(CreateMedicationCommand command) if (!isMedicationExist) throw new Exception("Medication already exist."); - return await medicationRepository.Add(entity); + var result = await medicationRepository.Add(entity); + return result > 0; + } + + public async Task Handle(DeleteMedicationCommand command) + { + var result = await medicationRepository.Delete(command.Name!); + return result > 0; } } diff --git a/src/SM.Medication.Application/Interfaces/IMedicationHandler.cs b/src/SM.Medication.Application/Interfaces/IMedicationHandler.cs index 175ce8e..ba50218 100644 --- a/src/SM.Medication.Application/Interfaces/IMedicationHandler.cs +++ b/src/SM.Medication.Application/Interfaces/IMedicationHandler.cs @@ -5,4 +5,6 @@ public interface IMedicationHandler { Task> Handle(); Task Handle(CreateMedicationCommand command); + Task Handle(DeleteMedicationCommand command); + } diff --git a/src/SM.Medication.Domain/Interfaces/IMedicationRepository.cs b/src/SM.Medication.Domain/Interfaces/IMedicationRepository.cs index 08a147c..5bf024a 100644 --- a/src/SM.Medication.Domain/Interfaces/IMedicationRepository.cs +++ b/src/SM.Medication.Domain/Interfaces/IMedicationRepository.cs @@ -3,7 +3,8 @@ namespace SM.Medication.Domain.Interfaces; public interface IMedicationRepository { - Task Add(Entities.Medication entity); + Task Add(Entities.Medication entity); + Task Delete(string v); Task> GetAll(); Task GetByName(string name); } diff --git a/src/SM.Medication.Infrastructure/Services/Repositories/MedicationRepository.cs b/src/SM.Medication.Infrastructure/Services/Repositories/MedicationRepository.cs index 4bdb82a..f508851 100644 --- a/src/SM.Medication.Infrastructure/Services/Repositories/MedicationRepository.cs +++ b/src/SM.Medication.Infrastructure/Services/Repositories/MedicationRepository.cs @@ -5,13 +5,20 @@ namespace SM.Medication.Infrastructure.Services.Repositories; public class MedicationRepository(SmartMedMedicationDbContext context) : IMedicationRepository { - public async Task Add(Domain.Entities.Medication entity) + public async Task Add(Domain.Entities.Medication entity) { context.Medications.Add(entity); - var result = await context.SaveChangesAsync(); + return await context.SaveChangesAsync(); + } + + public async Task Delete(string name) + { + var medication = await GetByName(name) ?? throw new Exception("Medication not found."); + + context.Medications.Remove(medication!); - return result > 0; + return await context.SaveChangesAsync(); } public async Task> GetAll()