mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Enforce brewery ownership on update and delete (#267)
This commit is contained in:
@@ -19,16 +19,25 @@ public record UpdateBreweryLocation(
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Updates an existing brewery post. Bound directly from the request body of <c>PUT /api/brewery/{id}</c>.
|
||||
/// A <c>null</c> <see cref="Location" /> clears the brewery's location. <see cref="RowVersion" /> must be the
|
||||
/// row-version last read for this brewery (e.g. from a prior <c>GET</c>); the update is rejected with a
|
||||
/// <c>409 Conflict</c> if the brewery was modified since then.
|
||||
/// Updates an existing brewery post. A <c>null</c> <see cref="Location" /> clears the brewery's location.
|
||||
/// <see cref="RowVersion" /> must be the row-version last read for this brewery (e.g. from a prior
|
||||
/// <c>GET</c>); the update is rejected with a <c>409 Conflict</c> if the brewery was modified since then.
|
||||
/// </summary>
|
||||
public record UpdateBreweryCommand(
|
||||
Guid BreweryPostId,
|
||||
Guid RequestingUserId,
|
||||
byte[] RowVersion,
|
||||
Guid PostedById,
|
||||
string BreweryName,
|
||||
string Description,
|
||||
UpdateBreweryLocation? Location
|
||||
) : IRequest<BreweryDto>;
|
||||
|
||||
/// <summary>Request body for <c>PUT /api/brewery/{id}</c>.</summary>
|
||||
public record UpdateBreweryRequest(
|
||||
Guid BreweryPostId,
|
||||
byte[] RowVersion,
|
||||
string BreweryName,
|
||||
string Description,
|
||||
UpdateBreweryLocation? Location
|
||||
);
|
||||
|
||||
|
||||
@@ -26,13 +26,18 @@ public class UpdateBreweryHandler(IBreweryRepository repository)
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
await repository.EnsureCallerOwnsBreweryAsync(
|
||||
request.BreweryPostId,
|
||||
request.RequestingUserId,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
BreweryPost entity = new()
|
||||
{
|
||||
BreweryPostId = request.BreweryPostId,
|
||||
PostedById = request.PostedById,
|
||||
RowVersion = request.RowVersion,
|
||||
BreweryName = request.BreweryName,
|
||||
Description = request.Description,
|
||||
RowVersion = request.RowVersion,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
Location = request.Location is null
|
||||
? null
|
||||
@@ -48,7 +53,7 @@ public class UpdateBreweryHandler(IBreweryRepository repository)
|
||||
},
|
||||
};
|
||||
|
||||
BreweryPost updated = await repository.UpdateAsync(entity);
|
||||
BreweryPost updated = await repository.UpdateAsync(entity, cancellationToken);
|
||||
return updated.ToDto();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user