| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using MUNity.Database.Context; |
| | 3 | | using MUNity.Database.General; |
| | 4 | | using MUNity.Database.Models.Conference; |
| | 5 | | using MUNity.Database.Models.Conference.Roles; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics.CodeAnalysis; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Text; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace MUNity.Database.FluentAPI; |
| | 14 | |
|
| | 15 | | public class CommitteeSpecificTools |
| | 16 | | { |
| | 17 | | private MunityContext _dbContext; |
| | 18 | |
|
| | 19 | | private string _committeeId; |
| | 20 | |
|
| | 21 | | public ConferenceDelegateRole AddSeatByCountryName(string countryName, string authTypeName = "Participant") |
| 0 | 22 | | { |
| 0 | 23 | | var committee = _dbContext.Committees |
| 0 | 24 | | .Include(n => n.Conference) |
| 0 | 25 | | .FirstOrDefault(n => n.CommitteeId == _committeeId); |
| 0 | 26 | | if (committee == null) |
| 0 | 27 | | throw new CommitteeNotFoundException($"The given Committee ({_committeeId}) was not found."); |
| | 28 | |
|
| 0 | 29 | | var country = _dbContext.Countries |
| 0 | 30 | | .FirstOrDefault(n => n.Name == countryName || |
| 0 | 31 | | n.FullName == countryName); |
| | 32 | |
|
| 0 | 33 | | if (country == null) |
| 0 | 34 | | { |
| 0 | 35 | | country = _dbContext.CountryNameTranslations.Where(n => n.TranslatedFullName == countryName || |
| 0 | 36 | | n.TranslatedName == countryName).Select(a => a.Country).FirstOrDefault(); |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | if (country == null) |
| 0 | 40 | | throw new NullReferenceException($"No country with the name {countryName} was found..."); |
| | 41 | |
|
| 0 | 42 | | var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n => |
| 0 | 43 | | n.RoleAuthName == authTypeName && n.Conference.ConferenceId == committee.Conference.ConferenceId); |
| | 44 | |
|
| 0 | 45 | | var role = new ConferenceDelegateRole() |
| 0 | 46 | | { |
| 0 | 47 | | Committee = committee, |
| 0 | 48 | | Conference = committee.Conference, |
| 0 | 49 | | ConferenceRoleAuth = participantAuth, |
| 0 | 50 | | RoleName = country.Name, |
| 0 | 51 | | DelegateCountry = country, |
| 0 | 52 | | RoleFullName = country.FullName, |
| 0 | 53 | | DelegateType = "Delegate", |
| 0 | 54 | | RoleShort = country.Iso |
| 0 | 55 | | }; |
| | 56 | |
|
| 0 | 57 | | _dbContext.Delegates.Add(role); |
| 0 | 58 | | _dbContext.SaveChanges(); |
| 0 | 59 | | return role; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Whenever you need to add multiple countries in bulk into the committee use this method instead of multiple: |
| | 64 | | /// AddSeatByCountryName-Method calls. It will perform significantly faster. |
| | 65 | | /// Not this method will use the AuthRole named: Participate. Make sure this auth exists otherwise it will assign |
| | 66 | | /// null as RoleAuth. The DelegateType will be set to Delegate |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="countryNames"></param> |
| | 69 | | /// <returns></returns> |
| | 70 | | public List<ConferenceDelegateRole> AddSeatsByCountryNames(params string[] countryNames) |
| 8 | 71 | | { |
| 8 | 72 | | var list = new List<ConferenceDelegateRole>(); |
| | 73 | |
|
| 8 | 74 | | var committee = _dbContext.Committees |
| 8 | 75 | | .Include(n => n.Conference) |
| 8 | 76 | | .FirstOrDefault(n => n.CommitteeId == _committeeId); |
| 8 | 77 | | if (committee == null) |
| 0 | 78 | | throw new CommitteeNotFoundException($"The given Committee ({_committeeId}) was not found."); |
| | 79 | |
|
| 8 | 80 | | var nameArray = countryNames.ToList(); |
| | 81 | |
|
| 8 | 82 | | var countries = _dbContext.Countries |
| 8 | 83 | | .Where(n => nameArray.Contains(n.FullName) || nameArray.Contains(n.Name)).Distinct().ToList(); |
| | 84 | |
|
| | 85 | | // TODO: Also search countries by their translation |
| | 86 | |
|
| 8 | 87 | | var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n => |
| 8 | 88 | | n.RoleAuthName == "Participate" && n.Conference.ConferenceId == committee.Conference.ConferenceId); |
| | 89 | |
|
| 592 | 90 | | foreach (var countryName in nameArray) |
| 284 | 91 | | { |
| 7656 | 92 | | var fittingCountry = countries.FirstOrDefault(n => n.Name == countryName || n.FullName == countryName); |
| | 93 | |
|
| 284 | 94 | | if (fittingCountry == null) |
| 0 | 95 | | throw new NullReferenceException($"No country found for the name: {countryName}"); |
| | 96 | |
|
| 284 | 97 | | var role = new ConferenceDelegateRole() |
| 284 | 98 | | { |
| 284 | 99 | | Committee = committee, |
| 284 | 100 | | Conference = committee.Conference, |
| 284 | 101 | | ConferenceRoleAuth = participantAuth, |
| 284 | 102 | | RoleName = fittingCountry.Name, |
| 284 | 103 | | DelegateCountry = fittingCountry, |
| 284 | 104 | | RoleFullName = fittingCountry.FullName, |
| 284 | 105 | | DelegateType = "Delegate", |
| 284 | 106 | | RoleShort = fittingCountry.Iso |
| 284 | 107 | | }; |
| | 108 | |
|
| 284 | 109 | | _dbContext.Delegates.Add(role); |
| 284 | 110 | | } |
| | 111 | | //if (country == null) |
| | 112 | | //{ |
| | 113 | | // country = _dbContext.CountryNameTranslations.Where(n => n.TranslatedFullName == countryName || |
| | 114 | | // n.TranslatedName == countryName).Select(a => a.Country).FirstOrDefault(); |
| | 115 | | //} |
| | 116 | |
|
| | 117 | |
|
| 8 | 118 | | _dbContext.SaveChanges(); |
| 8 | 119 | | return list; |
| 8 | 120 | | } |
| | 121 | |
|
| | 122 | | public ConferenceDelegateRole AddSeat(string name, int? countryId = null, string shortName = null, string subTypeNam |
| 0 | 123 | | { |
| 0 | 124 | | var committee = _dbContext.Committees.Include(n => n.Conference) |
| 0 | 125 | | .FirstOrDefault(n => n.CommitteeId == _committeeId); |
| | 126 | |
|
| 0 | 127 | | if (committee == null) |
| 0 | 128 | | throw new ArgumentException($"The committe with the given id {_committeeId} was not found!"); |
| | 129 | |
|
| 0 | 130 | | var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n => |
| 0 | 131 | | n.RoleAuthName == subTypeName && n.Conference.ConferenceId == committee.Conference.ConferenceId); |
| | 132 | |
|
| 0 | 133 | | Country country = null; |
| 0 | 134 | | if (countryId.HasValue) |
| 0 | 135 | | { |
| 0 | 136 | | country = _dbContext.Countries.FirstOrDefault(n => n.CountryId == countryId); |
| 0 | 137 | | if (country == null) |
| 0 | 138 | | throw new ArgumentException($"The given country with id: {countryId} was not found!"); |
| 0 | 139 | | } |
| | 140 | |
|
| 0 | 141 | | if (participantAuth == null) |
| 0 | 142 | | throw new ArgumentException($"The given authorization was not found!"); |
| | 143 | |
|
| 0 | 144 | | var role = new ConferenceDelegateRole() |
| 0 | 145 | | { |
| 0 | 146 | | Committee = committee, |
| 0 | 147 | | Conference = committee.Conference, |
| 0 | 148 | | ConferenceRoleAuth = participantAuth, |
| 0 | 149 | | RoleName = name, |
| 0 | 150 | | DelegateCountry = country, |
| 0 | 151 | | RoleFullName = name, |
| 0 | 152 | | DelegateType = subTypeName, |
| 0 | 153 | | RoleShort = shortName |
| 0 | 154 | | }; |
| | 155 | |
|
| 0 | 156 | | _dbContext.Delegates.Add(role); |
| 0 | 157 | | _dbContext.SaveChanges(); |
| 0 | 158 | | return role; |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public ConferenceParticipationCostRule AddCostRule(decimal cost, string name) |
| 1 | 162 | | { |
| 1 | 163 | | var committee = _dbContext.Committees.FirstOrDefault(n => n.CommitteeId == this._committeeId); |
| 1 | 164 | | var costRule = new ConferenceParticipationCostRule() |
| 1 | 165 | | { |
| 1 | 166 | | Committee = committee, |
| 1 | 167 | | Conference = null, |
| 1 | 168 | | Role = null, |
| 1 | 169 | | Delegation = null, |
| 1 | 170 | | AddPercentage = null, |
| 1 | 171 | | CostRuleTitle = name, |
| 1 | 172 | | Costs = cost, |
| 1 | 173 | | CutPercentage = null, |
| 1 | 174 | | UserMaxAge = null, |
| 1 | 175 | | UserMinAge = null |
| 1 | 176 | | }; |
| 1 | 177 | | _dbContext.ConferenceParticipationCostRules.Add(costRule); |
| 1 | 178 | | _dbContext.SaveChanges(); |
| 1 | 179 | | return costRule; |
| 1 | 180 | | } |
| | 181 | |
|
| 9 | 182 | | public CommitteeSpecificTools(MunityContext context, [NotNull] string committeeId) |
| 9 | 183 | | { |
| 9 | 184 | | this._dbContext = context; |
| 9 | 185 | | this._committeeId = committeeId; |
| 9 | 186 | | } |
| | 187 | | } |