Skip to content

Commit

Permalink
feat: implement update Timeline endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
HunorTotBagi committed Jan 8, 2025
1 parent 4a7ce66 commit a120633
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
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);
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
}
}
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);
}
}

0 comments on commit a120633

Please sign in to comment.