ac3f34e4f3
Added .gitignore, solution, and project files for .NET 10. Introduced AGENTS.md and PLAN.md for guidelines and planning. Implemented appsettings.json, launchSettings.json, and MySQL config. Created AppDBContext and all domain models with composite keys. Added IUnitOfWork and EFUnitOfWork. Structured feature folders for Region, Route, Vehicle, Store, Warehouse, and Order with DTOs, repositories, services, and controllers. Added initial EF Core migrations and sample HTTP request. Registered dependencies and enabled OpenAPI in development.
86 lines
2.5 KiB
C#
86 lines
2.5 KiB
C#
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.Warehouse;
|
|
|
|
public class WarehouseService(IWarehouseRepository repo, IUnitOfWork uow)
|
|
{
|
|
private static WarehouseResponse ToResponse(Models.Warehouse w) =>
|
|
new(w.Id, w.Name, w.Location, w.Type, w.VehicleId, w.IsActive, w.CreatedAt);
|
|
|
|
public async Task<List<WarehouseResponse>> GetAllAsync() =>
|
|
(await repo.GetAllAsync())
|
|
.Where(w => w.IsActive)
|
|
.Select(ToResponse)
|
|
.ToList();
|
|
|
|
public async Task<WarehouseResponse?> GetByIdAsync(Guid id)
|
|
{
|
|
var warehouse = await repo.GetByIdAsync(id);
|
|
if (warehouse is null || !warehouse.IsActive) return null;
|
|
return ToResponse(warehouse);
|
|
}
|
|
|
|
public async Task<WarehouseResponse> CreateAsync(CreateWarehouseRequest request, CancellationToken ct = default)
|
|
{
|
|
var warehouse = new Models.Warehouse
|
|
{
|
|
Name = request.Name,
|
|
Location = request.Location,
|
|
Type = request.Type,
|
|
VehicleId = request.VehicleId
|
|
};
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.AddAsync(warehouse);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(warehouse);
|
|
}
|
|
|
|
public async Task<WarehouseResponse?> UpdateAsync(Guid id, UpdateWarehouseRequest request, CancellationToken ct = default)
|
|
{
|
|
var warehouse = await repo.GetByIdAsync(id);
|
|
if (warehouse is null || !warehouse.IsActive) return null;
|
|
warehouse.Name = request.Name;
|
|
warehouse.Location = request.Location;
|
|
warehouse.Type = request.Type;
|
|
warehouse.VehicleId = request.VehicleId;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Update(warehouse);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(warehouse);
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var warehouse = await repo.GetByIdAsync(id);
|
|
if (warehouse is null || !warehouse.IsActive) return false;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Remove(warehouse);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return true;
|
|
}
|
|
}
|