profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

main default branch 429 files Expires Sep 13, 2026, 9:06 AM
SplitPresetService.cs 4,145 bytes
1 using App.BLL.DTO;
2 using App.BLL.Mappers;
3 using App.Domain;
4 using App.Domain.Contracts;
5
6 namespace App.BLL.Services;
7
8 public class SplitPresetService : ISplitPresetService
9 {
10 private readonly IAppUnitOfWork _uow;
11
12 public SplitPresetService(IAppUnitOfWork uow)
13 {
14 _uow = uow;
15 }
16
17 public async Task<List<SplitPresetBllDto>> GetByTripIdAsync(Guid tripId, Guid userId)
18 {
19 if (!await _uow.TripParticipants.IsParticipantAsync(tripId, userId))
20 return new List<SplitPresetBllDto>();
21 var presets = await _uow.SplitPresets.GetByTripIdAsync(tripId);
22 return SplitPresetBllDtoFactory.CreateList(presets, includeMembers: true);
23 }
24
25 public async Task<SplitPresetBllDto?> GetByIdAsync(Guid id, Guid userId)
26 {
27 var preset = await _uow.SplitPresets.GetByIdAsync(id);
28 if (preset == null) return null;
29 if (!await _uow.TripParticipants.IsParticipantAsync(preset.TripId, userId)) return null;
30 return SplitPresetBllDtoFactory.Create(preset, includeMembers: true);
31 }
32
33 public async Task<(SplitPresetBllDto? preset, string? errorCode)> CreateAsync(SplitPresetBllDto preset,
34 List<(Guid UserId, decimal? ShareWeight, decimal? Percentage)> members, Guid userId)
35 {
36 if (!await _uow.TripParticipants.IsParticipantAsync(preset.TripId, userId))
37 return (null, "forbidden");
38
39 var entity = SplitPresetBllDtoFactory.ToEntity(preset);
40 if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
41 entity.CreatedById = userId;
42 _uow.SplitPresets.Add(entity);
43
44 var memberRepo = _uow.GetRepository<SplitPresetMember>();
45 foreach (var m in members)
46 {
47 memberRepo.Add(new SplitPresetMember
48 {
49 SplitPresetId = entity.Id,
50 UserId = m.UserId,
51 ShareWeight = m.ShareWeight,
52 Percentage = m.Percentage
53 });
54 }
55
56 await _uow.SaveChangesAsync();
57
58 var reloaded = await _uow.SplitPresets.GetByIdAsync(entity.Id);
59 return (reloaded == null ? null : SplitPresetBllDtoFactory.Create(reloaded, includeMembers: true), null);
60 }
61
62 public async Task<(bool success, string? errorCode)> UpdateAsync(Guid id, string name, ESplitMethod splitMethod,
63 List<(Guid UserId, decimal? ShareWeight, decimal? Percentage)> members, Guid userId)
64 {
65 var preset = await _uow.SplitPresets.GetByIdAsync(id);
66 if (preset == null) return (false, "notfound");
67
68 if (!await _uow.TripParticipants.IsParticipantAsync(preset.TripId, userId))
69 return (false, "forbidden");
70
71 preset.Name = name;
72 preset.SplitMethod = splitMethod;
73 _uow.SplitPresets.Update(preset);
74
75 var memberRepo = _uow.GetRepository<SplitPresetMember>();
76 if (preset.Members != null)
77 {
78 foreach (var member in preset.Members.ToList())
79 {
80 await memberRepo.RemoveAsync(member.Id);
81 }
82 }
83
84 foreach (var m in members)
85 {
86 memberRepo.Add(new SplitPresetMember
87 {
88 SplitPresetId = preset.Id,
89 UserId = m.UserId,
90 ShareWeight = m.ShareWeight,
91 Percentage = m.Percentage
92 });
93 }
94
95 await _uow.SaveChangesAsync();
96 return (true, null);
97 }
98
99 public async Task<(bool success, string? errorCode)> DeleteAsync(Guid id, Guid userId)
100 {
101 var preset = await _uow.SplitPresets.GetByIdAsync(id);
102 if (preset == null) return (false, "notfound");
103
104 if (!await _uow.TripParticipants.IsParticipantAsync(preset.TripId, userId))
105 return (false, "forbidden");
106
107 var memberRepo = _uow.GetRepository<SplitPresetMember>();
108 if (preset.Members != null)
109 {
110 foreach (var member in preset.Members.ToList())
111 {
112 await memberRepo.RemoveAsync(member.Id);
113 }
114 }
115
116 await _uow.SplitPresets.RemoveAsync(id);
117 await _uow.SaveChangesAsync();
118 return (true, null);
119 }
120 }
121