| | | 1 | | using MUNity.Database.Context; |
| | | 2 | | using MUNity.Database.Models.Organization; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace MUNity.Database.FluentAPI; |
| | | 7 | | |
| | | 8 | | public class OrganizationSpecificTools |
| | | 9 | | { |
| | | 10 | | private string _organizationId; |
| | | 11 | | |
| | | 12 | | private MunityContext _dbContext; |
| | | 13 | | |
| | | 14 | | public OrganizationMember AddUserIntoRole(string username, string roleName) |
| | 4 | 15 | | { |
| | 4 | 16 | | var user = this._dbContext.Users.FirstOrDefault(n => n.NormalizedUserName == username.ToUpper()); |
| | | 17 | | |
| | 4 | 18 | | if (user == null) |
| | 1 | 19 | | throw new UserNotFoundException($"No user with the username {username} was found."); |
| | | 20 | | |
| | 3 | 21 | | var role = this._dbContext.OrganizationRoles.FirstOrDefault(n => n.Organization.OrganizationId == _organizationI |
| | 3 | 22 | | n.RoleName == roleName); |
| | | 23 | | |
| | 3 | 24 | | if (role == null) |
| | 1 | 25 | | throw new OrganizationRoleNotFoundException($"No Role with the name {roleName} was found for the organizatio |
| | | 26 | | |
| | 2 | 27 | | var membership = new OrganizationMember() |
| | 2 | 28 | | { |
| | 2 | 29 | | JoinedDate = DateTime.Now, |
| | 2 | 30 | | Organization = _dbContext.Organizations.Find(_organizationId), |
| | 2 | 31 | | Role = role, |
| | 2 | 32 | | User = user |
| | 2 | 33 | | }; |
| | | 34 | | |
| | 2 | 35 | | _dbContext.OrganizationMembers.Add(membership); |
| | 2 | 36 | | _dbContext.SaveChanges(); |
| | 2 | 37 | | return membership; |
| | 2 | 38 | | } |
| | | 39 | | |
| | | 40 | | public bool HasUserMembership(string username) |
| | 1 | 41 | | { |
| | 1 | 42 | | return _dbContext.OrganizationMembers.Any(n => n.Organization.OrganizationId == _organizationId && |
| | 1 | 43 | | n.User.UserName == username); |
| | 1 | 44 | | } |
| | | 45 | | |
| | 5 | 46 | | public OrganizationSpecificTools(MunityContext context, string organizationId) |
| | 5 | 47 | | { |
| | 5 | 48 | | this._dbContext = context; |
| | 5 | 49 | | this._organizationId = organizationId; |
| | 5 | 50 | | } |
| | | 51 | | } |