< Summary

Class:MUNity.Database.FluentAPI.ConferenceSpecificTools
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\ConferenceSpecificTools.cs
Covered lines:147
Uncovered lines:224
Coverable lines:371
Total lines:522
Line coverage:39.6% (147 of 371)
Covered branches:19
Total branches:88
Branch coverage:21.5% (19 of 88)
Covered methods:11
Total methods:19
Method coverage:57.8% (11 of 19)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AddCommittee(...)83.33%695%
AddTeamRoleGroup(...)83.33%694.11%
GetCostOfRole(...)0%180%
GetTeamRoleCost(...)50%685.71%
MakeUserParticipateInTeamRole(...)50%687.5%
GroupRolesOfCountryIntoADelegation(...)0%140%
HasDelegationByName(...)100%10%
DelegationSizeByName(...)0%40%
GroupRolesOfCountryIntoADelegationByCommitteeIds(...)0%60%
AddDelegation(...)100%1100%
AddCostRuleForRolesOfSubType(...)50%232%
CreateDelegationApplication()50%485.71%
CostsOfDelegationByName(...)0%160%
AddBasicAuthorizations()100%1100%
ApplicationsWithFreeSlots()100%1100%
DelegationsWithOnlyAtLocationSlots(...)100%1100%
DelegationsWithOnlyAtLocationAndRoleCount(...)100%10%
DelegationsWithOnlyOnlineRoles(...)100%10%
.ctor(...)100%1100%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\ConferenceSpecificTools.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using MUNity.Base;
 3using MUNity.Database.Context;
 4using MUNity.Database.Models.Conference;
 5using MUNity.Database.Models.Conference.Roles;
 6using System;
 7using System.Collections.Generic;
 8using System.Diagnostics.CodeAnalysis;
 9using System.Linq;
 10using System.Text;
 11using System.Threading.Tasks;
 12
 13namespace MUNity.Database.FluentAPI
 14{
 15    public class ConferenceSpecificTools
 16    {
 17        private MunityContext _dbContext;
 18
 19        private string _conferenceId;
 20
 21        /// <summary>
 22        /// Creates a new Committee inside the given Conference.
 23        /// </summary>
 24        /// <param name="options"></param>
 25        /// <returns></returns>
 26        public Committee AddCommittee(Action<CommitteeOptionsBuilder> options)
 627        {
 628            var conference = _dbContext.Conferences.Find(_conferenceId);
 29
 630            if (conference == null)
 031                throw new ConferenceNotFoundException($"No conference found for {_conferenceId}");
 32
 633            var builder = new CommitteeOptionsBuilder();
 634            options(builder);
 635            builder.Committee.Conference = conference;
 36
 637            var committeeEasy = Util.IdGenerator.AsPrimaryKey(builder.Committee.CommitteeShort);
 638            builder.Committee.CommitteeId = conference.ConferenceId + "-" + committeeEasy;
 39
 640            if (builder.Committee.ChildCommittees.Count > 0)
 241            {
 1042                foreach (var committeeChildCommittee in builder.Committee.ChildCommittees)
 243                {
 244                    committeeChildCommittee.Conference = conference;
 245                }
 246            }
 647            conference.Committees.Add(builder.Committee);
 648            this._dbContext.SaveChanges();
 649            return builder.Committee;
 650        }
 51
 52        public TeamRoleGroup AddTeamRoleGroup(Action<ITeamRoleBuilder> options)
 253        {
 254            var conference = _dbContext.Conferences.Find(_conferenceId);
 55
 256            if (conference == null)
 057                throw new ConferenceNotFoundException($"No conference found for {_conferenceId}");
 58
 259            var builder = new TeamRoleGroupBuilder();
 260            options(builder);
 261            conference.TeamRoleGroups.Add(builder.Group);
 262            if (builder.Group.TeamRoles.Count > 0)
 263            {
 3264                foreach (var conferenceTeamRole in builder.Group.TeamRoles)
 1365                {
 1366                    conferenceTeamRole.Conference = conference;
 1367                }
 268            }
 269            _dbContext.SaveChanges();
 270            return builder.Group;
 271        }
 72
 73        public decimal GetCostOfRole(int roleId)
 074        {
 75
 76            // Check if the role has a Cost exception
 77            // if yes return it
 078            var roleCosts = _dbContext.ConferenceParticipationCostRules.FirstOrDefault(n =>
 079            n.Role.RoleId == roleId);
 80
 081            if (roleCosts != null && roleCosts.Costs.HasValue)
 082                return roleCosts.Costs.Value;
 83
 84            // Check of the delegation has a cost exception
 85            // if yes return it
 086            var delegationId = _dbContext.Delegates.Where(n => n.RoleId == roleId)
 087                .Select(n => n.Delegation.DelegationId)
 088                .FirstOrDefault();
 89
 090            if (delegationId != null)
 091            {
 092                var delegationCosts = _dbContext.ConferenceParticipationCostRules.FirstOrDefault(n =>
 093                n.Delegation.DelegationId == delegationId);
 094                if (delegationCosts?.Costs != null)
 095                    return delegationCosts.Costs.Value;
 096            }
 97
 98            // Check of the committee has a cost exception
 99            // if yes return it
 0100            var committeeId = _dbContext.Delegates.Where(n => n.RoleId == roleId)
 0101                .Select(n => n.Committee.CommitteeId)
 0102                .FirstOrDefault();
 0103            if (committeeId != null)
 0104            {
 0105                var committeeCosts = _dbContext.ConferenceParticipationCostRules.FirstOrDefault(n =>
 0106                n.Committee.CommitteeId == committeeId);
 0107                if (committeeCosts?.Costs != null)
 0108                    return committeeCosts.Costs.Value;
 0109            }
 110
 111            // Check for the cost of the conference
 112            // and return it.
 0113            var conferenceId = _dbContext.Delegates
 0114                .AsNoTracking()
 0115                .Where(n => n.RoleId == roleId)
 0116                .Select(n => n.Conference.ConferenceId).FirstOrDefault();
 117
 0118            if (conferenceId == null)
 0119                throw new ConferenceNotFoundException($"No Conference for the role {roleId} found");
 120
 0121            var conferenceCost = _dbContext.Conferences.Find(conferenceId).GeneralParticipationCost;
 0122            return conferenceCost;
 0123        }
 124
 125        public decimal GetTeamRoleCost(int teamRoleId)
 1126        {
 127            // Check if the role has a Cost exception
 128            // if yes return it
 1129            var roleCosts = _dbContext.ConferenceParticipationCostRules.FirstOrDefault(n =>
 1130            n.Role.RoleId == teamRoleId);
 131
 1132            if (roleCosts != null && roleCosts.Costs.HasValue)
 0133                return roleCosts.Costs.Value;
 134
 1135            var conferenceId = _dbContext.ConferenceTeamRoles
 1136                .AsNoTracking()
 1137                .Where(n => n.RoleId == teamRoleId)
 1138                .Select(n => n.Conference.ConferenceId).FirstOrDefault();
 139
 1140            if (conferenceId == null)
 0141                throw new ConferenceNotFoundException($"No Conference for the role {teamRoleId} found");
 142
 1143            var conferenceCost = _dbContext.Conferences.Find(conferenceId).GeneralParticipationCost;
 1144            return conferenceCost;
 1145        }
 146
 147        public Participation MakeUserParticipateInTeamRole(string username, string roleName)
 1148        {
 1149            var user = _dbContext.Users.FirstOrDefault(n => n.NormalizedUserName == username.ToUpper());
 150
 1151            if (user == null)
 0152                throw new UserNotFoundException($"No user found for {username}");
 153
 1154            var roles = _dbContext.ConferenceTeamRoles.Where(n => n.Conference.ConferenceId == _conferenceId &&
 1155            n.RoleName == roleName).ToList();
 1156            if (roles.Count == 0)
 0157                throw new ConferenceRoleNotFoundException($"No role with the name {roleName} was found fpr the Conferenc
 1158            if (roles.Count > 1)
 0159                throw new NotUnambiguousRoleNameException($"The role name '{roleName}' is not unambiguous for the confer
 1160            var role = roles.First();
 161
 1162            var participation = new Participation()
 1163            {
 1164                Cost = GetTeamRoleCost(role.RoleId),
 1165                IsMainRole = true,
 1166                Paid = 0,
 1167                ParticipationSecret = "TODO",
 1168                Role = role,
 1169                User = user
 1170            };
 1171            _dbContext.Participations.Add(participation);
 1172            _dbContext.SaveChanges();
 1173            return participation;
 1174        }
 175
 176        /// <summary>
 177        ///
 178        /// </summary>
 179        /// <param name="countryName"></param>
 180        /// <param name="delegationName">if left to null the countryname will be used</param>
 181        /// <param name="delegationFullName">if left null the wird Delegation + delegationName will be used.</param>
 182        /// <param name="delegationShortName">if left to null the country ISO will be used.</param>
 183        /// <returns></returns>
 184        public Delegation GroupRolesOfCountryIntoADelegation(string countryName,
 185            string delegationName = null, string delegationFullName = null, string delegationShortName = null)
 0186        {
 0187            var conference = _dbContext.Conferences.FirstOrDefault(n => n.ConferenceId == _conferenceId);
 0188            if (conference == null)
 0189                throw new NullReferenceException($"The conference with id '{_conferenceId}' was not found!");
 190
 191
 0192            if (delegationFullName == null)
 0193                delegationFullName = "Delegation " + delegationName;
 194
 0195            var country = _dbContext.Countries.FirstOrDefault(n => n.Name == countryName || n.FullName == countryName);
 0196            if (country == null)
 0197                throw new CountryNotFoundException($"Country {countryName} not found...");
 198
 0199            if (delegationName == null)
 0200                delegationName = country.Name;
 201
 0202            if (_dbContext.Delegations.Any(n => n.Name == delegationName && n.Conference.ConferenceId == _conferenceId))
 0203                throw new NameTakenException($"The Delegationname {delegationName} is already taken!");
 204
 205
 0206                if (delegationFullName == null)
 0207                delegationFullName = $"Delegation {delegationName}";
 208
 0209            if (delegationShortName == null)
 0210                delegationShortName = country.Iso;
 211
 0212            var delegation = new Delegation()
 0213            {
 0214                Conference = conference,
 0215                Name = delegationName,
 0216                Roles = _dbContext.Delegates
 0217                    .Where(n => n.DelegateCountry.CountryId == country.CountryId &&
 0218                    n.Conference.ConferenceId == _conferenceId).ToList(),
 0219                FullName = delegationFullName,
 0220                DelegationShort = delegationShortName
 0221            };
 0222            _dbContext.Delegations.Add(delegation);
 0223            _dbContext.SaveChanges();
 0224            return delegation;
 0225        }
 226
 227        public bool HasDelegationByName(string name)
 0228        {
 0229            return _dbContext.Delegations.Any(n => n.Conference.ConferenceId == _conferenceId &&
 0230            n.Name == name || n.FullName == name);
 0231        }
 232
 233        public int DelegationSizeByName(string name)
 0234        {
 0235            var size = _dbContext.Delegates.Count(n => (n.Delegation.Name == name || n.Delegation.FullName == name &&
 0236                n.Conference.ConferenceId == _conferenceId));
 237
 0238            if (size == 0)
 0239                size = _dbContext.Delegations.Any(n => n.Name == name || n.FullName == name) ? 0 : -1;
 0240            return size;
 0241        }
 242
 243        public Delegation GroupRolesOfCountryIntoADelegationByCommitteeIds(string countryName, params string[] committee
 0244        {
 0245            var country = _dbContext.Countries.AsNoTracking().FirstOrDefault(n => n.Name == countryName || n.FullName ==
 246
 0247            if (country == null)
 0248                throw new CountryNotFoundException($"Country with the name {countryName} was not found.");
 249
 0250            var delegation = new Delegation()
 0251            {
 0252                Conference = _dbContext.Conferences.Find(_conferenceId),
 0253                DelegationShort = country.Iso,
 0254                FullName = "Delegation " + country.Name,
 0255                Name = countryName,
 0256                Roles = new List<ConferenceDelegateRole>()
 0257            };
 258
 0259            foreach(var committee in committees)
 0260            {
 0261                var role = _dbContext.Delegates.FirstOrDefault(n => n.Conference.ConferenceId == _conferenceId &&
 0262                n.Committee.CommitteeId == committee && n.DelegateCountry.CountryId == country.CountryId);
 263
 0264                if (role != null)
 0265                    delegation.Roles.Add(role);
 266                else
 0267                    throw new ConferenceRoleNotFoundException($"{country.Name} ({country.CountryId}) seems to not be rep
 0268            }
 269
 0270            _dbContext.Delegations.Add(delegation);
 0271            _dbContext.SaveChanges();
 0272            return delegation;
 0273        }
 274
 275        public Delegation AddDelegation(Action<DelegationBuilder> options)
 35276        {
 35277            DelegationBuilder builder = new DelegationBuilder(_dbContext, _conferenceId);
 35278            options(builder);
 35279            var delegation = builder.Delegation;
 35280            return delegation;
 35281        }
 282
 283        public List<ConferenceParticipationCostRule> AddCostRuleForRolesOfSubType(string subTypeName, decimal costs, str
 1284        {
 1285            var roles = this._dbContext.Delegates.Where(n => n.DelegateType == subTypeName &&
 1286            n.Conference.ConferenceId == _conferenceId);
 287
 1288            var list = new List<ConferenceParticipationCostRule>();
 289
 3290            foreach (var role in roles)
 0291            {
 0292                var costRule = new ConferenceParticipationCostRule()
 0293                {
 0294                    Committee = null,
 0295                    Conference = null,
 0296                    Role = role,
 0297                    Delegation = null,
 0298                    AddPercentage = null,
 0299                    CostRuleTitle = name,
 0300                    Costs = costs,
 0301                    CutPercentage = null,
 0302                    UserMaxAge = null,
 0303                    UserMinAge = null
 0304                };
 305
 0306                _dbContext.ConferenceParticipationCostRules.Add(costRule);
 0307                list.Add(costRule);
 0308            }
 309
 1310            _dbContext.SaveChanges();
 1311            return list;
 1312        }
 313
 314        public DelegationApplicationBuilder CreateDelegationApplication()
 1315        {
 1316            var options = _dbContext.ConferenceApplicationOptions.FirstOrDefault(n => n.Conference.ConferenceId == _conf
 317
 1318            if (options == null || options.AllowDelegationApplication == false)
 0319                throw new ApplicationTypeNotAllowedException($"The Application type DelegationApplication is not allowed
 320
 1321            var builder = new DelegationApplicationBuilder(_dbContext, _conferenceId);
 1322            return builder;
 1323        }
 324
 325        public DelegationCostResult CostsOfDelegationByName(string delegationName)
 0326        {
 0327            var result = new DelegationCostResult();
 328
 0329            var delegationId = _dbContext.Delegations.Where(n =>
 0330            n.Conference.ConferenceId == _conferenceId &&
 0331            n.Name == delegationName)
 0332                .Select(n => n.DelegationId).FirstOrDefault();
 333
 0334            if (delegationId == null)
 0335                throw new DelegationNotFoundException($"No Delegation with the Name {delegationName} found for conferenc
 336
 0337            var delegationWithRoles = _dbContext.Delegations
 0338                .Include(n => n.Roles)
 0339                .Select(n => new
 0340                {
 0341                    DelegationId = n.DelegationId,
 0342                    ConferenceId = n.Conference.ConferenceId,
 0343                    Roles = n.Roles.Select(a => new
 0344                    {
 0345                        RoleId = a.RoleId,
 0346                        RoleName = a.RoleName,
 0347                        CommitteeId = a.Committee.CommitteeId,
 0348                    })
 0349                })
 0350                .FirstOrDefault(n => n.DelegationId == delegationId);
 351
 352
 0353            if (delegationWithRoles == null)
 0354                throw new NullReferenceException($"The Delegation with Id {delegationName} was not found!");
 355
 0356            decimal priceForConference =
 0357                _dbContext.Conferences.Where(n => n.ConferenceId == delegationWithRoles.ConferenceId)
 0358                    .Select(a => a.GeneralParticipationCost)
 0359                    .FirstOrDefault();
 360
 0361            decimal? priceForDelegation =
 0362                _dbContext.ConferenceParticipationCostRules.Where(n => n.Delegation.DelegationId == delegationId)
 0363                    .Select(n => n.Costs)
 0364                    .FirstOrDefault();
 365
 0366            foreach (var role in delegationWithRoles.Roles)
 0367            {
 368                // Check for role price
 0369                decimal? rolePrice = _dbContext.ConferenceParticipationCostRules
 0370                    .Where(n => n.Role.RoleId == role.RoleId)
 0371                    .Select(n => n.Costs)
 0372                    .FirstOrDefault();
 373
 374
 375
 376                // Check for Committee Price
 0377                if (rolePrice == null && role.CommitteeId != null)
 0378                {
 379
 0380                    if (priceForDelegation != null)
 0381                    {
 382                        // Prio 2: Price for the delegation
 0383                        result.Costs.Add(new DelegationCostPoint()
 0384                        {
 0385                            CommitteeName = _dbContext.Delegates.Where(n => n.RoleId == role.RoleId).Select(n => n.Commi
 0386                            Cost = priceForDelegation.Value,
 0387                            RoleId = role.RoleId,
 0388                            RoleName = role.RoleName
 0389                        });
 0390                    }
 391                    else
 0392                    {
 393                        // Prio 3: Price for the committee
 0394                        decimal? committeePrice = _dbContext.ConferenceParticipationCostRules
 0395                            .Where(n => n.Committee.CommitteeId == role.CommitteeId)
 0396                            .Select(n => n.Costs)
 0397                            .FirstOrDefault();
 398
 0399                        if (committeePrice != null)
 0400                        {
 0401                            result.Costs.Add(new DelegationCostPoint()
 0402                            {
 0403                                CommitteeName = _dbContext.Delegates.Where(n => n.RoleId == role.RoleId).Select(n => n.C
 0404                                Cost = committeePrice.Value,
 0405                                RoleId = role.RoleId,
 0406                                RoleName = role.RoleName
 0407                            });
 0408                        }
 409                        else
 0410                        {
 0411                            result.Costs.Add(new DelegationCostPoint()
 0412                            {
 0413                                CommitteeName = _dbContext.Delegates.Where(n => n.RoleId == role.RoleId).Select(n => n.C
 0414                                Cost = priceForConference,
 0415                                RoleId = role.RoleId,
 0416                                RoleName = role.RoleName
 0417                            });
 0418                        }
 0419                    }
 0420                }
 0421                else if (rolePrice != null)
 0422                {
 423                    // Prio 1: Role Price (Highest)
 0424                    result.Costs.Add(new DelegationCostPoint()
 0425                    {
 0426                        CommitteeName = _dbContext.Delegates.Where(n => n.RoleId == role.RoleId).Select(n => n.Committee
 0427                        Cost = rolePrice.Value,
 0428                        RoleId = role.RoleId,
 0429                        RoleName = role.RoleName
 0430                    });
 0431                }
 432
 0433            }
 434
 0435            return result;
 0436        }
 437
 438        public int AddBasicAuthorizations()
 1439        {
 1440            var conference = this._dbContext.Conferences.FirstOrDefault(n => n.ConferenceId == _conferenceId);
 1441            var ownerAuth = new ConferenceRoleAuth()
 1442            {
 1443                Conference = conference,
 1444                CanEditConferenceSettings = true,
 1445                CanEditParticipations = true,
 1446                CanSeeApplications = true,
 1447                PowerLevel = 1,
 1448                RoleAuthName = "Project-Owner"
 1449            };
 1450            _dbContext.ConferenceRoleAuthorizations.Add(ownerAuth);
 451
 1452            var participantControllingTeamAuth = new ConferenceRoleAuth()
 1453            {
 1454                Conference = conference,
 1455                CanEditConferenceSettings = false,
 1456                CanEditParticipations = true,
 1457                CanSeeApplications = true,
 1458                PowerLevel = 2,
 1459                RoleAuthName = "Team (Participant Management)"
 1460            };
 1461            _dbContext.ConferenceRoleAuthorizations.Add(participantControllingTeamAuth);
 462
 1463            var teamAuth = new ConferenceRoleAuth()
 1464            {
 1465                Conference = conference,
 1466                CanEditConferenceSettings = false,
 1467                CanEditParticipations = false,
 1468                CanSeeApplications = true,
 1469                RoleAuthName = "Team (Basic)",
 1470                PowerLevel = 3,
 1471            };
 1472            _dbContext.ConferenceRoleAuthorizations.Add(teamAuth);
 473
 1474            var participantAuth = new ConferenceRoleAuth()
 1475            {
 1476                Conference = conference,
 1477                CanEditConferenceSettings = false,
 1478                RoleAuthName = "Participant",
 1479                PowerLevel = 4,
 1480                CanSeeApplications = false,
 1481                CanEditParticipations = false
 1482            };
 1483            _dbContext.ConferenceRoleAuthorizations.Add(participantAuth);
 484
 1485            return _dbContext.SaveChanges();
 1486        }
 487
 488        public IQueryable<DelegationApplication> ApplicationsWithFreeSlots()
 1489        {
 1490            return _dbContext.DelegationApplications.Where(n => n.Conference.ConferenceId == _conferenceId &&
 1491                n.OpenToPublic &&
 1492                (n.Users.Count(n => n.Status == DelegationApplicationUserEntryStatuses.Joined || n.Status == DelegationA
 1493        }
 494
 495        public IQueryable<Delegation> DelegationsWithOnlyAtLocationSlots(int minRolesCount = 0)
 1496        {
 1497            return _dbContext.Delegations.Where(n => n.Conference.ConferenceId == _conferenceId &&
 1498               n.Roles.Count >= minRolesCount &&
 1499               n.Roles.All(a => a.Committee.CommitteeType == CommitteeTypes.AtLocation));
 1500        }
 501
 502        public IQueryable<Delegation> DelegationsWithOnlyAtLocationAndRoleCount(int roleCount)
 0503        {
 0504            return _dbContext.Delegations.Where(n => n.Conference.ConferenceId == _conferenceId &&
 0505               n.Roles.Count == roleCount &&
 0506               n.Roles.All(a => a.Committee.CommitteeType == CommitteeTypes.AtLocation));
 0507        }
 508
 509        public IQueryable<Delegation> DelegationsWithOnlyOnlineRoles(int minRolesCount = 0)
 0510        {
 0511            return _dbContext.Delegations.Where(n => n.Conference.ConferenceId == _conferenceId &&
 0512               n.Roles.Count >= minRolesCount &&
 0513               n.Roles.All(a => a.Committee.CommitteeType == CommitteeTypes.Online));
 0514        }
 515
 49516        public ConferenceSpecificTools(MunityContext context, [NotNull]string conferenceId)
 49517        {
 49518            this._dbContext = context;
 49519            this._conferenceId = conferenceId;
 49520        }
 521    }
 522}