/
qwiklly
/
arc-server
Обзор
Документация
Войти
/
qwiklly
/
arc-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
src/AppRemoteConfig/Services/OrganizationService.cs
98 строк
4 KB
CLTanuki
chore: modules exploded
24 янв 2024, 18:53
24 янв 2024, 18:53
6b5ba70
Код
Авторство
О чём код?
namespace AppRemoteConfig.Services { using System.Security.Claims; using AppRemoteConfig.Models; using AppRemoteConfig.Models.Database; using AppRemoteConfig.Models.Dto; using AppRemoteConfig.Services.Interfaces; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; public class OrganizationsService : IOrganizationsService { private readonly ILogger<OrganizationsService> _logger; private readonly ApplicationContext _dbContext; private readonly AuthenticationStateProvider _authenticationStateProvider; public OrganizationsService( ILogger<OrganizationsService> logger, ApplicationContext dbContext, AuthenticationStateProvider authenticationStateProvider) { this._logger = logger; this._dbContext = dbContext; this._authenticationStateProvider = authenticationStateProvider; } public async Task<IEnumerable<OrganizationEntityDto>> ListOrganizations() { var availableOrgs = await GetAvailableOrgs(); var organizations = await availableOrgs.ToListAsync(); return organizations.ConvertAll(Converters.OrganizationEntityToDto); } public async Task<OrganizationEntityDto?> GetOrganization(Guid orgId) { var organization = await this._dbContext.Organizations.FindAsync(orgId); return organization == null ? null : Converters.OrganizationEntityToDto(organization); } public async Task<OrganizationEntityDto?> PutOrganization(OrganizationEntityDto organization) { if (organization.Id.HasValue && !OrganizationExists(organization.Id.Value)) { return null; } this._dbContext.SetModified(Converters.OrganizationDtoToEntity(organization)); await this._dbContext.SaveChangesAsync(); return organization; } public async Task<OrganizationEntityDto> CreateOrganization(OrganizationEntityDto organization) { var authState = await this._authenticationStateProvider.GetAuthenticationStateAsync(); var userIdClaim = authState.User.FindFirst(ClaimTypes.NameIdentifier).Value; Guid.TryParse(userIdClaim, out var uuid); var savedOrg = this._dbContext.Organizations.Add(new Organization() { Title = organization.Title }); var orgAccess = new OrganizationAccess() { Id = Guid.NewGuid(), AppUserId = uuid, GrantedRights = AccessLevelEnum.Owner }; savedOrg.Entity.OrganizationAccesses.Add(orgAccess); await this._dbContext.SaveChangesAsync(); return Converters.OrganizationEntityToDto(savedOrg.Entity); } public async Task<bool> DeleteOrganization(Guid id) { var organization = await this._dbContext.Organizations.FindAsync(id); if (organization == null) { this._logger.LogError("Organization with the ID provided was not found"); return false; } this._dbContext.Organizations.Remove(organization); await this._dbContext.SaveChangesAsync(); return true; } private bool OrganizationExists(Guid id) { return this._dbContext.Organizations.Any(e => e.Id == id); } private async Task<IQueryable<Organization>> GetAvailableOrgs() { var authState = await this._authenticationStateProvider.GetAuthenticationStateAsync(); var userIdClaim = authState.User.FindFirst(ClaimTypes.NameIdentifier).Value; Guid.TryParse(userIdClaim, out var uuid); var orgs = this._dbContext.Organizations.Where(o => o.OrganizationAccesses.Any(a => a.AppUserId == uuid)); return orgs; } } }