using Distribution.Infra.UoW; namespace Distribution.Features.Store; public class StoreService(IStoreRepository repo, IUnitOfWork uow) { private static StoreResponse ToResponse(Models.Store s) => new(s.Id, s.OwnerName, s.StoreName, s.StoreAddress, s.Latitude, s.Longitude, s.OneTimeAmount, s.CreditAmount, s.IsActive, s.CreatedAt); public async Task> GetAllAsync() => (await repo.GetAllAsync()) .Where(s => s.IsActive) .Select(ToResponse) .ToList(); public async Task GetByIdAsync(Guid id) { var store = await repo.GetByIdAsync(id); if (store is null || !store.IsActive) return null; return ToResponse(store); } public async Task CreateAsync(CreateStoreRequest request, CancellationToken ct = default) { var store = new Models.Store { OwnerName = request.OwnerName, StoreName = request.StoreName, StoreAddress = request.StoreAddress, Latitude = request.Latitude, Longitude = request.Longitude, OneTimeAmount = request.OneTimeAmount, CreditAmount = request.CreditAmount }; await uow.BeginAsync(ct); try { await repo.AddAsync(store); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return ToResponse(store); } public async Task UpdateAsync(Guid id, UpdateStoreRequest request, CancellationToken ct = default) { var store = await repo.GetByIdAsync(id); if (store is null || !store.IsActive) return null; store.OwnerName = request.OwnerName; store.StoreName = request.StoreName; store.StoreAddress = request.StoreAddress; store.Latitude = request.Latitude; store.Longitude = request.Longitude; store.OneTimeAmount = request.OneTimeAmount; store.CreditAmount = request.CreditAmount; await uow.BeginAsync(ct); try { repo.Update(store); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return ToResponse(store); } public async Task DeleteAsync(Guid id, CancellationToken ct = default) { var store = await repo.GetByIdAsync(id); if (store is null || !store.IsActive) return false; await uow.BeginAsync(ct); try { repo.Remove(store); await uow.CommitAsync(ct); } catch { await uow.RollbackAsync(ct); throw; } return true; } }