TripBllDtoFactory.cs
3,458 bytes
| 1 | using App.BLL.DTO; |
|---|---|
| 2 | using App.Domain; |
| 3 | |
| 4 | namespace App.BLL.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 = entity.CreatedBy != null ? new AppUserBllDto |
| 25 | { |
| 26 | Id = entity.CreatedBy.Id, |
| 27 | FirstName = entity.CreatedBy.FirstName, |
| 28 | LastName = entity.CreatedBy.LastName, |
| 29 | Email = entity.CreatedBy.Email |
| 30 | } : null |
| 31 | }; |
| 32 | |
| 33 | if (includeParticipants && entity.Participants != null) |
| 34 | { |
| 35 | dto.Participants = entity.Participants |
| 36 | .Select(TripParticipantBllDtoFactory.Create) |
| 37 | .ToList(); |
| 38 | } |
| 39 | |
| 40 | if (includeExpenses && entity.Expenses != null) |
| 41 | { |
| 42 | dto.Expenses = entity.Expenses |
| 43 | .Select(e => ExpenseBllDtoFactory.Create(e)) |
| 44 | .ToList(); |
| 45 | } |
| 46 | |
| 47 | return dto; |
| 48 | } |
| 49 | |
| 50 | public static List<TripBllDto> CreateList(IEnumerable<Trip> entities, bool includeParticipants = false) |
| 51 | => entities.Select(t => Create(t, includeParticipants)).ToList(); |
| 52 | |
| 53 | public static Trip ToEntity(TripBllDto dto) => new() |
| 54 | { |
| 55 | Id = dto.Id, |
| 56 | Name = dto.Name, |
| 57 | Description = dto.Description, |
| 58 | Destination = dto.Destination, |
| 59 | StartDate = dto.StartDate, |
| 60 | EndDate = dto.EndDate, |
| 61 | Status = dto.Status, |
| 62 | DefaultCurrencyId = dto.DefaultCurrencyId, |
| 63 | CreatedById = dto.CreatedById |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | public static class TripParticipantBllDtoFactory |
| 68 | { |
| 69 | public static TripParticipantBllDto Create(TripParticipant entity) => new() |
| 70 | { |
| 71 | Id = entity.Id, |
| 72 | CreatedAt = entity.CreatedAt, |
| 73 | UpdatedAt = entity.UpdatedAt, |
| 74 | TripId = entity.TripId, |
| 75 | Trip = entity.Trip != null ? TripBllDtoFactory.Create(entity.Trip) : null, |
| 76 | UserId = entity.UserId, |
| 77 | User = entity.User != null ? new AppUserBllDto |
| 78 | { |
| 79 | Id = entity.User.Id, |
| 80 | FirstName = entity.User.FirstName, |
| 81 | LastName = entity.User.LastName, |
| 82 | Email = entity.User.Email |
| 83 | } : null, |
| 84 | Role = entity.Role, |
| 85 | Nickname = entity.Nickname, |
| 86 | JoinedAt = entity.JoinedAt, |
| 87 | LeftAt = entity.LeftAt, |
| 88 | IsActive = entity.IsActive |
| 89 | }; |
| 90 | |
| 91 | public static List<TripParticipantBllDto> CreateList(IEnumerable<TripParticipant> entities) |
| 92 | => entities.Select(Create).ToList(); |
| 93 | |
| 94 | public static TripParticipant ToEntity(TripParticipantBllDto dto) => new() |
| 95 | { |
| 96 | Id = dto.Id, |
| 97 | TripId = dto.TripId, |
| 98 | UserId = dto.UserId, |
| 99 | Role = dto.Role, |
| 100 | Nickname = dto.Nickname, |
| 101 | JoinedAt = dto.JoinedAt, |
| 102 | LeftAt = dto.LeftAt, |
| 103 | IsActive = dto.IsActive |
| 104 | }; |
| 105 | } |
| 106 | |