Skip to content

Commit

Permalink
Adding delete medication
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdaquina committed Jul 26, 2024
1 parent 1ad01de commit d31dffb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
58 changes: 50 additions & 8 deletions src/SM.Medication.Api/EndPoints/MedicationEndPoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Runtime.Versioning;
using SM.Medication.Api.Extensions;
using SM.Medication.Application.Commands;

Expand All @@ -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",
Expand All @@ -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!"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SM.Medication.Application.Commands;

public class DeleteMedicationCommand
{
public string? Name { get; set; }
}
9 changes: 8 additions & 1 deletion src/SM.Medication.Application/Handlers/MedicationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public async Task<bool> 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<bool> Handle(DeleteMedicationCommand command)
{
var result = await medicationRepository.Delete(command.Name!);
return result > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public interface IMedicationHandler
{
Task<List<MedicationDTO>> Handle();
Task<bool> Handle(CreateMedicationCommand command);
Task<bool> Handle(DeleteMedicationCommand command);

}
3 changes: 2 additions & 1 deletion src/SM.Medication.Domain/Interfaces/IMedicationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace SM.Medication.Domain.Interfaces;

public interface IMedicationRepository
{
Task<bool> Add(Entities.Medication entity);
Task<int> Add(Entities.Medication entity);
Task<int> Delete(string v);
Task<List<Domain.Entities.Medication>> GetAll();
Task<Domain.Entities.Medication?> GetByName(string name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ namespace SM.Medication.Infrastructure.Services.Repositories;

public class MedicationRepository(SmartMedMedicationDbContext context) : IMedicationRepository
{
public async Task<bool> Add(Domain.Entities.Medication entity)
public async Task<int> Add(Domain.Entities.Medication entity)
{
context.Medications.Add(entity);

var result = await context.SaveChangesAsync();
return await context.SaveChangesAsync();
}

public async Task<int> 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<List<Domain.Entities.Medication>> GetAll()
Expand Down

0 comments on commit d31dffb

Please sign in to comment.