mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
Format ./web/backend/Features/Features.Breweries
This commit is contained in:
@@ -18,7 +18,10 @@ public class CreateBreweryHandler(IBreweryRepository repository)
|
|||||||
/// <param name="request">The details of the brewery post to create.</param>
|
/// <param name="request">The details of the brewery post to create.</param>
|
||||||
/// <param name="cancellationToken">A token to observe for cancellation requests.</param>
|
/// <param name="cancellationToken">A token to observe for cancellation requests.</param>
|
||||||
/// <returns>The newly created brewery post.</returns>
|
/// <returns>The newly created brewery post.</returns>
|
||||||
public async Task<BreweryDto> Handle(CreateBreweryCommand request, CancellationToken cancellationToken)
|
public async Task<BreweryDto> Handle(
|
||||||
|
CreateBreweryCommand request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
{
|
{
|
||||||
BreweryPost entity = new()
|
BreweryPost entity = new()
|
||||||
{
|
{
|
||||||
@@ -34,8 +37,8 @@ public class CreateBreweryHandler(IBreweryRepository repository)
|
|||||||
AddressLine1 = request.Location.AddressLine1,
|
AddressLine1 = request.Location.AddressLine1,
|
||||||
AddressLine2 = request.Location.AddressLine2,
|
AddressLine2 = request.Location.AddressLine2,
|
||||||
PostalCode = request.Location.PostalCode,
|
PostalCode = request.Location.PostalCode,
|
||||||
Coordinates = request.Location.Coordinates
|
Coordinates = request.Location.Coordinates,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await repository.CreateAsync(entity);
|
await repository.CreateAsync(entity);
|
||||||
|
|||||||
@@ -15,9 +15,7 @@ public class CreateBreweryValidator : AbstractValidator<CreateBreweryCommand>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public CreateBreweryValidator()
|
public CreateBreweryValidator()
|
||||||
{
|
{
|
||||||
RuleFor(x => x.PostedById)
|
RuleFor(x => x.PostedById).NotEmpty().WithMessage("PostedById is required.");
|
||||||
.NotEmpty()
|
|
||||||
.WithMessage("PostedById is required.");
|
|
||||||
|
|
||||||
RuleFor(x => x.BreweryName)
|
RuleFor(x => x.BreweryName)
|
||||||
.NotEmpty()
|
.NotEmpty()
|
||||||
@@ -31,9 +29,7 @@ public class CreateBreweryValidator : AbstractValidator<CreateBreweryCommand>
|
|||||||
.MaximumLength(512)
|
.MaximumLength(512)
|
||||||
.WithMessage("Description cannot exceed 512 characters.");
|
.WithMessage("Description cannot exceed 512 characters.");
|
||||||
|
|
||||||
RuleFor(x => x.Location)
|
RuleFor(x => x.Location).NotNull().WithMessage("Location is required.");
|
||||||
.NotNull()
|
|
||||||
.WithMessage("Location is required.");
|
|
||||||
|
|
||||||
RuleFor(x => x.Location.CityId)
|
RuleFor(x => x.Location.CityId)
|
||||||
.NotEmpty()
|
.NotEmpty()
|
||||||
|
|||||||
@@ -19,7 +19,10 @@ public class UpdateBreweryHandler(IBreweryRepository repository)
|
|||||||
/// <param name="request">The updated details of the brewery post.</param>
|
/// <param name="request">The updated details of the brewery post.</param>
|
||||||
/// <param name="cancellationToken">A token to observe for cancellation requests.</param>
|
/// <param name="cancellationToken">A token to observe for cancellation requests.</param>
|
||||||
/// <returns>The updated brewery post.</returns>
|
/// <returns>The updated brewery post.</returns>
|
||||||
public async Task<BreweryDto> Handle(UpdateBreweryCommand request, CancellationToken cancellationToken)
|
public async Task<BreweryDto> Handle(
|
||||||
|
UpdateBreweryCommand request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
{
|
{
|
||||||
BreweryPost entity = new()
|
BreweryPost entity = new()
|
||||||
{
|
{
|
||||||
@@ -38,8 +41,8 @@ public class UpdateBreweryHandler(IBreweryRepository repository)
|
|||||||
AddressLine1 = request.Location.AddressLine1,
|
AddressLine1 = request.Location.AddressLine1,
|
||||||
AddressLine2 = request.Location.AddressLine2,
|
AddressLine2 = request.Location.AddressLine2,
|
||||||
PostalCode = request.Location.PostalCode,
|
PostalCode = request.Location.PostalCode,
|
||||||
Coordinates = request.Location.Coordinates
|
Coordinates = request.Location.Coordinates,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await repository.UpdateAsync(entity);
|
await repository.UpdateAsync(entity);
|
||||||
|
|||||||
@@ -41,11 +41,13 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
|||||||
if (brewery is null)
|
if (brewery is null)
|
||||||
return NotFound(new ResponseBody { Message = $"Brewery with ID {id} not found." });
|
return NotFound(new ResponseBody { Message = $"Brewery with ID {id} not found." });
|
||||||
|
|
||||||
return Ok(new ResponseBody<BreweryDto>
|
return Ok(
|
||||||
|
new ResponseBody<BreweryDto>
|
||||||
{
|
{
|
||||||
Message = "Brewery retrieved successfully.",
|
Message = "Brewery retrieved successfully.",
|
||||||
Payload = brewery
|
Payload = brewery,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -59,14 +61,19 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<ResponseBody<IEnumerable<BreweryDto>>>> GetAll(
|
public async Task<ActionResult<ResponseBody<IEnumerable<BreweryDto>>>> GetAll(
|
||||||
[FromQuery] int? limit,
|
[FromQuery] int? limit,
|
||||||
[FromQuery] int? offset)
|
[FromQuery] int? offset
|
||||||
|
)
|
||||||
{
|
{
|
||||||
IEnumerable<BreweryDto> breweries = await mediator.Send(new GetAllBreweriesQuery(limit, offset));
|
IEnumerable<BreweryDto> breweries = await mediator.Send(
|
||||||
return Ok(new ResponseBody<IEnumerable<BreweryDto>>
|
new GetAllBreweriesQuery(limit, offset)
|
||||||
|
);
|
||||||
|
return Ok(
|
||||||
|
new ResponseBody<IEnumerable<BreweryDto>>
|
||||||
{
|
{
|
||||||
Message = "Breweries retrieved successfully.",
|
Message = "Breweries retrieved successfully.",
|
||||||
Payload = breweries
|
Payload = breweries,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -78,14 +85,19 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
|||||||
/// <see cref="BreweryDto" />.
|
/// <see cref="BreweryDto" />.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<ResponseBody<BreweryDto>>> Create([FromBody] CreateBreweryCommand command)
|
public async Task<ActionResult<ResponseBody<BreweryDto>>> Create(
|
||||||
|
[FromBody] CreateBreweryCommand command
|
||||||
|
)
|
||||||
{
|
{
|
||||||
BreweryDto brewery = await mediator.Send(command);
|
BreweryDto brewery = await mediator.Send(command);
|
||||||
return Created($"/api/brewery/{brewery.BreweryPostId}", new ResponseBody<BreweryDto>
|
return Created(
|
||||||
|
$"/api/brewery/{brewery.BreweryPostId}",
|
||||||
|
new ResponseBody<BreweryDto>
|
||||||
{
|
{
|
||||||
Message = "Brewery created successfully.",
|
Message = "Brewery created successfully.",
|
||||||
Payload = brewery
|
Payload = brewery,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -105,17 +117,22 @@ public class BreweryController(IMediator mediator) : ControllerBase
|
|||||||
/// when the route ID does not match the payload ID.
|
/// when the route ID does not match the payload ID.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
[HttpPut("{id:guid}")]
|
[HttpPut("{id:guid}")]
|
||||||
public async Task<ActionResult<ResponseBody<BreweryDto>>> Update(Guid id, [FromBody] UpdateBreweryCommand command)
|
public async Task<ActionResult<ResponseBody<BreweryDto>>> Update(
|
||||||
|
Guid id,
|
||||||
|
[FromBody] UpdateBreweryCommand command
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (command.BreweryPostId != id)
|
if (command.BreweryPostId != id)
|
||||||
return BadRequest(new ResponseBody { Message = "Route ID does not match payload ID." });
|
return BadRequest(new ResponseBody { Message = "Route ID does not match payload ID." });
|
||||||
|
|
||||||
BreweryDto brewery = await mediator.Send(command);
|
BreweryDto brewery = await mediator.Send(command);
|
||||||
return Ok(new ResponseBody<BreweryDto>
|
return Ok(
|
||||||
|
new ResponseBody<BreweryDto>
|
||||||
{
|
{
|
||||||
Message = "Brewery updated successfully.",
|
Message = "Brewery updated successfully.",
|
||||||
Payload = brewery
|
Payload = brewery,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ public static class BreweryDtoMapper
|
|||||||
AddressLine1 = brewery.Location.AddressLine1,
|
AddressLine1 = brewery.Location.AddressLine1,
|
||||||
AddressLine2 = brewery.Location.AddressLine2,
|
AddressLine2 = brewery.Location.AddressLine2,
|
||||||
PostalCode = brewery.Location.PostalCode,
|
PostalCode = brewery.Location.PostalCode,
|
||||||
Coordinates = brewery.Location.Coordinates
|
Coordinates = brewery.Location.Coordinates,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,15 @@ namespace Features.Breweries.Queries.GetAllBreweries;
|
|||||||
public class GetAllBreweriesHandler(IBreweryRepository repository)
|
public class GetAllBreweriesHandler(IBreweryRepository repository)
|
||||||
: IRequestHandler<GetAllBreweriesQuery, IEnumerable<BreweryDto>>
|
: IRequestHandler<GetAllBreweriesQuery, IEnumerable<BreweryDto>>
|
||||||
{
|
{
|
||||||
public async Task<IEnumerable<BreweryDto>> Handle(GetAllBreweriesQuery request, CancellationToken cancellationToken)
|
public async Task<IEnumerable<BreweryDto>> Handle(
|
||||||
|
GetAllBreweriesQuery request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
{
|
{
|
||||||
IEnumerable<BreweryPost> breweries = await repository.GetAllAsync(request.Limit, request.Offset);
|
IEnumerable<BreweryPost> breweries = await repository.GetAllAsync(
|
||||||
|
request.Limit,
|
||||||
|
request.Offset
|
||||||
|
);
|
||||||
return breweries.Select(b => b.ToDto());
|
return breweries.Select(b => b.ToDto());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,10 @@ namespace Features.Breweries.Queries.GetBreweryById;
|
|||||||
public class GetBreweryByIdHandler(IBreweryRepository repository)
|
public class GetBreweryByIdHandler(IBreweryRepository repository)
|
||||||
: IRequestHandler<GetBreweryByIdQuery, BreweryDto?>
|
: IRequestHandler<GetBreweryByIdQuery, BreweryDto?>
|
||||||
{
|
{
|
||||||
public async Task<BreweryDto?> Handle(GetBreweryByIdQuery request, CancellationToken cancellationToken)
|
public async Task<BreweryDto?> Handle(
|
||||||
|
GetBreweryByIdQuery request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
{
|
{
|
||||||
BreweryPost? brewery = await repository.GetByIdAsync(request.BreweryPostId);
|
BreweryPost? brewery = await repository.GetByIdAsync(request.BreweryPostId);
|
||||||
return brewery?.ToDto();
|
return brewery?.ToDto();
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ namespace Features.Breweries.Repository;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="connectionFactory">The factory used to create database connections.</param>
|
/// <param name="connectionFactory">The factory used to create database connections.</param>
|
||||||
public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||||
: Repository<BreweryPost>(connectionFactory), IBreweryRepository
|
: Repository<BreweryPost>(connectionFactory),
|
||||||
|
IBreweryRepository
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves a brewery post by ID using the <c>USP_GetBreweryById</c> stored procedure.
|
/// Retrieves a brewery post by ID using the <c>USP_GetBreweryById</c> stored procedure.
|
||||||
@@ -29,7 +30,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
AddParameter(command, "@BreweryPostID", id);
|
AddParameter(command, "@BreweryPostID", id);
|
||||||
|
|
||||||
await using DbDataReader reader = await command.ExecuteReaderAsync();
|
await using DbDataReader reader = await command.ExecuteReaderAsync();
|
||||||
if (await reader.ReadAsync()) return MapToEntity(reader);
|
if (await reader.ReadAsync())
|
||||||
|
return MapToEntity(reader);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +60,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
await using DbDataReader reader = await command.ExecuteReaderAsync();
|
await using DbDataReader reader = await command.ExecuteReaderAsync();
|
||||||
List<BreweryPost> breweries = new();
|
List<BreweryPost> breweries = new();
|
||||||
|
|
||||||
while (await reader.ReadAsync()) breweries.Add(MapToEntity(reader));
|
while (await reader.ReadAsync())
|
||||||
|
breweries.Add(MapToEntity(reader));
|
||||||
|
|
||||||
return breweries;
|
return breweries;
|
||||||
}
|
}
|
||||||
@@ -121,7 +124,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
command.CommandText = "USP_CreateBrewery";
|
command.CommandText = "USP_CreateBrewery";
|
||||||
command.CommandType = CommandType.StoredProcedure;
|
command.CommandType = CommandType.StoredProcedure;
|
||||||
|
|
||||||
if (brewery.Location is null) throw new ArgumentException("Location must be provided when creating a brewery.");
|
if (brewery.Location is null)
|
||||||
|
throw new ArgumentException("Location must be provided when creating a brewery.");
|
||||||
|
|
||||||
AddParameter(command, "@BreweryName", brewery.BreweryName);
|
AddParameter(command, "@BreweryName", brewery.BreweryName);
|
||||||
AddParameter(command, "@Description", brewery.Description);
|
AddParameter(command, "@Description", brewery.Description);
|
||||||
@@ -195,7 +199,7 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
PostalCode = reader.GetString(reader.GetOrdinal("PostalCode")),
|
PostalCode = reader.GetString(reader.GetOrdinal("PostalCode")),
|
||||||
Coordinates = reader.IsDBNull(reader.GetOrdinal("Coordinates"))
|
Coordinates = reader.IsDBNull(reader.GetOrdinal("Coordinates"))
|
||||||
? null
|
? null
|
||||||
: reader.GetFieldValue<byte[]>(reader.GetOrdinal("Coordinates"))
|
: reader.GetFieldValue<byte[]>(reader.GetOrdinal("Coordinates")),
|
||||||
};
|
};
|
||||||
brewery.Location = location;
|
brewery.Location = location;
|
||||||
}
|
}
|
||||||
@@ -215,11 +219,7 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
/// <param name="command">The command to add the parameter to.</param>
|
/// <param name="command">The command to add the parameter to.</param>
|
||||||
/// <param name="name">The parameter name (including any prefix, e.g. "@BreweryName").</param>
|
/// <param name="name">The parameter name (including any prefix, e.g. "@BreweryName").</param>
|
||||||
/// <param name="value">The parameter value, or <c>null</c> to bind <see cref="DBNull.Value" />.</param>
|
/// <param name="value">The parameter value, or <c>null</c> to bind <see cref="DBNull.Value" />.</param>
|
||||||
private static void AddParameter(
|
private static void AddParameter(DbCommand command, string name, object? value)
|
||||||
DbCommand command,
|
|
||||||
string name,
|
|
||||||
object? value
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
DbParameter p = command.CreateParameter();
|
DbParameter p = command.CreateParameter();
|
||||||
p.ParameterName = name;
|
p.ParameterName = name;
|
||||||
|
|||||||
Reference in New Issue
Block a user