< Summary

Class:MUNity.Database.FluentAPI.BuildReadyDelegationBuilder
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\DelegationBuilder.cs
Covered lines:11
Uncovered lines:0
Coverable lines:11
Total lines:182
Line coverage:100% (11 of 11)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:3
Method coverage:100% (3 of 3)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Delegation()100%1100%
Save()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
 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
 105167        public Delegation Delegation { get; private set; }
 168
 169        public Delegation Save()
 35170        {
 35171            _context.Delegations.Add(Delegation);
 35172            _context.SaveChanges();
 35173            return Delegation;
 35174        }
 175
 35176        public BuildReadyDelegationBuilder(MunityContext context, Delegation delegation)
 35177        {
 35178            this._context = context;
 35179            this.Delegation = delegation;
 35180        }
 181    }
 182}

Methods/Properties

get_Delegation()
Save()
.ctor(...)