PollBllDtoFactory.cs
2,625 bytes
| 1 | using App.BLL.DTO; |
|---|---|
| 2 | using App.Domain; |
| 3 | |
| 4 | namespace App.BLL.Mappers; |
| 5 | |
| 6 | public static class PollBllDtoFactory |
| 7 | { |
| 8 | public static TripPollBllDto Create(TripPoll entity, bool includeOptions = false) => new() |
| 9 | { |
| 10 | Id = entity.Id, |
| 11 | CreatedAt = entity.CreatedAt, |
| 12 | UpdatedAt = entity.UpdatedAt, |
| 13 | TripId = entity.TripId, |
| 14 | Trip = entity.Trip != null ? TripBllDtoFactory.Create(entity.Trip) : null, |
| 15 | CreatedByUserId = entity.CreatedByUserId, |
| 16 | CreatedByUser = entity.CreatedByUser != null ? new AppUserBllDto |
| 17 | { |
| 18 | Id = entity.CreatedByUser.Id, |
| 19 | FirstName = entity.CreatedByUser.FirstName, |
| 20 | LastName = entity.CreatedByUser.LastName, |
| 21 | Email = entity.CreatedByUser.Email |
| 22 | } : null, |
| 23 | Question = entity.Question, |
| 24 | AllowMultipleVotes = entity.AllowMultipleVotes, |
| 25 | IsAnonymous = entity.IsAnonymous, |
| 26 | ClosedAt = entity.ClosedAt, |
| 27 | Options = includeOptions && entity.Options != null |
| 28 | ? entity.Options.OrderBy(o => o.DisplayOrder).Select(PollOptionBllDtoFactory.Create).ToList() |
| 29 | : null |
| 30 | }; |
| 31 | |
| 32 | public static List<TripPollBllDto> CreateList(IEnumerable<TripPoll> entities, bool includeOptions = false) |
| 33 | => entities.Select(p => Create(p, includeOptions)).ToList(); |
| 34 | |
| 35 | public static TripPoll ToEntity(TripPollBllDto dto) => new() |
| 36 | { |
| 37 | Id = dto.Id, |
| 38 | TripId = dto.TripId, |
| 39 | CreatedByUserId = dto.CreatedByUserId, |
| 40 | Question = dto.Question, |
| 41 | AllowMultipleVotes = dto.AllowMultipleVotes, |
| 42 | IsAnonymous = dto.IsAnonymous, |
| 43 | ClosedAt = dto.ClosedAt |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | public static class PollOptionBllDtoFactory |
| 48 | { |
| 49 | public static TripPollOptionBllDto Create(TripPollOption entity) => new() |
| 50 | { |
| 51 | Id = entity.Id, |
| 52 | CreatedAt = entity.CreatedAt, |
| 53 | UpdatedAt = entity.UpdatedAt, |
| 54 | PollId = entity.PollId, |
| 55 | Text = entity.Text, |
| 56 | DisplayOrder = entity.DisplayOrder, |
| 57 | VoteCount = entity.Votes?.Count ?? 0, |
| 58 | VoterUserIds = entity.Votes?.Select(v => v.UserId).ToList() ?? new List<Guid>(), |
| 59 | Voters = entity.Votes?.Where(v => v.User != null).Select(v => new AppUserBllDto |
| 60 | { |
| 61 | Id = v.User!.Id, |
| 62 | FirstName = v.User.FirstName, |
| 63 | LastName = v.User.LastName, |
| 64 | Email = v.User.Email |
| 65 | }).ToList() ?? new List<AppUserBllDto>() |
| 66 | }; |
| 67 | |
| 68 | public static TripPollOption ToEntity(TripPollOptionBllDto dto) => new() |
| 69 | { |
| 70 | Id = dto.Id, |
| 71 | PollId = dto.PollId, |
| 72 | Text = dto.Text, |
| 73 | DisplayOrder = dto.DisplayOrder |
| 74 | }; |
| 75 | } |
| 76 | |