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:
@@ -23,3 +23,10 @@ public record CreateBreweryCommand(
|
||||
string Description,
|
||||
CreateBreweryLocation Location
|
||||
) : IRequest<BreweryDto>;
|
||||
|
||||
/// <summary>Request body for <c>POST /api/brewery</c>. <c>PostedById</c> comes from the caller's JWT.</summary>
|
||||
public record CreateBreweryRequest(
|
||||
string BreweryName,
|
||||
string Description,
|
||||
CreateBreweryLocation Location
|
||||
);
|
||||
@@ -37,6 +37,17 @@ public class CreateBreweryHandler(IBreweryRepository repository)
|
||||
};
|
||||
|
||||
await repository.CreateAsync(entity);
|
||||
return entity.ToDto();
|
||||
|
||||
// RowVersion is a DB-computed column, unset on the in-memory entity; re-fetch so callers
|
||||
// get back the value needed for a subsequent optimistic-concurrency update. Uses
|
||||
// CancellationToken.None since the write already committed: cancelling the caller's request
|
||||
// at this point must not turn a successful creation into a thrown exception.
|
||||
BreweryPost created =
|
||||
await repository.GetByIdAsync(entity.BreweryPostId, CancellationToken.None)
|
||||
?? throw new InvalidOperationException(
|
||||
$"Brewery '{entity.BreweryPostId}' was not found after a successful creation."
|
||||
);
|
||||
|
||||
return created.ToDto();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user