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