< Summary

Class:MUNity.Database.FluentAPI.CommitteeSpecificTools
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\CommitteeSpecificTools.cs
Covered lines:55
Uncovered lines:66
Coverable lines:121
Total lines:187
Line coverage:45.4% (55 of 121)
Covered branches:6
Total branches:22
Branch coverage:27.2% (6 of 22)
Covered methods:3
Total methods:5
Method coverage:60% (3 of 5)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AddSeatByCountryName(...)0%60%
AddSeatsByCountryNames(...)75%893.93%
AddSeat(...)0%80%
AddCostRule(...)100%1100%
.ctor(...)100%1100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using MUNity.Database.Context;
 3using MUNity.Database.General;
 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
 15public class CommitteeSpecificTools
 16{
 17    private MunityContext _dbContext;
 18
 19    private string _committeeId;
 20
 21    public ConferenceDelegateRole AddSeatByCountryName(string countryName, string authTypeName = "Participant")
 022    {
 023        var committee = _dbContext.Committees
 024            .Include(n => n.Conference)
 025            .FirstOrDefault(n => n.CommitteeId == _committeeId);
 026        if (committee == null)
 027            throw new CommitteeNotFoundException($"The given Committee ({_committeeId}) was not found.");
 28
 029        var country = _dbContext.Countries
 030            .FirstOrDefault(n => n.Name == countryName ||
 031            n.FullName == countryName);
 32
 033        if (country == null)
 034        {
 035            country = _dbContext.CountryNameTranslations.Where(n => n.TranslatedFullName == countryName ||
 036                n.TranslatedName == countryName).Select(a => a.Country).FirstOrDefault();
 037        }
 38
 039        if (country == null)
 040            throw new NullReferenceException($"No country with the name {countryName} was found...");
 41
 042        var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n =>
 043            n.RoleAuthName == authTypeName && n.Conference.ConferenceId == committee.Conference.ConferenceId);
 44
 045        var role = new ConferenceDelegateRole()
 046        {
 047            Committee = committee,
 048            Conference = committee.Conference,
 049            ConferenceRoleAuth = participantAuth,
 050            RoleName = country.Name,
 051            DelegateCountry = country,
 052            RoleFullName = country.FullName,
 053            DelegateType = "Delegate",
 054            RoleShort = country.Iso
 055        };
 56
 057        _dbContext.Delegates.Add(role);
 058        _dbContext.SaveChanges();
 059        return role;
 060    }
 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)
 871    {
 872        var list = new List<ConferenceDelegateRole>();
 73
 874        var committee = _dbContext.Committees
 875            .Include(n => n.Conference)
 876            .FirstOrDefault(n => n.CommitteeId == _committeeId);
 877        if (committee == null)
 078            throw new CommitteeNotFoundException($"The given Committee ({_committeeId}) was not found.");
 79
 880        var nameArray = countryNames.ToList();
 81
 882        var countries = _dbContext.Countries
 883            .Where(n => nameArray.Contains(n.FullName) || nameArray.Contains(n.Name)).Distinct().ToList();
 84
 85        // TODO: Also search countries by their translation
 86
 887        var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n =>
 888            n.RoleAuthName == "Participate" && n.Conference.ConferenceId == committee.Conference.ConferenceId);
 89
 59290        foreach (var countryName in nameArray)
 28491        {
 765692            var fittingCountry = countries.FirstOrDefault(n => n.Name == countryName || n.FullName == countryName);
 93
 28494            if (fittingCountry == null)
 095                throw new NullReferenceException($"No country found for the name: {countryName}");
 96
 28497            var role = new ConferenceDelegateRole()
 28498            {
 28499                Committee = committee,
 284100                Conference = committee.Conference,
 284101                ConferenceRoleAuth = participantAuth,
 284102                RoleName = fittingCountry.Name,
 284103                DelegateCountry = fittingCountry,
 284104                RoleFullName = fittingCountry.FullName,
 284105                DelegateType = "Delegate",
 284106                RoleShort = fittingCountry.Iso
 284107            };
 108
 284109            _dbContext.Delegates.Add(role);
 284110        }
 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
 8118        _dbContext.SaveChanges();
 8119        return list;
 8120    }
 121
 122    public ConferenceDelegateRole AddSeat(string name, int? countryId = null, string shortName = null, string subTypeNam
 0123    {
 0124        var committee = _dbContext.Committees.Include(n => n.Conference)
 0125            .FirstOrDefault(n => n.CommitteeId == _committeeId);
 126
 0127        if (committee == null)
 0128            throw new ArgumentException($"The committe with the given id {_committeeId} was not found!");
 129
 0130        var participantAuth = _dbContext.ConferenceRoleAuthorizations.FirstOrDefault(n =>
 0131            n.RoleAuthName == subTypeName && n.Conference.ConferenceId == committee.Conference.ConferenceId);
 132
 0133        Country country = null;
 0134        if (countryId.HasValue)
 0135        {
 0136            country = _dbContext.Countries.FirstOrDefault(n => n.CountryId == countryId);
 0137            if (country == null)
 0138                throw new ArgumentException($"The given country with id: {countryId} was not found!");
 0139        }
 140
 0141        if (participantAuth == null)
 0142            throw new ArgumentException($"The given authorization was not found!");
 143
 0144        var role = new ConferenceDelegateRole()
 0145        {
 0146            Committee = committee,
 0147            Conference = committee.Conference,
 0148            ConferenceRoleAuth = participantAuth,
 0149            RoleName = name,
 0150            DelegateCountry = country,
 0151            RoleFullName = name,
 0152            DelegateType = subTypeName,
 0153            RoleShort = shortName
 0154        };
 155
 0156        _dbContext.Delegates.Add(role);
 0157        _dbContext.SaveChanges();
 0158        return role;
 0159    }
 160
 161    public ConferenceParticipationCostRule AddCostRule(decimal cost, string name)
 1162    {
 1163        var committee = _dbContext.Committees.FirstOrDefault(n => n.CommitteeId == this._committeeId);
 1164        var costRule = new ConferenceParticipationCostRule()
 1165        {
 1166            Committee = committee,
 1167            Conference = null,
 1168            Role = null,
 1169            Delegation = null,
 1170            AddPercentage = null,
 1171            CostRuleTitle = name,
 1172            Costs = cost,
 1173            CutPercentage = null,
 1174            UserMaxAge = null,
 1175            UserMinAge = null
 1176        };
 1177        _dbContext.ConferenceParticipationCostRules.Add(costRule);
 1178        _dbContext.SaveChanges();
 1179        return costRule;
 1180    }
 181
 9182    public CommitteeSpecificTools(MunityContext context, [NotNull] string committeeId)
 9183    {
 9184        this._dbContext = context;
 9185        this._committeeId = committeeId;
 9186    }
 187}