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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ using MediatR;
|
||||
|
||||
namespace Features.Breweries.Commands.DeleteBrewery;
|
||||
|
||||
/// <summary>Deletes a brewery post. Bound directly from the route of <c>DELETE /api/brewery/{id}</c>.</summary>
|
||||
public record DeleteBreweryCommand(Guid BreweryPostId) : IRequest;
|
||||
/// <summary>Deletes a brewery post. <see cref="BreweryPostId" /> is bound from the route of <c>DELETE /api/brewery/{id}</c>.</summary>
|
||||
public record DeleteBreweryCommand(Guid BreweryPostId, Guid RequestingUserId) : IRequest;
|
||||
|
||||
@@ -10,8 +10,17 @@ public class DeleteBreweryHandler(IBreweryRepository repository)
|
||||
/// <exception cref="Domain.Exceptions.NotFoundException">
|
||||
/// Thrown when no brewery exists with <paramref name="request" />'s <c>BreweryPostId</c>.
|
||||
/// </exception>
|
||||
public Task Handle(DeleteBreweryCommand request, CancellationToken cancellationToken)
|
||||
/// <exception cref="Domain.Exceptions.ForbiddenException">
|
||||
/// Thrown when <paramref name="request" />'s <c>RequestingUserId</c> did not post the brewery.
|
||||
/// </exception>
|
||||
public async Task Handle(DeleteBreweryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
return repository.DeleteAsync(request.BreweryPostId);
|
||||
await repository.EnsureCallerOwnsBreweryAsync(
|
||||
request.BreweryPostId,
|
||||
request.RequestingUserId,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
await repository.DeleteAsync(request.BreweryPostId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,13 +70,13 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
||||
/// <remarks>Allows anonymous access.</remarks>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("locations")]
|
||||
public async Task<ActionResult<ResponseBody<IEnumerable<SimplifiedBreweryDto>>>> GetAllLocations()
|
||||
public async Task<ActionResult<ResponseBody<IEnumerable<BreweryWithLocationDto>>>> GetAllLocations()
|
||||
{
|
||||
IEnumerable<SimplifiedBreweryDto> breweries = await mediator.Send(
|
||||
IEnumerable<BreweryWithLocationDto> breweries = await mediator.Send(
|
||||
new GetAllBreweryLocationsQuery()
|
||||
);
|
||||
return Ok(
|
||||
new ResponseBody<IEnumerable<SimplifiedBreweryDto>>
|
||||
new ResponseBody<IEnumerable<BreweryWithLocationDto>>
|
||||
{
|
||||
Message = "Brewery locations retrieved successfully.",
|
||||
Payload = breweries,
|
||||
@@ -91,17 +91,17 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
||||
/// <remarks>Allows anonymous access.</remarks>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("locations/nearby")]
|
||||
public async Task<ActionResult<ResponseBody<IEnumerable<SimplifiedBreweryDto>>>> GetLocationsWithinRange(
|
||||
public async Task<ActionResult<ResponseBody<IEnumerable<BreweryWithLocationDto>>>> GetLocationsWithinRange(
|
||||
[FromQuery] double latitude,
|
||||
[FromQuery] double longitude,
|
||||
[FromQuery] double rangeInMetres
|
||||
)
|
||||
{
|
||||
IEnumerable<SimplifiedBreweryDto> breweries = await mediator.Send(
|
||||
IEnumerable<BreweryWithLocationDto> breweries = await mediator.Send(
|
||||
new GetBreweryLocationsWithinRangeQuery(latitude, longitude, rangeInMetres)
|
||||
);
|
||||
return Ok(
|
||||
new ResponseBody<IEnumerable<SimplifiedBreweryDto>>
|
||||
new ResponseBody<IEnumerable<BreweryWithLocationDto>>
|
||||
{
|
||||
Message = "Brewery locations retrieved successfully.",
|
||||
Payload = breweries,
|
||||
@@ -113,10 +113,17 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
||||
/// <returns><c>201 Created</c> with the newly created brewery.</returns>
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ResponseBody<BreweryDto>>> Create(
|
||||
[FromBody] CreateBreweryCommand command
|
||||
[FromBody] CreateBreweryRequest request
|
||||
)
|
||||
{
|
||||
BreweryDto brewery = await mediator.Send(command);
|
||||
BreweryDto brewery = await mediator.Send(
|
||||
new CreateBreweryCommand(
|
||||
User.GetAuthenticatedUserId(),
|
||||
request.BreweryName,
|
||||
request.Description,
|
||||
request.Location
|
||||
)
|
||||
);
|
||||
return Created(
|
||||
$"/api/brewery/{brewery.BreweryPostId}",
|
||||
new ResponseBody<BreweryDto>
|
||||
@@ -128,27 +135,37 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
||||
}
|
||||
|
||||
/// <summary>Updates an existing brewery post.</summary>
|
||||
/// <param name="id">Must match <paramref name="command" />'s <c>BreweryPostId</c>.</param>
|
||||
/// <param name="id">Must match <paramref name="request" />'s <c>BreweryPostId</c>.</param>
|
||||
/// <returns>
|
||||
/// <c>200 OK</c> with the updated brewery; <c>400 Bad Request</c> if the route ID does not match the
|
||||
/// payload ID; <c>404 Not Found</c> if the brewery or its <c>CityId</c> does not exist; or
|
||||
/// <c>409 Conflict</c> if the brewery was modified since <c>command.RowVersion</c> was read.
|
||||
/// <c>409 Conflict</c> if the brewery was modified since <c>request.RowVersion</c> was read.
|
||||
/// </returns>
|
||||
[HttpPut("{id:guid}")]
|
||||
public async Task<ActionResult<ResponseBody<BreweryDto>>> Update(
|
||||
Guid id,
|
||||
[FromBody] UpdateBreweryCommand command
|
||||
[FromBody] UpdateBreweryRequest request
|
||||
)
|
||||
{
|
||||
if (command.BreweryPostId != id)
|
||||
if (request.BreweryPostId != id)
|
||||
return BadRequest(new ResponseBody { Message = "Route ID does not match payload ID." });
|
||||
|
||||
BreweryDto brewery = await mediator.Send(command);
|
||||
BreweryDto breweryUpdated = await mediator.Send(
|
||||
new UpdateBreweryCommand(
|
||||
request.BreweryPostId,
|
||||
User.GetAuthenticatedUserId(),
|
||||
request.RowVersion,
|
||||
request.BreweryName,
|
||||
request.Description,
|
||||
request.Location
|
||||
)
|
||||
);
|
||||
|
||||
return Ok(
|
||||
new ResponseBody<BreweryDto>
|
||||
{
|
||||
Message = "Brewery updated successfully.",
|
||||
Payload = brewery,
|
||||
Payload = breweryUpdated,
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -158,7 +175,7 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
||||
[HttpDelete("{id:guid}")]
|
||||
public async Task<ActionResult<ResponseBody>> Delete(Guid id)
|
||||
{
|
||||
await mediator.Send(new DeleteBreweryCommand(id));
|
||||
await mediator.Send(new DeleteBreweryCommand(id, User.GetAuthenticatedUserId()));
|
||||
return Ok(new ResponseBody { Message = "Brewery deleted successfully." });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public record BreweryDto(
|
||||
/// The distance, in metres, from a query's origin point, or <see langword="null"/> for queries with no
|
||||
/// origin point (e.g. <c>GET /api/brewery/locations</c>).
|
||||
/// </param>
|
||||
public record SimplifiedBreweryDto(
|
||||
public record BreweryWithLocationDto(
|
||||
Guid BreweryPostId,
|
||||
string BreweryName,
|
||||
BreweryLocationDto? Location,
|
||||
|
||||
@@ -45,9 +45,9 @@ public static class BreweryDtoMapper
|
||||
/// Converts a <see cref="BreweryPost" /> to its simplified DTO representation, used for
|
||||
/// location-based queries that only need identifying and location details.
|
||||
/// </summary>
|
||||
public static SimplifiedBreweryDto ToSimplifiedDto(this BreweryPost brewery)
|
||||
public static BreweryWithLocationDto ToSimplifiedDto(this BreweryPost brewery)
|
||||
{
|
||||
return new SimplifiedBreweryDto(
|
||||
return new BreweryWithLocationDto(
|
||||
BreweryPostId: brewery.BreweryPostId,
|
||||
BreweryName: brewery.BreweryName,
|
||||
Location: brewery.Location is null
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace Features.Breweries.Queries.GetAllBreweryLocations;
|
||||
|
||||
/// <summary>Handles <see cref="GetAllBreweryLocationsQuery" /> by retrieving all located brewery posts.</summary>
|
||||
public class GetAllBreweryLocationsHandler(IBreweryRepository repository)
|
||||
: IRequestHandler<GetAllBreweryLocationsQuery, IEnumerable<SimplifiedBreweryDto>>
|
||||
: IRequestHandler<GetAllBreweryLocationsQuery, IEnumerable<BreweryWithLocationDto>>
|
||||
{
|
||||
/// <summary>Retrieves every brewery post that has a set location.</summary>
|
||||
public async Task<IEnumerable<SimplifiedBreweryDto>> Handle(
|
||||
public async Task<IEnumerable<BreweryWithLocationDto>> Handle(
|
||||
GetAllBreweryLocationsQuery request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
|
||||
@@ -7,4 +7,4 @@ namespace Features.Breweries.Queries.GetAllBreweryLocations;
|
||||
/// Retrieves all brewery posts that have a set location. Bound directly from
|
||||
/// <c>GET /api/brewery/locations</c>.
|
||||
/// </summary>
|
||||
public record GetAllBreweryLocationsQuery : IRequest<IEnumerable<SimplifiedBreweryDto>>;
|
||||
public record GetAllBreweryLocationsQuery : IRequest<IEnumerable<BreweryWithLocationDto>>;
|
||||
|
||||
@@ -15,7 +15,7 @@ public class GetBreweryByIdHandler(IBreweryRepository repository)
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
BreweryPost? brewery = await repository.GetByIdAsync(request.BreweryPostId);
|
||||
BreweryPost? brewery = await repository.GetByIdAsync(request.BreweryPostId, cancellationToken);
|
||||
return brewery?.ToDto();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace Features.Breweries.Queries.GetBreweryLocationsWithinRange;
|
||||
/// Handles <see cref="GetBreweryLocationsWithinRangeQuery" /> by retrieving nearby located brewery posts.
|
||||
/// </summary>
|
||||
public class GetBreweryLocationsWithinRangeHandler(IBreweryRepository repository)
|
||||
: IRequestHandler<GetBreweryLocationsWithinRangeQuery, IEnumerable<SimplifiedBreweryDto>>
|
||||
: IRequestHandler<GetBreweryLocationsWithinRangeQuery, IEnumerable<BreweryWithLocationDto>>
|
||||
{
|
||||
/// <summary>Retrieves brewery posts within range of the requested coordinates, nearest first.</summary>
|
||||
public async Task<IEnumerable<SimplifiedBreweryDto>> Handle(
|
||||
public async Task<IEnumerable<BreweryWithLocationDto>> Handle(
|
||||
GetBreweryLocationsWithinRangeQuery request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
|
||||
@@ -12,4 +12,4 @@ namespace Features.Breweries.Queries.GetBreweryLocationsWithinRange;
|
||||
/// <param name="Longitude">The origin longitude, in decimal degrees.</param>
|
||||
/// <param name="RangeInMetres">The maximum distance, in metres, from the origin coordinates.</param>
|
||||
public record GetBreweryLocationsWithinRangeQuery(double Latitude, double Longitude, double RangeInMetres)
|
||||
: IRequest<IEnumerable<SimplifiedBreweryDto>>;
|
||||
: IRequest<IEnumerable<BreweryWithLocationDto>>;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using Domain.Exceptions;
|
||||
|
||||
namespace Features.Breweries.Repository;
|
||||
|
||||
/// <summary>Shared ownership check for commands that mutate a specific brewery post.</summary>
|
||||
public static class BreweryOwnershipExtensions
|
||||
{
|
||||
/// <exception cref="Domain.Exceptions.NotFoundException">Thrown when no brewery exists with the given ID.</exception>
|
||||
/// <exception cref="Domain.Exceptions.ForbiddenException">Thrown when <paramref name="requestingUserId" /> did not post the brewery.</exception>
|
||||
public static async Task EnsureCallerOwnsBreweryAsync(
|
||||
this IBreweryRepository repository,
|
||||
Guid breweryPostId,
|
||||
Guid requestingUserId,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
Guid postedById =
|
||||
await repository.GetPostedByIdAsync(breweryPostId, cancellationToken)
|
||||
?? throw new NotFoundException($"Brewery with ID {breweryPostId} not found.");
|
||||
|
||||
if (postedById != requestingUserId)
|
||||
throw new ForbiddenException("You are not authorized to modify this brewery.");
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<BreweryPost?> GetByIdAsync(Guid id)
|
||||
public async Task<BreweryPost?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using DbConnection connection = await CreateConnection();
|
||||
IEnumerable<BreweryPost> results = await connection.QueryAsync<
|
||||
@@ -39,43 +39,59 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
Country,
|
||||
BreweryPost
|
||||
>(
|
||||
"""
|
||||
SELECT
|
||||
bp.BreweryPostID,
|
||||
bp.PostedByID,
|
||||
bp.BreweryName,
|
||||
bp.Description,
|
||||
bp.CreatedAt,
|
||||
bp.UpdatedAt,
|
||||
bp.RowVersion,
|
||||
bpl.BreweryPostLocationID,
|
||||
bpl.AddressLine1,
|
||||
bpl.AddressLine2,
|
||||
bpl.PostalCode,
|
||||
CONVERT(varbinary(max), bpl.Coordinates) AS Coordinates,
|
||||
c.CityID,
|
||||
c.CityName,
|
||||
sp.StateProvinceID,
|
||||
sp.StateProvinceName,
|
||||
sp.ISO3166_2 AS Iso31662,
|
||||
co.CountryID,
|
||||
co.CountryName,
|
||||
co.ISO3166_1 AS Iso31661
|
||||
FROM dbo.BreweryPost bp
|
||||
LEFT JOIN dbo.BreweryPostLocation bpl ON bp.BreweryPostID = bpl.BreweryPostID
|
||||
LEFT JOIN dbo.City c ON bpl.CityID = c.CityID
|
||||
LEFT JOIN dbo.StateProvince sp ON c.StateProvinceID = sp.StateProvinceID
|
||||
LEFT JOIN dbo.Country co ON sp.CountryID = co.CountryID
|
||||
WHERE bp.BreweryPostID = @BreweryPostId
|
||||
""",
|
||||
new CommandDefinition(
|
||||
"""
|
||||
SELECT
|
||||
bp.BreweryPostID,
|
||||
bp.PostedByID,
|
||||
bp.BreweryName,
|
||||
bp.Description,
|
||||
bp.CreatedAt,
|
||||
bp.UpdatedAt,
|
||||
bp.RowVersion,
|
||||
bpl.BreweryPostLocationID,
|
||||
bpl.AddressLine1,
|
||||
bpl.AddressLine2,
|
||||
bpl.PostalCode,
|
||||
CONVERT(varbinary(max), bpl.Coordinates) AS Coordinates,
|
||||
c.CityID,
|
||||
c.CityName,
|
||||
sp.StateProvinceID,
|
||||
sp.StateProvinceName,
|
||||
sp.ISO3166_2 AS Iso31662,
|
||||
co.CountryID,
|
||||
co.CountryName,
|
||||
co.ISO3166_1 AS Iso31661
|
||||
FROM dbo.BreweryPost bp
|
||||
LEFT JOIN dbo.BreweryPostLocation bpl ON bp.BreweryPostID = bpl.BreweryPostID
|
||||
LEFT JOIN dbo.City c ON bpl.CityID = c.CityID
|
||||
LEFT JOIN dbo.StateProvince sp ON c.StateProvinceID = sp.StateProvinceID
|
||||
LEFT JOIN dbo.Country co ON sp.CountryID = co.CountryID
|
||||
WHERE bp.BreweryPostID = @BreweryPostId
|
||||
""",
|
||||
new { BreweryPostId = id },
|
||||
cancellationToken: cancellationToken
|
||||
),
|
||||
MapBreweryRow,
|
||||
new { BreweryPostId = id },
|
||||
splitOn: "BreweryPostLocationID,CityID,StateProvinceID,CountryID"
|
||||
);
|
||||
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Guid?> GetPostedByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using DbConnection connection = await CreateConnection();
|
||||
return await connection.ExecuteScalarAsync<Guid?>(
|
||||
new CommandDefinition(
|
||||
"SELECT PostedByID FROM dbo.BreweryPost WHERE BreweryPostID = @BreweryPostId",
|
||||
new { BreweryPostId = id },
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit, int? offset)
|
||||
{
|
||||
@@ -221,10 +237,10 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<BreweryPost> UpdateAsync(BreweryPost brewery)
|
||||
public async Task<BreweryPost> UpdateAsync(BreweryPost brewery, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using DbConnection connection = await CreateConnection();
|
||||
await using DbTransaction transaction = await connection.BeginTransactionAsync();
|
||||
await using DbTransaction transaction = await connection.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -238,7 +254,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
BreweryPostID = @BreweryPostId
|
||||
""",
|
||||
new { brewery.BreweryPostId },
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
)
|
||||
is not null;
|
||||
@@ -253,7 +270,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
new CommandDefinition(
|
||||
"SELECT 1 FROM dbo.City WHERE CityID = @CityId",
|
||||
new { brewery.Location.CityId },
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
)
|
||||
is not null;
|
||||
@@ -280,7 +298,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
brewery.Description,
|
||||
brewery.RowVersion,
|
||||
},
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
);
|
||||
|
||||
@@ -300,7 +319,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
WHERE BreweryPostID = @BreweryPostId
|
||||
""",
|
||||
new { brewery.BreweryPostId },
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -315,7 +335,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
WHERE BreweryPostID = @BreweryPostId
|
||||
""",
|
||||
new { brewery.BreweryPostId },
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
)
|
||||
is not null;
|
||||
@@ -346,7 +367,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
Longitude = brewery.Location.Coordinates?.Longitude,
|
||||
Srid = GeographySrid,
|
||||
},
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
);
|
||||
else
|
||||
@@ -385,12 +407,13 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
Longitude = brewery.Location.Coordinates?.Longitude,
|
||||
Srid = GeographySrid,
|
||||
},
|
||||
transaction
|
||||
transaction,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync();
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -398,7 +421,7 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
throw;
|
||||
}
|
||||
|
||||
return await GetByIdAsync(brewery.BreweryPostId)
|
||||
return await GetByIdAsync(brewery.BreweryPostId, cancellationToken)
|
||||
?? throw new InvalidOperationException(
|
||||
$"Brewery '{brewery.BreweryPostId}' was not found after a successful update."
|
||||
);
|
||||
@@ -542,25 +565,27 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||
/// Composes a joined row's <see cref="BreweryPost" />, <see cref="BreweryPostLocation" />,
|
||||
/// <see cref="City" />, <see cref="StateProvince" /> and <see cref="Country" /> fragments into a
|
||||
/// single populated <see cref="BreweryPost" />. When the row has no location (a left-joined
|
||||
/// brewery with no address on file), <paramref name="location" /> is a blank instance produced
|
||||
/// from all-<c>NULL</c> columns; the schema guarantees that whenever a location row was joined
|
||||
/// in, its City/StateProvince/Country chain was too.
|
||||
/// brewery with no address on file), Dapper passes <see langword="null" /> for
|
||||
/// <paramref name="location" /> rather than a blank instance, since its identity column
|
||||
/// (<c>BreweryPostLocationID</c>) is <c>NULL</c>; the schema guarantees that whenever a location
|
||||
/// row was joined in, its City/StateProvince/Country chain was too.
|
||||
/// </summary>
|
||||
private static BreweryPost MapBreweryRow(
|
||||
BreweryPost post,
|
||||
BreweryPostLocation location,
|
||||
City city,
|
||||
StateProvince stateProvince,
|
||||
Country country
|
||||
BreweryPostLocation? location,
|
||||
City? city,
|
||||
StateProvince? stateProvince,
|
||||
Country? country
|
||||
)
|
||||
{
|
||||
if (location.BreweryPostLocationId == Guid.Empty)
|
||||
if (location is null)
|
||||
return post;
|
||||
|
||||
stateProvince.CountryId = country.CountryId;
|
||||
// The schema guarantees that whenever a location row was joined in, its City/StateProvince/
|
||||
// Country chain was too, so these are never null alongside a non-null location.
|
||||
city!.StateProvinceId = stateProvince!.StateProvinceId;
|
||||
stateProvince.CountryId = country!.CountryId;
|
||||
stateProvince.Country = country;
|
||||
|
||||
city.StateProvinceId = stateProvince.StateProvinceId;
|
||||
city.StateProvince = stateProvince;
|
||||
|
||||
location.BreweryPostId = post.BreweryPostId;
|
||||
|
||||
@@ -11,7 +11,13 @@ public interface IBreweryRepository
|
||||
/// Retrieves a brewery post by ID, joined to its location. Returns <c>null</c> if no brewery post
|
||||
/// exists with the given ID, or if it has no associated location.
|
||||
/// </summary>
|
||||
Task<BreweryPost?> GetByIdAsync(Guid id);
|
||||
Task<BreweryPost?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the <c>PostedById</c> of the brewery post with the given ID, without joining its
|
||||
/// location. Returns <c>null</c> if no brewery post exists with the given ID.
|
||||
/// </summary>
|
||||
Task<Guid?> GetPostedByIdAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves all brewery posts, optionally paginated, ordered by creation date descending. Posts
|
||||
@@ -50,7 +56,7 @@ public interface IBreweryRepository
|
||||
/// Thrown when <paramref name="brewery" />'s <c>RowVersion</c> no longer matches the stored row (it was
|
||||
/// modified by another request since it was last read).
|
||||
/// </exception>
|
||||
Task<BreweryPost> UpdateAsync(BreweryPost brewery);
|
||||
Task<BreweryPost> UpdateAsync(BreweryPost brewery, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>Deletes a brewery post by ID. Its location and photos are removed via cascading foreign keys.</summary>
|
||||
/// <exception cref="Domain.Exceptions.NotFoundException">Thrown when no brewery exists with the given <paramref name="id" />.</exception>
|
||||
|
||||
Reference in New Issue
Block a user