profileShare

rasmusjy / splitapp-backend-microservices

Read-only snapshot

No repository description.

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