< Summary

Class:MUNity.Database.FluentAPI.DelegationApplicationBuilder
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\FluentAPI\Conference\DelegationApplicationBuilder.cs
Covered lines:86
Uncovered lines:11
Coverable lines:97
Total lines:143
Line coverage:88.6% (86 of 97)
Covered branches:8
Total branches:14
Branch coverage:57.1% (8 of 14)
Covered methods:7
Total methods:8
Method coverage:87.5% (7 of 8)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
WithAuthor(...)50%476.19%
WithMember(...)75%494.11%
WithPreferedDelegation(...)100%10%
WithPreferedDelegationByName(...)50%488.88%
WithFieldInput(...)50%292.85%
IsOpenedToPublic()100%1100%
Submit()100%1100%

File(s)

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

#LineLine coverage
 1using MUNity.Base;
 2using MUNity.Database.Context;
 3using MUNity.Database.Models.Conference;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Text;
 8using System.Threading.Tasks;
 9
 10namespace MUNity.Database.FluentAPI
 11{
 12
 13    public class DelegationApplicationBuilder
 14    {
 115        private DelegationApplication application = new DelegationApplication();
 16
 17        private MunityContext _context;
 18
 19        private string conferenceId;
 20
 21        public DelegationApplicationBuilder WithAuthor(string username)
 122        {
 123            var user = _context.Users.FirstOrDefault(n => n.UserName == username);
 124            if (user == null)
 025                throw new ArgumentException($"No user with the username {username} was found.");
 26
 127            var alreadyInsideUser = application.Users.FirstOrDefault(a => a.User.UserName == username);
 128            if (alreadyInsideUser != null)
 029            {
 030                alreadyInsideUser.Status = DelegationApplicationUserEntryStatuses.Joined;
 031                alreadyInsideUser.CanWrite = true;
 032            }
 33            else
 134            {
 135                application.Users.Add(new DelegationApplicationUserEntry()
 136                {
 137                    User = user,
 138                    Application = application,
 139                    Status = DelegationApplicationUserEntryStatuses.Joined,
 140                    CanWrite = true
 141                });
 142            }
 143            return this;
 144        }
 45
 46        public DelegationApplicationBuilder WithMember(string username)
 147        {
 148            var user = _context.Users.FirstOrDefault(n => n.UserName == username);
 149            if (user == null)
 050                throw new ArgumentException($"No user with the username {username} was found.");
 51
 252            var alreadyInsideUser = application.Users.FirstOrDefault(a => a.User.UserName == username);
 153            if (alreadyInsideUser == null)
 154            {
 155                application.Users.Add(new DelegationApplicationUserEntry()
 156                {
 157                    User = user,
 158                    Application = application,
 159                    Status = DelegationApplicationUserEntryStatuses.Invited,
 160                    CanWrite = true
 161                });
 162            }
 163            return this;
 164        }
 65
 66        public DelegationApplicationBuilder WithPreferedDelegation(string delegationId)
 067        {
 068            throw new NotImplementedException($"WithPreferedDelegation is not implemented yet.");
 69        }
 70
 71        public DelegationApplicationBuilder WithPreferedDelegationByName(string name)
 372        {
 373            var preferedDelegation =
 374                _context.Delegations.FirstOrDefault(
 375                    n => n.Conference.ConferenceId == conferenceId && n.Name == name);
 76
 377            if (preferedDelegation == null)
 078                throw new ArgumentException($"Unable to find a delegation with the name {name} inside the conference {co
 79
 380            var isAllowed = _context.Delegates.Any(n => n.Delegation.DelegationId == preferedDelegation.DelegationId &&
 381            n.ApplicationState == EApplicationStates.DelegationApplication);
 82
 383            if (!isAllowed)
 084                throw new ApplicationTypeNotAllowedException($"No Role inside the Delegation {preferedDelegation.Name} f
 85
 386            application.DelegationWishes.Add(new DelegationApplicationPickedDelegation()
 387            {
 388                Delegation = preferedDelegation,
 389                Application = application,
 390                Priority = (byte)application.DelegationWishes.Count
 391            });
 392            return this;
 393        }
 94
 95        public DelegationApplicationBuilder WithFieldInput(string fieldName, string value)
 196        {
 197            var field = _context.ConferenceApplicationFields.FirstOrDefault(n => n.FieldName == fieldName &&
 198            n.Forumula.FormulaType == ConferenceApplicationFormulaTypes.Delegation &&
 199            n.Forumula.Options.Conference.ConferenceId == conferenceId);
 1100            if (field == null)
 0101                throw new ArgumentException($"Unable to find the field with the name {fieldName} for the conference {con
 102
 1103            application.FormulaInputs.Add(new ConferenceDelegationApplicationFieldInput()
 1104            {
 1105                Value = value,
 1106                Application = application,
 1107                Field = field
 1108            });
 1109            return this;
 1110        }
 111
 112        public DelegationApplicationBuilder IsOpenedToPublic()
 1113        {
 1114            this.application.OpenToPublic = true;
 1115            return this;
 1116        }
 117
 118        public DelegationApplication Submit()
 1119        {
 1120            _context.DelegationApplications.Add(this.application);
 1121            _context.SaveChanges();
 1122            return application;
 1123        }
 124
 1125        public DelegationApplicationBuilder(MunityContext context, string conferenceId)
 1126        {
 1127            this._context = context;
 1128            this.conferenceId = conferenceId;
 129
 1130            this.application = new DelegationApplication()
 1131            {
 1132                DelegationWishes = new List<DelegationApplicationPickedDelegation>(),
 1133                Users = new List<DelegationApplicationUserEntry>(),
 1134                FormulaInputs = new List<ConferenceDelegationApplicationFieldInput>(),
 1135                ApplyDate = DateTime.Now,
 1136                OpenToPublic = false,
 1137                Status = ApplicationStatuses.Writing,
 1138                Conference = context.Conferences.Find(conferenceId)
 1139            };
 1140        }
 141    }
 142
 143}