| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | | using MUNity.Database.Context; |
| | 4 | | using MUNity.Database.Models.Conference; |
| | 5 | | using MUNity.Schema.Conference; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Security.Claims; |
| | 10 | | using System.Text; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace MUNity.Services |
| | 14 | | { |
| | 15 | | public class ConferenceApplicationService |
| | 16 | | { |
| | 17 | | private MunityContext _dbContext; |
| | 18 | |
|
| | 19 | | private ILogger<ConferenceApplicationService> _logger; |
| | 20 | |
|
| | 21 | | public enum AvailableDelegationsTypes |
| | 22 | | { |
| | 23 | | All, |
| | 24 | | OnlineOnly, |
| | 25 | | PresenceOnly |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public List<ApplicationAvailableDelegation> AvailableDelegations(string conferenceId, int minRolesCount = 1, Ava |
| 0 | 29 | | { |
| | 30 | | IQueryable<Delegation> delegationsQuery; |
| | 31 | |
|
| 0 | 32 | | switch (type) |
| | 33 | | { |
| | 34 | | case AvailableDelegationsTypes.All: |
| 0 | 35 | | throw new NotImplementedException(); |
| | 36 | | case AvailableDelegationsTypes.OnlineOnly: |
| 0 | 37 | | delegationsQuery = _dbContext.Fluent.ForConference(conferenceId) |
| 0 | 38 | | .DelegationsWithOnlyOnlineRoles(); |
| 0 | 39 | | break; |
| | 40 | | case AvailableDelegationsTypes.PresenceOnly: |
| 0 | 41 | | delegationsQuery = _dbContext.Fluent.ForConference(conferenceId) |
| 0 | 42 | | .DelegationsWithOnlyAtLocationSlots(); |
| 0 | 43 | | break; |
| | 44 | | default: |
| 0 | 45 | | throw new NotImplementedException(); |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | return delegationsQuery.Select(n => new ApplicationAvailableDelegation() |
| 0 | 49 | | { |
| 0 | 50 | | DelegationId = n.DelegationId, |
| 0 | 51 | | Name = n.Name, |
| 0 | 52 | | Roles = n.Roles.Select(role => new ApplicationDelegationRoleSlot() |
| 0 | 53 | | { |
| 0 | 54 | | CommitteeName = role.Committee.Name, |
| 0 | 55 | | Costs = _dbContext.Fluent.ForConference(conferenceId).GetCostOfRole(role.RoleId), |
| 0 | 56 | | RoleName = role.RoleName, |
| 0 | 57 | | CountryName = role.DelegateCountry.Name, |
| 0 | 58 | | CountryIso = role.DelegateCountry.Iso |
| 0 | 59 | | }).ToList() |
| 0 | 60 | | }).Where(n => n.Roles.Count >= minRolesCount).ToList(); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public FindUserForApplicationResult FindUserToAddToDelegationApplication(string mailOrUsername, string conferenc |
| 0 | 64 | | { |
| 0 | 65 | | var normalized = mailOrUsername.ToUpper(); |
| 0 | 66 | | var user = _dbContext.Users.FirstOrDefault(n => n.NormalizedUserName == normalized || n.NormalizedEmail == n |
| 0 | 67 | | if (user != null) |
| 0 | 68 | | { |
| | 69 | | // Check if user is already participating |
| 0 | 70 | | var alreadyParticipating = _dbContext.Participations.Any(n => n.User.Id == user.Id && n.Role.Conference. |
| 0 | 71 | | if (alreadyParticipating) |
| 0 | 72 | | return new FindUserForApplicationResult() { Status = FindUserForApplicationResult.ResultStatuses.Alr |
| | 73 | |
|
| | 74 | | // Check user already in an application |
| 0 | 75 | | var alreadyApplying = _dbContext.DelegationApplicationUserEntries |
| 0 | 76 | | .Any(n => n.User.Id == user.Id && n.Application.DelegationWishes |
| 0 | 77 | | .Any(a => a.Delegation.Conference.ConferenceId == conferenceId)); |
| 0 | 78 | | if (alreadyApplying) |
| 0 | 79 | | return new FindUserForApplicationResult() { Status = FindUserForApplicationResult.ResultStatuses.Alr |
| | 80 | |
|
| | 81 | | // Return the user and ok |
| 0 | 82 | | return new FindUserForApplicationResult() |
| 0 | 83 | | { |
| 0 | 84 | | Status = FindUserForApplicationResult.ResultStatuses.CanBeAdded, |
| 0 | 85 | | ForeName = user.Forename, |
| 0 | 86 | | LastName = user.Lastname, |
| 0 | 87 | | UserName = user.UserName |
| 0 | 88 | | }; |
| | 89 | | } |
| | 90 | | else |
| 0 | 91 | | { |
| | 92 | | // Return user doesnt exists result |
| 0 | 93 | | return new FindUserForApplicationResult() { Status = FindUserForApplicationResult.ResultStatuses.NoUserF |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public List<UserApplicationInfo> GetApplicationsOfUser(ClaimsPrincipal claim) |
| 0 | 98 | | { |
| 0 | 99 | | var list = new List<UserApplicationInfo>(); |
| | 100 | |
|
| 0 | 101 | | var delegationApplications = _dbContext.DelegationApplicationUserEntries |
| 0 | 102 | | .Where(n => n.User.NormalizedUserName == claim.Identity.Name.ToUpper()) |
| 0 | 103 | | .Include(n => n.Application) |
| 0 | 104 | | .AsNoTracking() |
| 0 | 105 | | .Select(n => new UserApplicationInfo() |
| 0 | 106 | | { |
| 0 | 107 | | ApplicationId = n.Application.DelegationApplicationId, |
| 0 | 108 | | ConferenceFullName = n.Application.Conference.FullName, |
| 0 | 109 | | ConferenceId = n.Application.Conference.ConferenceId, |
| 0 | 110 | | ConferenceName = n.Application.Conference.Name, |
| 0 | 111 | | ConferenceShort = n.Application.Conference.ConferenceShort |
| 0 | 112 | | }); |
| | 113 | |
|
| | 114 | |
|
| 0 | 115 | | return delegationApplications.ToList(); |
| 0 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public ConferenceApplicationService(MunityContext context, ILogger<ConferenceApplicationService> logger) |
| 0 | 119 | | { |
| 0 | 120 | | this._dbContext = context; |
| 0 | 121 | | this._logger = logger; |
| 0 | 122 | | } |
| | 123 | | } |
| | 124 | | } |