TripBllDtoFactory.cs
2,897 bytes
| 1 | using SplitApp.Modules.Trips.Domain.Entities; |
|---|---|
| 2 | using SplitApp.WebApp.Application.DTO; |
| 3 | |
| 4 | namespace SplitApp.WebApp.Application.Mappers; |
| 5 | |
| 6 | public static class TripBllDtoFactory |
| 7 | { |
| 8 | public static TripBllDto Create(Trip entity, bool includeParticipants = false, bool includeExpenses = false) |
| 9 | { |
| 10 | var dto = new TripBllDto |
| 11 | { |
| 12 | Id = entity.Id, |
| 13 | CreatedAt = entity.CreatedAt, |
| 14 | UpdatedAt = entity.UpdatedAt, |
| 15 | Name = entity.Name, |
| 16 | Description = entity.Description, |
| 17 | Destination = entity.Destination, |
| 18 | StartDate = entity.StartDate, |
| 19 | EndDate = entity.EndDate, |
| 20 | Status = entity.Status, |
| 21 | DefaultCurrencyId = entity.DefaultCurrencyId, |
| 22 | DefaultCurrency = entity.DefaultCurrency != null ? CurrencyBllDtoFactory.Create(entity.DefaultCurrency) : null, |
| 23 | CreatedById = entity.CreatedById, |
| 24 | CreatedBy = AppUserBllDtoFactory.Create(entity.CreatedBy), |
| 25 | }; |
| 26 | |
| 27 | if (includeParticipants && entity.Participants != null) |
| 28 | { |
| 29 | dto.Participants = entity.Participants |
| 30 | .Select(TripParticipantBllDtoFactory.Create) |
| 31 | .ToList(); |
| 32 | } |
| 33 | |
| 34 | return dto; |
| 35 | } |
| 36 | |
| 37 | public static List<TripBllDto> CreateList(IEnumerable<Trip> entities, bool includeParticipants = false) |
| 38 | => entities.Select(t => Create(t, includeParticipants)).ToList(); |
| 39 | |
| 40 | public static Trip ToEntity(TripBllDto dto) => new() |
| 41 | { |
| 42 | Id = dto.Id, |
| 43 | Name = dto.Name, |
| 44 | Description = dto.Description, |
| 45 | Destination = dto.Destination, |
| 46 | StartDate = dto.StartDate, |
| 47 | EndDate = dto.EndDate, |
| 48 | Status = dto.Status, |
| 49 | DefaultCurrencyId = dto.DefaultCurrencyId, |
| 50 | CreatedById = dto.CreatedById |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | public static class TripParticipantBllDtoFactory |
| 55 | { |
| 56 | public static TripParticipantBllDto Create(TripParticipant entity) => new() |
| 57 | { |
| 58 | Id = entity.Id, |
| 59 | CreatedAt = entity.CreatedAt, |
| 60 | UpdatedAt = entity.UpdatedAt, |
| 61 | TripId = entity.TripId, |
| 62 | Trip = entity.Trip != null ? TripBllDtoFactory.Create(entity.Trip) : null, |
| 63 | UserId = entity.UserId, |
| 64 | User = AppUserBllDtoFactory.Create(entity.User), |
| 65 | Role = entity.Role, |
| 66 | Nickname = entity.Nickname, |
| 67 | JoinedAt = entity.JoinedAt, |
| 68 | LeftAt = entity.LeftAt, |
| 69 | IsActive = entity.IsActive |
| 70 | }; |
| 71 | |
| 72 | public static List<TripParticipantBllDto> CreateList(IEnumerable<TripParticipant> entities) |
| 73 | => entities.Select(Create).ToList(); |
| 74 | |
| 75 | public static TripParticipant ToEntity(TripParticipantBllDto dto) => new() |
| 76 | { |
| 77 | Id = dto.Id, |
| 78 | TripId = dto.TripId, |
| 79 | UserId = dto.UserId, |
| 80 | Role = dto.Role, |
| 81 | Nickname = dto.Nickname, |
| 82 | JoinedAt = dto.JoinedAt, |
| 83 | LeftAt = dto.LeftAt, |
| 84 | IsActive = dto.IsActive |
| 85 | }; |
| 86 | } |
| 87 | |