Skip to content

Commit

Permalink
Fixes put endpoint to actually update the pictogramId (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
MertzAndreas authored Nov 26, 2024
1 parent 7deafe9 commit b87664a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
72 changes: 36 additions & 36 deletions GirafAPI/Endpoints/ActivityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,49 +301,49 @@ await dbContext.Entry(citizen)

// PUT updated activity
group.MapPut("/activity/{id:int}", async (int id, UpdateActivityDTO updatedActivity, GirafDbContext dbContext) =>
{
try
{
var activity = await dbContext.Activities.FindAsync(id);

if (activity is null)
try
{
return Results.NotFound("Activity not found.");
}
var activity = await dbContext.Activities
.Include(a => a.Pictogram)
.FirstOrDefaultAsync(a => a.Id == id);

if (updatedActivity.PictogramId is not null)
{
if (activity is null)
{
return Results.NotFound("Activity not found.");
}

var pictogram = await dbContext.Pictograms.FindAsync(updatedActivity.PictogramId);
dbContext.Entry(activity).CurrentValues.SetValues(updatedActivity.ToEntity(id, pictogram));
if (pictogram is null)
{
return Results.BadRequest($"Pictogram with ID {updatedActivity.PictogramId} not found.");
}

activity.Pictogram = pictogram;
dbContext.Entry(activity).CurrentValues.SetValues(updatedActivity.ToEntity(id));
await dbContext.SaveChangesAsync();

return Results.Ok();
}
else
catch (DbUpdateException)
{
dbContext.Entry(activity).CurrentValues.SetValues(updatedActivity.ToEntity(id));
// Database update issue
return Results.BadRequest("Failed to update activity. Ensure the provided data is correct.");
}

await dbContext.SaveChangesAsync();

return Results.Ok();
}
catch (DbUpdateException)
{
// database update issue
return Results.BadRequest("Failed to update activity. Ensure the provided data is correct.");
}
catch (Exception)
{
// Server Error for any unexpected errors
return Results.Problem("An error occurred while updating the activity.", statusCode: StatusCodes.Status500InternalServerError);
}
})
.WithName("UpdateActivity")
.WithDescription("Updates an existing activity using ID.")
.WithTags("Activities")
.Accepts<UpdateActivityDTO>("application/json")
.Produces(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound)
.Produces(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status500InternalServerError);
catch (Exception)
{
// Server error for any unexpected errors
return Results.Problem("An error occurred while updating the activity.", statusCode: StatusCodes.Status500InternalServerError);
}
})
.WithName("UpdateActivity")
.WithDescription("Updates an existing activity using ID.")
.WithTags("Activities")
.Accepts<UpdateActivityDTO>("application/json")
.Produces(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound)
.Produces(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status500InternalServerError);


// PUT IsComplete activity
Expand Down
2 changes: 1 addition & 1 deletion GirafAPI/Entities/Activities/DTOs/UpdateActivityDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public record UpdateActivityDTO(
[Required][StringLength(10)] string StartTime,
[Required][StringLength(10)] string EndTime,
[Required] bool IsCompleted,
int? PictogramId
int PictogramId
);

0 comments on commit b87664a

Please sign in to comment.