| | 1 | | using MUNity.Base; |
| | 2 | | using MUNity.Database.Context; |
| | 3 | | using MUNity.Database.Models.Conference; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | namespace MUNity.Database.FluentAPI |
| | 11 | | { |
| | 12 | |
|
| | 13 | | public class DelegationApplicationBuilder |
| | 14 | | { |
| 1 | 15 | | private DelegationApplication application = new DelegationApplication(); |
| | 16 | |
|
| | 17 | | private MunityContext _context; |
| | 18 | |
|
| | 19 | | private string conferenceId; |
| | 20 | |
|
| | 21 | | public DelegationApplicationBuilder WithAuthor(string username) |
| 1 | 22 | | { |
| 1 | 23 | | var user = _context.Users.FirstOrDefault(n => n.UserName == username); |
| 1 | 24 | | if (user == null) |
| 0 | 25 | | throw new ArgumentException($"No user with the username {username} was found."); |
| | 26 | |
|
| 1 | 27 | | var alreadyInsideUser = application.Users.FirstOrDefault(a => a.User.UserName == username); |
| 1 | 28 | | if (alreadyInsideUser != null) |
| 0 | 29 | | { |
| 0 | 30 | | alreadyInsideUser.Status = DelegationApplicationUserEntryStatuses.Joined; |
| 0 | 31 | | alreadyInsideUser.CanWrite = true; |
| 0 | 32 | | } |
| | 33 | | else |
| 1 | 34 | | { |
| 1 | 35 | | application.Users.Add(new DelegationApplicationUserEntry() |
| 1 | 36 | | { |
| 1 | 37 | | User = user, |
| 1 | 38 | | Application = application, |
| 1 | 39 | | Status = DelegationApplicationUserEntryStatuses.Joined, |
| 1 | 40 | | CanWrite = true |
| 1 | 41 | | }); |
| 1 | 42 | | } |
| 1 | 43 | | return this; |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | public DelegationApplicationBuilder WithMember(string username) |
| 1 | 47 | | { |
| 1 | 48 | | var user = _context.Users.FirstOrDefault(n => n.UserName == username); |
| 1 | 49 | | if (user == null) |
| 0 | 50 | | throw new ArgumentException($"No user with the username {username} was found."); |
| | 51 | |
|
| 2 | 52 | | var alreadyInsideUser = application.Users.FirstOrDefault(a => a.User.UserName == username); |
| 1 | 53 | | if (alreadyInsideUser == null) |
| 1 | 54 | | { |
| 1 | 55 | | application.Users.Add(new DelegationApplicationUserEntry() |
| 1 | 56 | | { |
| 1 | 57 | | User = user, |
| 1 | 58 | | Application = application, |
| 1 | 59 | | Status = DelegationApplicationUserEntryStatuses.Invited, |
| 1 | 60 | | CanWrite = true |
| 1 | 61 | | }); |
| 1 | 62 | | } |
| 1 | 63 | | return this; |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | public DelegationApplicationBuilder WithPreferedDelegation(string delegationId) |
| 0 | 67 | | { |
| 0 | 68 | | throw new NotImplementedException($"WithPreferedDelegation is not implemented yet."); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public DelegationApplicationBuilder WithPreferedDelegationByName(string name) |
| 3 | 72 | | { |
| 3 | 73 | | var preferedDelegation = |
| 3 | 74 | | _context.Delegations.FirstOrDefault( |
| 3 | 75 | | n => n.Conference.ConferenceId == conferenceId && n.Name == name); |
| | 76 | |
|
| 3 | 77 | | if (preferedDelegation == null) |
| 0 | 78 | | throw new ArgumentException($"Unable to find a delegation with the name {name} inside the conference {co |
| | 79 | |
|
| 3 | 80 | | var isAllowed = _context.Delegates.Any(n => n.Delegation.DelegationId == preferedDelegation.DelegationId && |
| 3 | 81 | | n.ApplicationState == EApplicationStates.DelegationApplication); |
| | 82 | |
|
| 3 | 83 | | if (!isAllowed) |
| 0 | 84 | | throw new ApplicationTypeNotAllowedException($"No Role inside the Delegation {preferedDelegation.Name} f |
| | 85 | |
|
| 3 | 86 | | application.DelegationWishes.Add(new DelegationApplicationPickedDelegation() |
| 3 | 87 | | { |
| 3 | 88 | | Delegation = preferedDelegation, |
| 3 | 89 | | Application = application, |
| 3 | 90 | | Priority = (byte)application.DelegationWishes.Count |
| 3 | 91 | | }); |
| 3 | 92 | | return this; |
| 3 | 93 | | } |
| | 94 | |
|
| | 95 | | public DelegationApplicationBuilder WithFieldInput(string fieldName, string value) |
| 1 | 96 | | { |
| 1 | 97 | | var field = _context.ConferenceApplicationFields.FirstOrDefault(n => n.FieldName == fieldName && |
| 1 | 98 | | n.Forumula.FormulaType == ConferenceApplicationFormulaTypes.Delegation && |
| 1 | 99 | | n.Forumula.Options.Conference.ConferenceId == conferenceId); |
| 1 | 100 | | if (field == null) |
| 0 | 101 | | throw new ArgumentException($"Unable to find the field with the name {fieldName} for the conference {con |
| | 102 | |
|
| 1 | 103 | | application.FormulaInputs.Add(new ConferenceDelegationApplicationFieldInput() |
| 1 | 104 | | { |
| 1 | 105 | | Value = value, |
| 1 | 106 | | Application = application, |
| 1 | 107 | | Field = field |
| 1 | 108 | | }); |
| 1 | 109 | | return this; |
| 1 | 110 | | } |
| | 111 | |
|
| | 112 | | public DelegationApplicationBuilder IsOpenedToPublic() |
| 1 | 113 | | { |
| 1 | 114 | | this.application.OpenToPublic = true; |
| 1 | 115 | | return this; |
| 1 | 116 | | } |
| | 117 | |
|
| | 118 | | public DelegationApplication Submit() |
| 1 | 119 | | { |
| 1 | 120 | | _context.DelegationApplications.Add(this.application); |
| 1 | 121 | | _context.SaveChanges(); |
| 1 | 122 | | return application; |
| 1 | 123 | | } |
| | 124 | |
|
| 1 | 125 | | public DelegationApplicationBuilder(MunityContext context, string conferenceId) |
| 1 | 126 | | { |
| 1 | 127 | | this._context = context; |
| 1 | 128 | | this.conferenceId = conferenceId; |
| | 129 | |
|
| 1 | 130 | | this.application = new DelegationApplication() |
| 1 | 131 | | { |
| 1 | 132 | | DelegationWishes = new List<DelegationApplicationPickedDelegation>(), |
| 1 | 133 | | Users = new List<DelegationApplicationUserEntry>(), |
| 1 | 134 | | FormulaInputs = new List<ConferenceDelegationApplicationFieldInput>(), |
| 1 | 135 | | ApplyDate = DateTime.Now, |
| 1 | 136 | | OpenToPublic = false, |
| 1 | 137 | | Status = ApplicationStatuses.Writing, |
| 1 | 138 | | Conference = context.Conferences.Find(conferenceId) |
| 1 | 139 | | }; |
| 1 | 140 | | } |
| | 141 | | } |
| | 142 | |
|
| | 143 | | } |