< Summary

Class:MUNity.Database.FluentAPI.DelegationBuilder
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\DelegationBuilder.cs
Covered lines:25
Uncovered lines:13
Coverable lines:38
Total lines:182
Line coverage:65.7% (25 of 38)
Covered branches:2
Total branches:10
Branch coverage:20% (2 of 10)
Covered methods:4
Total methods:5
Method coverage:80% (4 of 5)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Delegation()100%1100%
WithName(...)100%1100%
SetIdAsEasyIdByName()0%60%
WithCountry(...)50%288.88%
.ctor(...)50%292.3%

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    {
 14015        internal Delegation Delegation { get; private set; }
 16
 17        private MunityContext _dbContext;
 18
 19        private string _conferenceId;
 20
 21        public DelegationBuilder WithName(string name)
 3522        {
 23            // Handle easyId
 24
 3525            Delegation.Name = name;
 26            //SetIdAsEasyIdByName();
 3527            return this;
 3528        }
 29
 30        private void SetIdAsEasyIdByName()
 031        {
 032            if (!string.IsNullOrWhiteSpace(Delegation.DelegationId)) return;
 33
 034            var easyNameDelegation = Util.IdGenerator.AsPrimaryKey(Delegation.Name);
 035            var easyId = _conferenceId + "-" + easyNameDelegation;
 036            if (_dbContext.Delegations.All(n => n.DelegationId != easyId) && _dbContext.ChangeTracker.Entries<Delegation
 037            {
 038                Console.WriteLine($"Assign easy id to Delegation {Delegation.Name}: {easyId}");
 039                Delegation.DelegationId = easyId;
 040            }
 41            else
 042                Delegation.DelegationId = Guid.NewGuid().ToString();
 043        }
 44
 45        public DelegationBuilderCommitteeSelector WithCountry(string countryName)
 3546        {
 3547            short countryId = _dbContext.Countries.AsNoTracking()
 3548                .Where(n => n.Name == countryName || n.FullName == countryName)
 3549                .Select(n => n.CountryId)
 3550                .FirstOrDefault();
 3551            if (countryId == 0)
 052                throw new CountryNotFoundException($"No country with the name {countryName} found!");
 53
 3554            return new DelegationBuilderCommitteeSelector(_dbContext, _conferenceId, countryId, Delegation);
 3555        }
 56
 3557        public DelegationBuilder(MunityContext context, string conferenceId)
 3558        {
 3559            this._dbContext = context;
 3560            this._conferenceId = conferenceId;
 3561            var conference = _dbContext.Conferences.Find(conferenceId);
 62
 3563            if (conference == null)
 064                throw new ConferenceNotFoundException($"No conference with the id {conferenceId} found!");
 65
 3566            this.Delegation = new Delegation()
 3567            {
 3568                Conference = conference,
 3569                Roles = new List<ConferenceDelegateRole>()
 3570            };
 3571        }
 72    }
 73
 74    public class DelegationBuilderCommitteeSelector
 75    {
 76
 77        private MunityContext _dbContext;
 78
 79        private string _conferenceId;
 80
 81        internal Delegation Delegation { get; private set; }
 82
 83        private int _countryId;
 84
 85        public BuildReadyDelegationBuilder InsideCommittee(params string[] committeeIds)
 86        {
 87
 88            foreach(var committeeId in committeeIds)
 89            {
 90                // Find a fitting role
 91                var role = _dbContext.Delegates
 92                    .Where(n => n.Delegation == null &&
 93                    n.DelegateCountry.CountryId == _countryId &&
 94                    n.Committee.CommitteeId == committeeId)
 95                    .FirstOrDefault();
 96                if (role == null)
 97                {
 98                    var isCountryRepresentedInsideCommittee = _dbContext.Delegates.Any(n => n.DelegateCountry.CountryId 
 99                    var msg = $"No fitting role to add was found for committee id: {committeeId} in conference {_confere
 100                    if (!isCountryRepresentedInsideCommittee)
 101                        msg += $" The country {_dbContext.Countries.Find((short)_countryId)?.Name} is not represented in
 102                    else
 103                        msg += $" The country seems to be represented inside the given committee, but it seems to alread
 104                    throw new ConferenceRoleNotFoundException(msg);
 105                }
 106                else
 107                    Delegation.Roles.Add(role);
 108
 109            }
 110
 111            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 112        }
 113
 114        public BuildReadyDelegationBuilder InsideCommitteeByShort(params string[] committeeShorts)
 115        {
 116
 117            foreach (var committeeShort in committeeShorts)
 118            {
 119                // Find a fitting role
 120                var role = _dbContext.Delegates
 121                    .Where(n => n.Delegation == null &&
 122                    n.DelegateCountry.CountryId == _countryId &&
 123                    n.Committee.CommitteeShort == committeeShort &&
 124                    n.Conference.ConferenceId == _conferenceId)
 125                    .FirstOrDefault();
 126                if (role == null)
 127                {
 128                    var isCountryRepresentedInsideCommittee = _dbContext.Delegates.Any(n => n.DelegateCountry.CountryId 
 129                    var msg = $"No fitting role to add was found for committee short: {committeeShort} in conference {_c
 130                    if (!isCountryRepresentedInsideCommittee)
 131                        msg += $" The country {_dbContext.Countries.Find((short)_countryId)?.Name} is not represented in
 132                    else
 133                        msg += $" The country seems to be represented inside the given committee, but it seems to alread
 134                    throw new ConferenceRoleNotFoundException(msg);
 135                }
 136                else
 137                    Delegation.Roles.Add(role);
 138
 139            }
 140
 141            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 142        }
 143
 144        public BuildReadyDelegationBuilder InsideAnyCommittee()
 145        {
 146            var roles = _dbContext.Delegates
 147                .Where(n => n.DelegateCountry.CountryId == _countryId &&
 148                n.Conference.ConferenceId == _conferenceId &&
 149                n.Delegation == null).ToList();
 150            Delegation.Roles = roles;
 151            return new BuildReadyDelegationBuilder(this._dbContext, Delegation);
 152        }
 153
 154        public DelegationBuilderCommitteeSelector(MunityContext context, string conferenceId, int countryId, Delegation 
 155        {
 156            this._dbContext = context;
 157            this._conferenceId = conferenceId;
 158            this.Delegation = delegation;
 159            this._countryId = countryId;
 160        }
 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}