profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
BudgetCategoryService.cs 2,992 bytes
1 using App.BLL.DTO;
2 using App.BLL.Mappers;
3 using App.Domain.Contracts;
4
5 namespace App.BLL.Services;
6
7 public class BudgetCategoryService : IBudgetCategoryService
8 {
9 private readonly IAppUnitOfWork _uow;
10
11 public BudgetCategoryService(IAppUnitOfWork uow)
12 {
13 _uow = uow;
14 }
15
16 public async Task<List<BudgetCategoryBllDto>> GetByTripIdAsync(Guid tripId, Guid userId)
17 {
18 if (!await _uow.TripParticipants.IsParticipantAsync(tripId, userId))
19 return new List<BudgetCategoryBllDto>();
20 var categories = await _uow.BudgetCategories.GetByTripIdAsync(tripId);
21 return BudgetCategoryBllDtoFactory.CreateList(categories);
22 }
23
24 public async Task<List<BudgetCategoryBllDto>> GetByTripIdRawAsync(Guid tripId)
25 {
26 var categories = await _uow.BudgetCategories.GetByTripIdAsync(tripId);
27 return BudgetCategoryBllDtoFactory.CreateList(categories);
28 }
29
30 public async Task<BudgetCategoryBllDto?> GetByIdAsync(Guid id)
31 {
32 var category = await _uow.BudgetCategories.GetByIdAsync(id);
33 return category == null ? null : BudgetCategoryBllDtoFactory.Create(category);
34 }
35
36 public async Task<(BudgetCategoryBllDto? category, string? errorCode)> CreateAsync(BudgetCategoryBllDto category, Guid userId)
37 {
38 if (!await _uow.TripParticipants.IsOrganizerAsync(category.TripId, userId))
39 return (null, "forbidden");
40
41 var entity = BudgetCategoryBllDtoFactory.ToEntity(category);
42 if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
43 _uow.BudgetCategories.Add(entity);
44 await _uow.SaveChangesAsync();
45
46 var reloaded = await _uow.BudgetCategories.GetByIdAsync(entity.Id);
47 return (reloaded == null ? null : BudgetCategoryBllDtoFactory.Create(reloaded), null);
48 }
49
50 public async Task<(bool success, string? errorCode)> UpdateAsync(Guid id, BudgetCategoryBllDto incoming, Guid userId)
51 {
52 var existing = await _uow.BudgetCategories.GetByIdAsync(id);
53 if (existing == null) return (false, "notfound");
54
55 if (!await _uow.TripParticipants.IsOrganizerAsync(existing.TripId, userId))
56 return (false, "forbidden");
57
58 existing.Name = incoming.Name;
59 existing.IconName = incoming.IconName;
60 existing.PlannedAmount = incoming.PlannedAmount;
61 existing.DisplayOrder = incoming.DisplayOrder;
62
63 _uow.BudgetCategories.Update(existing);
64 await _uow.SaveChangesAsync();
65 return (true, null);
66 }
67
68 public async Task<(bool success, string? errorCode)> DeleteAsync(Guid id, Guid userId)
69 {
70 var existing = await _uow.BudgetCategories.GetByIdAsync(id);
71 if (existing == null) return (false, "notfound");
72
73 if (!await _uow.TripParticipants.IsOrganizerAsync(existing.TripId, userId))
74 return (false, "forbidden");
75
76 await _uow.BudgetCategories.RemoveAsync(id);
77 await _uow.SaveChangesAsync();
78 return (true, null);
79 }
80 }
81