-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement update
Timeline
endpoint
- Loading branch information
1 parent
4a7ce66
commit a120633
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Backend/src/Modules/Timelines/Timelines.Api/Endpoints/Timelines/UpdateTimeline.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Timelines.Application.Entities.Timelines.Commands.UpdateTimeline; | ||
|
||
namespace Timelines.Api.Endpoints.Timelines; | ||
|
||
public class UpdateTimeline : ICarterModule | ||
{ | ||
public void AddRoutes(IEndpointRouteBuilder app) | ||
{ | ||
app.MapPut("/Timelines", async (UpdateTimelineRequest request, ISender sender) => | ||
{ | ||
var command = request.Adapt<UpdateTimelineCommand>(); | ||
var result = await sender.Send(command); | ||
var response = result.Adapt<UpdateTimelineResponse>(); | ||
|
||
return Results.Ok(response); | ||
}) | ||
.WithName("UpdateTimeline") | ||
.Produces<UpdateTimelineResponse>() | ||
.ProducesProblem(StatusCodes.Status400BadRequest) | ||
.WithSummary("Update Timeline") | ||
.WithDescription("Update Timeline"); | ||
} | ||
} | ||
|
||
public record UpdateTimelineRequest(TimelineDto Timeline); | ||
|
||
public record UpdateTimelineResponse(bool TimelineUpdated); |
18 changes: 18 additions & 0 deletions
18
...Timelines.Application/Entities/Timelines/Commands/UpdateTimeline/UpdateTimelineCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Timelines.Application.Entities.Timelines.Dtos; | ||
|
||
namespace Timelines.Application.Entities.Timelines.Commands.UpdateTimeline; | ||
|
||
public record UpdateTimelineCommand(TimelineDto Timeline) : ICommand<UpdateTimelineResult>; | ||
|
||
public record UpdateTimelineResult(bool TimelineUpdated); | ||
|
||
public class UpdateTimelineCommandValidator : AbstractValidator<UpdateTimelineCommand> | ||
{ | ||
public UpdateTimelineCommandValidator() | ||
{ | ||
RuleFor(x => x.Timeline.Id).NotEmpty().WithMessage("Id is required."); | ||
RuleFor(x => x.Timeline.Title).NotEmpty().WithMessage("Title is required."); | ||
|
||
// ToDo: Add remaining Timeline command validators | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...Timelines.Application/Entities/Timelines/Commands/UpdateTimeline/UpdateTimelineHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Timelines.Application.Entities.Timelines.Dtos; | ||
using Timelines.Application.Entities.Timelines.Exceptions; | ||
|
||
namespace Timelines.Application.Entities.Timelines.Commands.UpdateTimeline; | ||
|
||
public class UpdateTimelineHandler(ITimelinesDbContext dbContext) : ICommandHandler<UpdateTimelineCommand, UpdateTimelineResult> | ||
{ | ||
public async Task<UpdateTimelineResult> Handle(UpdateTimelineCommand command, CancellationToken cancellationToken) | ||
{ | ||
var timeline = await dbContext.Timelines | ||
.AsNoTracking() | ||
.SingleOrDefaultAsync(n => n.Id == TimelineId.Of(Guid.Parse(command.Timeline.Id)), cancellationToken); | ||
|
||
if (timeline is null) | ||
throw new TimelineNotFoundException(command.Timeline.Id); | ||
|
||
UpdateTimelineWithNewValues(timeline, command.Timeline); | ||
|
||
dbContext.Timelines.Update(timeline); | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
return new UpdateTimelineResult(true); | ||
} | ||
|
||
private static void UpdateTimelineWithNewValues(Timeline timeline, TimelineDto timelineDto) | ||
{ | ||
timeline.Update( | ||
timelineDto.Title); | ||
} | ||
} |