| | 1 | | using Microsoft.AspNetCore.Identity; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using Microsoft.Extensions.Logging; |
| | 4 | | using MUNity.Database.Context; |
| | 5 | | using MUNity.Database.Models.Organization; |
| | 6 | | using MUNity.Database.Models.User; |
| | 7 | | using MUNity.Schema.Organization; |
| | 8 | | using MUNity.Services.Exceptions; |
| | 9 | | using System; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Security.Claims; |
| | 13 | | using System.Text; |
| | 14 | | using System.Threading.Tasks; |
| | 15 | |
|
| | 16 | | namespace MUNity.Services |
| | 17 | | { |
| | 18 | | public class OrganizationService |
| | 19 | | { |
| | 20 | | private readonly MunityContext context; |
| | 21 | |
|
| | 22 | | ILogger<OrganizationService> log; |
| | 23 | |
|
| | 24 | | private UserManager<MunityUser> userManager; |
| | 25 | |
|
| | 26 | | public bool IsNameAvailable(string name) |
| 2 | 27 | | { |
| 2 | 28 | | if (string.IsNullOrWhiteSpace(name)) |
| 0 | 29 | | return false; |
| 2 | 30 | | return context.Organizations.AsNoTracking().All(n => n.OrganizationName.ToLower() != name.ToLower()); |
| 2 | 31 | | } |
| | 32 | |
|
| | 33 | | public bool IsShortAvailable(string shortName) |
| 2 | 34 | | { |
| 2 | 35 | | if (string.IsNullOrWhiteSpace(shortName)) |
| 0 | 36 | | return false; |
| 2 | 37 | | return context.Organizations.AsNoTracking().All(n => n.OrganizationShort.ToLower() != shortName.ToLower()); |
| 2 | 38 | | } |
| | 39 | |
|
| | 40 | | public Organization CreateOrganization(string name, string shortName, MunityUser user) |
| 1 | 41 | | { |
| | 42 | |
|
| 1 | 43 | | var organization = new Organization() |
| 1 | 44 | | { |
| 1 | 45 | | OrganizationName = name, |
| 1 | 46 | | OrganizationShort = shortName, |
| 1 | 47 | | }; |
| | 48 | |
|
| 1 | 49 | | var easyId = Util.IdGenerator.AsPrimaryKey(shortName); |
| 1 | 50 | | if (context.Organizations.All(n => n.OrganizationId != easyId)) |
| 1 | 51 | | organization.OrganizationId = easyId; |
| | 52 | |
|
| 1 | 53 | | var orgaAdminRole = new OrganizationRole() |
| 1 | 54 | | { |
| 1 | 55 | | CanCreateProject = true, |
| 1 | 56 | | CanManageMembers = true, |
| 1 | 57 | | CanCreateRoles = true, |
| 1 | 58 | | RoleName = "Admin", |
| 1 | 59 | | Organization = organization |
| 1 | 60 | | }; |
| | 61 | |
|
| 1 | 62 | | var membership = new OrganizationMember() |
| 1 | 63 | | { |
| 1 | 64 | | JoinedDate = DateTime.Now, |
| 1 | 65 | | Organization = organization, |
| 1 | 66 | | Role = orgaAdminRole, |
| 1 | 67 | | User = user |
| 1 | 68 | | }; |
| | 69 | |
|
| 1 | 70 | | context.Organizations.Add(organization); |
| 1 | 71 | | context.OrganizationRoles.Add(orgaAdminRole); |
| 1 | 72 | | context.OrganizationMembers.Add(membership); |
| 1 | 73 | | context.SaveChanges(); |
| 1 | 74 | | return organization; |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public async Task<CreateOrganizationResponse> CreateOrganizationAsync(CreateOrganizationRequest request, ClaimsP |
| 0 | 78 | | { |
| 0 | 79 | | log?.LogDebug("Request creating an organization!"); |
| | 80 | |
|
| 0 | 81 | | var response = new CreateOrganizationResponse(); |
| | 82 | |
|
| 0 | 83 | | if (claim == null ||claim.Identity == null || string.IsNullOrEmpty(claim.Identity.Name)) |
| 0 | 84 | | { |
| 0 | 85 | | response.Status = CreateOrganizationResponse.CreateOrgaStatusCodes.Error; |
| 0 | 86 | | log?.LogDebug($"The given claim was not valid."); |
| 0 | 87 | | return response; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | log?.LogInformation($"{claim.Identity.Name} wants to create the organization {request.Name}"); |
| | 91 | |
|
| 0 | 92 | | var user = await userManager.GetUserAsync(claim); |
| 0 | 93 | | if (user == null) |
| 0 | 94 | | { |
| 0 | 95 | | response.Status = CreateOrganizationResponse.CreateOrgaStatusCodes.Error; |
| 0 | 96 | | log?.LogDebug($"User was not found to create the organization."); |
| 0 | 97 | | return response; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | if (!IsNameAvailable(request.Name)) |
| 0 | 101 | | { |
| 0 | 102 | | response.Status = CreateOrganizationResponse.CreateOrgaStatusCodes.NameTaken; |
| 0 | 103 | | log?.LogDebug($"Organization Name: {request.Name} was already taken!"); |
| 0 | 104 | | return response; |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | if (!IsShortAvailable(request.ShortName)) |
| 0 | 108 | | { |
| 0 | 109 | | response.Status = CreateOrganizationResponse.CreateOrgaStatusCodes.ShortTaken; |
| 0 | 110 | | log?.LogDebug($"Organization Short: {request.ShortName} was already taken!"); |
| 0 | 111 | | return response; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | var createdOrganization = CreateOrganization(request.Name, request.ShortName, user); |
| 0 | 115 | | if (createdOrganization != null) |
| 0 | 116 | | { |
| 0 | 117 | | response.OrganizationId = createdOrganization.OrganizationId; |
| 0 | 118 | | log?.LogDebug($"Organization {request.Name} with the id {createdOrganization.OrganizationId} has been cr |
| 0 | 119 | | } |
| 0 | 120 | | return response; |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | |
|
| | 124 | |
|
| | 125 | | public bool OrganizationWithIdExisits(string id) |
| 1 | 126 | | { |
| 1 | 127 | | return context.Organizations.AsNoTracking().Any(n => n.OrganizationId == id); |
| 1 | 128 | | } |
| | 129 | |
|
| | 130 | | public bool IsUsernameMemberOfOrganiation(string username, string organizationId) |
| 1 | 131 | | { |
| 1 | 132 | | return context.OrganizationMembers.AsNoTracking().Any(n => n.User.UserName == username && n.Organization.Org |
| 1 | 133 | | } |
| | 134 | |
|
| | 135 | | public bool IsMemberOfOrganization(ClaimsPrincipal principal, string organizationId) |
| 0 | 136 | | { |
| 0 | 137 | | return IsUsernameMemberOfOrganiation(principal.Identity.Name, organizationId); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public OrganizationTinyInfo GetTinyInfo(string organizationId) |
| 1 | 141 | | { |
| 1 | 142 | | return context.Organizations.AsNoTracking().Select(n => new OrganizationTinyInfo() |
| 1 | 143 | | { |
| 1 | 144 | | Name = n.OrganizationName, |
| 1 | 145 | | OrganizationId = n.OrganizationId, |
| 1 | 146 | | Short = n.OrganizationShort |
| 1 | 147 | | }).FirstOrDefault(n => n.OrganizationId == organizationId); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | public List<OrganizationTinyInfo> GetTyinInfosOfAllOrgas() |
| 0 | 151 | | { |
| 0 | 152 | | return context.Organizations.AsNoTracking().Select(n => new OrganizationTinyInfo() |
| 0 | 153 | | { |
| 0 | 154 | | Name = n.OrganizationName, |
| 0 | 155 | | OrganizationId = n.OrganizationId, |
| 0 | 156 | | Short = n.OrganizationShort |
| 0 | 157 | | }).ToList(); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public List<OrganizationTinyInfo> GetTinyInfoOfUserOrganizations(string username) |
| 0 | 161 | | { |
| 0 | 162 | | return context.Organizations.AsNoTracking().Where(n => n.Member.Any(a => a.User.UserName == username)) |
| 0 | 163 | | .Select(n => new OrganizationTinyInfo() |
| 0 | 164 | | { |
| 0 | 165 | | Name = n.OrganizationName, |
| 0 | 166 | | OrganizationId = n.OrganizationId, |
| 0 | 167 | | Short = n.OrganizationShort |
| 0 | 168 | | }).ToList(); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | public List<OrganizationTinyInfo> GetTinyInfoOfUserOrganizations(ClaimsPrincipal claimPrincipal) |
| 0 | 172 | | { |
| 0 | 173 | | return GetTinyInfoOfUserOrganizations(claimPrincipal.Identity.Name); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | public OrganizationWithConferenceInfo GetOrgaConferenceInfo(string organizationId) |
| 1 | 177 | | { |
| 1 | 178 | | return context.Organizations.AsNoTracking().Select(n => new OrganizationWithConferenceInfo() |
| 1 | 179 | | { |
| 1 | 180 | | Name = n.OrganizationName, |
| 1 | 181 | | OrganizationId = n.OrganizationId, |
| 1 | 182 | | Short = n.OrganizationShort, |
| 1 | 183 | | ProjectCount = n.Projects.Count, |
| 1 | 184 | | ConferenceCount = n.Projects.Select(n => n.Conferences).Count() |
| 1 | 185 | | }).FirstOrDefault(n => n.OrganizationId == organizationId); |
| 1 | 186 | | } |
| | 187 | |
|
| | 188 | | public Organization GetCompleteOrganizationInfo(string organizationId) |
| 0 | 189 | | { |
| 0 | 190 | | return context.Organizations |
| 0 | 191 | | .AsNoTracking() |
| 0 | 192 | | .Include(n => n.Member) |
| 0 | 193 | | .ThenInclude(n => n.User) |
| 0 | 194 | | .Include(n => n.Projects) |
| 0 | 195 | | .Include(n => n.Roles) |
| 0 | 196 | | .FirstOrDefault(n => n.OrganizationId == organizationId); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | public OrganizationDashboardInfo GetDashboardInfo(string organizationId) |
| 0 | 200 | | { |
| 0 | 201 | | var info = context.Organizations |
| 0 | 202 | | .AsNoTracking() |
| 0 | 203 | | .Select(n => new OrganizationDashboardInfo() |
| 0 | 204 | | { |
| 0 | 205 | | OrganizationId = n.OrganizationId, |
| 0 | 206 | | Name = n.OrganizationName, |
| 0 | 207 | | Short = n.OrganizationShort, |
| 0 | 208 | | Projects = n.Projects.Select(a => new OrganizationDashboardProjectInfo() |
| 0 | 209 | | { |
| 0 | 210 | | ConferenceCount = a.Conferences.Count, |
| 0 | 211 | | Name = a.ProjectName, |
| 0 | 212 | | ProjectId = a.ProjectId |
| 0 | 213 | | }).ToList() |
| 0 | 214 | | }) |
| 0 | 215 | | .FirstOrDefault(n => n.OrganizationId == organizationId); |
| 0 | 216 | | info.Memebrs = context.OrganizationMembers |
| 0 | 217 | | .Where(n => n.Organization.OrganizationId == organizationId) |
| 0 | 218 | | .Take(8) |
| 0 | 219 | | .Select(a => new OrganizationMemberInfo() |
| 0 | 220 | | { |
| 0 | 221 | | Forename = a.User.Forename, |
| 0 | 222 | | LastName = a.User.Lastname, |
| 0 | 223 | | MemberUserName = a.User.UserName, |
| 0 | 224 | | RoleName = a.Role.RoleName |
| 0 | 225 | | }).ToList(); |
| | 226 | |
|
| 0 | 227 | | return info; |
| 0 | 228 | | } |
| | 229 | |
|
| 1 | 230 | | public OrganizationService(MunityContext context, ILogger<OrganizationService> logger, UserManager<MunityUser> u |
| 1 | 231 | | { |
| 1 | 232 | | this.context = context; |
| 1 | 233 | | this.log = logger; |
| 1 | 234 | | this.userManager = userManager; |
| 1 | 235 | | } |
| | 236 | | } |
| | 237 | | } |