< Summary

Class:MUNity.Database.FluentAPI.DelegationBuilderCommitteeSelector
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\DelegationBuilder.cs
Covered lines:43
Uncovered lines:14
Coverable lines:57
Total lines:182
Line coverage:75.4% (43 of 57)
Covered branches:6
Total branches:16
Branch coverage:37.5% (6 of 16)
Covered methods:5
Total methods:5
Method coverage:100% (5 of 5)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Delegation()100%1100%
InsideCommittee(...)37.5%865%
InsideCommitteeByShort(...)37.5%866.66%
InsideAnyCommittee()100%1100%
.ctor(...)100%1100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using MUNity.Database.Context;
 3using MUNity.Database.Models.Conference;
 4using MUNity.Database.Models.Conference.Roles;
 5using System;
 6using System.Collections.Generic;
 7using System.Linq;
 8using System.Text;
 9using System.Threading.Tasks;
 10
 11namespace MUNity.Database.FluentAPI
 12{
 13    public class DelegationBuilder
 14    {
 15        internal Delegation Delegation { get; private set; }
 16
 17        private MunityContext _dbContext;
 18
 19        private string _conferenceId;
 20
 21        public DelegationBuilder WithName(string name)
 22        {
 23            // Handle easyId
 24
 25            Delegation.Name = name;
 26            //SetIdAsEasyIdByName();
 27            return this;
 28        }
 29
 30        private void SetIdAsEasyIdByName()
 31        {
 32            if (!string.IsNullOrWhiteSpace(Delegation.DelegationId)) return;
 33
 34            var easyNameDelegation = Util.IdGenerator.AsPrimaryKey(Delegation.Name);
 35            var easyId = _conferenceId + "-" + easyNameDelegation;
 36            if (_dbContext.Delegations.All(n => n.DelegationId != easyId) && _dbContext.ChangeTracker.Entries<Delegation
 37            {
 38                Console.WriteLine($"Assign easy id to Delegation {Delegation.Name}: {easyId}");
 39                Delegation.DelegationId = easyId;
 40            }
 41            else
 42                Delegation.DelegationId = Guid.NewGuid().ToString();
 43        }
 44
 45        public DelegationBuilderCommitteeSelector WithCountry(string countryName)
 46        {
 47            short countryId = _dbContext.Countries.AsNoTracking()
 48                .Where(n => n.Name == countryName || n.FullName == countryName)
 49                .Select(n => n.CountryId)
 50                .FirstOrDefault();
 51            if (countryId == 0)
 52                throw new CountryNotFoundException($"No country with the name {countryName} found!");
 53
 54            return new DelegationBuilderCommitteeSelector(_dbContext, _conferenceId, countryId, Delegation);
 55        }
 56
 57        public DelegationBuilder(MunityContext context, string conferenceId)
 58        {
 59            this._dbContext = context;
 60            this._conferenceId = conferenceId;
 61            var conference = _dbContext.Conferences.Find(conferenceId);
 62
 63            if (conference == null)
 64                throw new ConferenceNotFoundException($"No conference with the id {conferenceId} found!");
 65
 66            this.Delegation = new Delegation()
 67            {
 68                Conference = conference,
 69                Roles = new List<ConferenceDelegateRole>()
 70            };
 71        }
 72    }
 73
 74    public class DelegationBuilderCommitteeSelector
 75    {
 76
 77        private MunityContext _dbContext;
 78
 79        private string _conferenceId;
 80
 14281        internal Delegation Delegation { get; private set; }
 82
 83        private int _countryId;
 84
 85        public BuildReadyDelegationBuilder InsideCommittee(params string[] committeeIds)
 186        {
 87
 588            foreach(var committeeId in committeeIds)
 189            {
 90                // Find a fitting role
 191                var role = _dbContext.Delegates
 192                    .Where(n => n.Delegation == null &&
 193                    n.DelegateCountry.CountryId == _countryId &&
 194                    n.Committee.CommitteeId == committeeId)
 195                    .FirstOrDefault();
 196                if (role == null)
 097                {
 098                    var isCountryRepresentedInsideCommittee = _dbContext.Delegates.Any(n => n.DelegateCountry.CountryId 
 099                    var msg = $"No fitting role to add was found for committee id: {committeeId} in conference {_confere
 0100                    if (!isCountryRepresentedInsideCommittee)
 0101                        msg += $" The country {_dbContext.Countries.Find((short)_countryId)?.Name} is not represented in
 102                    else
 0103                        msg += $" The country seems to be represented inside the given committee, but it seems to alread
 0104                    throw new ConferenceRoleNotFoundException(msg);
 105                }
 106                else
 1107                    Delegation.Roles.Add(role);
 108
 1109            }
 110
 1111            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 1112        }
 113
 114        public BuildReadyDelegationBuilder InsideCommitteeByShort(params string[] committeeShorts)
 32115        {
 116
 234117            foreach (var committeeShort in committeeShorts)
 69118            {
 119                // Find a fitting role
 69120                var role = _dbContext.Delegates
 69121                    .Where(n => n.Delegation == null &&
 69122                    n.DelegateCountry.CountryId == _countryId &&
 69123                    n.Committee.CommitteeShort == committeeShort &&
 69124                    n.Conference.ConferenceId == _conferenceId)
 69125                    .FirstOrDefault();
 69126                if (role == null)
 0127                {
 0128                    var isCountryRepresentedInsideCommittee = _dbContext.Delegates.Any(n => n.DelegateCountry.CountryId 
 0129                    var msg = $"No fitting role to add was found for committee short: {committeeShort} in conference {_c
 0130                    if (!isCountryRepresentedInsideCommittee)
 0131                        msg += $" The country {_dbContext.Countries.Find((short)_countryId)?.Name} is not represented in
 132                    else
 0133                        msg += $" The country seems to be represented inside the given committee, but it seems to alread
 0134                    throw new ConferenceRoleNotFoundException(msg);
 135                }
 136                else
 69137                    Delegation.Roles.Add(role);
 138
 69139            }
 140
 32141            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 32142        }
 143
 144        public BuildReadyDelegationBuilder InsideAnyCommittee()
 2145        {
 2146            var roles = _dbContext.Delegates
 2147                .Where(n => n.DelegateCountry.CountryId == _countryId &&
 2148                n.Conference.ConferenceId == _conferenceId &&
 2149                n.Delegation == null).ToList();
 2150            Delegation.Roles = roles;
 2151            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 2152        }
 153
 35154        public DelegationBuilderCommitteeSelector(MunityContext context, string conferenceId, int countryId, Delegation 
 35155        {
 35156            this._dbContext = context;
 35157            this._conferenceId = conferenceId;
 35158            this.Delegation = delegation;
 35159            this._countryId = countryId;
 35160        }
 161    }
 162
 163    public class BuildReadyDelegationBuilder
 164    {
 165        private MunityContext _context;
 166
 167        public Delegation Delegation { get; private set; }
 168
 169        public Delegation Save()
 170        {
 171            _context.Delegations.Add(Delegation);
 172            _context.SaveChanges();
 173            return Delegation;
 174        }
 175
 176        public BuildReadyDelegationBuilder(MunityContext context, Delegation delegation)
 177        {
 178            this._context = context;
 179            this.Delegation = delegation;
 180        }
 181    }
 182}