< Summary

Class:MUNity.Services.DelegationService
Assembly:MUNity.Services
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\DelegationService.cs
Covered lines:0
Uncovered lines:105
Coverable lines:105
Total lines:151
Line coverage:0% (0 of 105)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)
Covered methods:0
Total methods:5
Method coverage:0% (0 of 5)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GetManageDelegationsInfo()0%20%
CreateDelegationAsync()0%60%
GetAvailableRoles()0%20%
AddRoleToDelegation()0%80%
.ctor(...)100%10%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityServices\Services\DelegationService.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Identity;
 2using MUNity.Database.Context;
 3using MUNity.Database.Models.User;
 4using MUNity.Schema.Conference;
 5using System;
 6using System.Collections.Generic;
 7using System.Linq;
 8using System.Security.Claims;
 9using System.Text;
 10using System.Threading.Tasks;
 11using MUNity.Schema.Extensions;
 12using MUNity.Database.Models.Conference;
 13using Microsoft.EntityFrameworkCore;
 14
 15namespace MUNity.Services
 16{
 17    public class DelegationService
 18    {
 19        private MunityContext context;
 20
 21        private UserConferenceAuthService authService;
 22
 23        public async Task<ManageDelegationsInfo> GetManageDelegationsInfo(string conferenceId, ClaimsPrincipal claim)
 024        {
 025            var isAllowed = await authService.IsUserAllowedToEditConference(conferenceId, claim);
 026            if (!isAllowed)
 027            {
 028                return null;
 29            }
 30
 031            ManageDelegationsInfo info = context.Conferences
 032                .Include(n => n.Delegations)
 033                .ThenInclude(n => n.Roles)
 034                .AsSingleQuery()
 035                .Select(conf => new ManageDelegationsInfo()
 036            {
 037                ConferenceId = conf.ConferenceId,
 038                ConferenceName = conf.Name,
 039                ConferenceShort = conf.ConferenceShort,
 040                OrganizationId = conf.ConferenceProject.ProjectOrganization.OrganizationId,
 041                OrganizationName = conf.ConferenceProject.ProjectOrganization.OrganizationName,
 042                OrganizationShort = conf.ConferenceProject.ProjectOrganization.OrganizationShort,
 043                ProjectId = conf.ConferenceProject.ProjectId,
 044                ProjectName = conf.ConferenceProject.ProjectName,
 045                ProjectShort = conf.ConferenceProject.ProjectShort,
 046                Delegations = conf.Delegations.Select(del => new ManageDelegationInfo()
 047                {
 048                    DelegationId = del.DelegationId,
 049                    DelegationName = del.Name,
 050                    DelegationShort = del.DelegationShort,
 051                    Roles = del.Roles.Select(role => new ManageDelegationRoleInfo()
 052                    {
 053                        ApplicationState = role.ApplicationState,
 054                        HasParicipant = role.Participations.Any(),
 055                        RoleCommitteeId = role.Committee.CommitteeId,
 056                        RoleCommitteeName = role.Committee.Name,
 057                        RoleId = role.RoleId,
 058                        RoleName = role.RoleName
 059                    }).ToList()
 060                }).ToList()
 061            }).FirstOrDefault(n => n.ConferenceId == conferenceId);
 062            return info;
 063        }
 64
 65        public async Task<CreateDelegationResponse> CreateDelegationAsync(CreateDelegationRequest request, ClaimsPrincip
 066        {
 067            var response = new CreateDelegationResponse();
 068            var isAllowed = await authService.IsUserAllowedToEditConference(request.ConferenceId, claim);
 069            if (!isAllowed)
 070            {
 071                response.AddNoPermissionError();
 072                return response;
 73            }
 74
 075            var conference = context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId);
 076            if (conference == null)
 077            {
 078                response.AddNotFoundError(nameof(request.ConferenceId));
 079                return response;
 80            }
 81
 082            var delegation = new Delegation()
 083            {
 084                Conference = conference,
 085                DelegationShort = request.DelegationShort,
 086                FullName = request.DelegationFullName,
 087                Name = request.DelegationName
 088            };
 89
 090            string easyId = conference.ConferenceId + "-" + Util.IdGenerator.AsPrimaryKey(request.DelegationShort);
 091            if (context.Delegations.All(n => n.DelegationId != easyId))
 092                delegation.DelegationId = easyId;
 93
 094            context.Delegations.Add(delegation);
 095            context.SaveChanges();
 96
 097            return response;
 098        }
 99
 100        public async Task<List<ManageDelegationRoleInfo>> GetAvailableRoles(string conferenceId, ClaimsPrincipal claim)
 0101        {
 0102            var isAllowed = await authService.IsUserAllowedToEditConference(conferenceId, claim);
 0103            if (!isAllowed)
 0104                return new List<ManageDelegationRoleInfo>();
 105
 0106            var roles = context.Delegates
 0107                .Where(n => n.Delegation == null && n.Conference.ConferenceId == conferenceId)
 0108                .OrderBy(n => n.RoleName)
 0109                .Select(n => new ManageDelegationRoleInfo()
 0110                {
 0111                    ApplicationState = n.ApplicationState,
 0112                    HasParicipant = n.Participations.Any(),
 0113                    RoleCommitteeId = n.Committee.CommitteeId,
 0114                    RoleCommitteeName = n.Committee.Name,
 0115                    RoleId = n.RoleId,
 0116                    RoleName = n.RoleName
 0117                }).ToList();
 0118            return roles;
 0119        }
 120
 121        public async Task<bool> AddRoleToDelegation(int roleId, string delegationId, ClaimsPrincipal claim)
 0122        {
 0123            var role = context.Delegates
 0124                .Include(n => n.Conference)
 0125                .FirstOrDefault(n => n.RoleId == roleId);
 0126            if (role == null || role.Conference == null)
 0127                return false;
 128
 0129            var isAllowed = await authService.IsUserAllowedToEditConference(role.Conference.ConferenceId, claim);
 0130            if (!isAllowed)
 0131                return false;
 132
 0133            var delegation = context.Delegations
 0134                .Include(n => n.Conference)
 0135                .FirstOrDefault(n => n.DelegationId == delegationId);
 136
 0137            if (role.Conference.ConferenceId != delegation.Conference.ConferenceId)
 0138                return false;
 139
 0140            role.Delegation = delegation;
 0141            context.SaveChanges();
 0142            return true;
 0143        }
 144
 0145        public DelegationService(MunityContext context, UserConferenceAuthService authService)
 0146        {
 0147            this.context = context;
 0148            this.authService = authService;
 0149        }
 150    }
 151}