From 33f545c7b34badb69ad38f7c63c63ecefe86b0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Tue, 10 Aug 2021 17:43:13 +0500 Subject: [PATCH] WellSectionController tested and fixed --- AsbCloudApp/Services/IWellSectionService.cs | 1 - .../Services/WellSectionService.cs | 92 +++++++++++-------- .../Controllers/WellSectionController.cs | 49 +++++++--- 3 files changed, 91 insertions(+), 51 deletions(-) diff --git a/AsbCloudApp/Services/IWellSectionService.cs b/AsbCloudApp/Services/IWellSectionService.cs index 67dfe012..02cd3880 100644 --- a/AsbCloudApp/Services/IWellSectionService.cs +++ b/AsbCloudApp/Services/IWellSectionService.cs @@ -12,7 +12,6 @@ namespace AsbCloudApp.Services Task InsertAsync(WellSectionDto newItem, int idWell, CancellationToken token = default); Task> InsertRangeAsync(IEnumerable newItems, int idWell, CancellationToken token = default); Task UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default); - Task DeleteAsync(int id, CancellationToken token = default); Task DeleteAsync(IEnumerable ids, CancellationToken token = default); } } diff --git a/AsbCloudInfrastructure/Services/WellSectionService.cs b/AsbCloudInfrastructure/Services/WellSectionService.cs index 6fdbeb79..9b9f0909 100644 --- a/AsbCloudInfrastructure/Services/WellSectionService.cs +++ b/AsbCloudInfrastructure/Services/WellSectionService.cs @@ -67,6 +67,9 @@ namespace AsbCloudInfrastructure.Services .FirstOrDefaultAsync(e => e.Id == id, token) .ConfigureAwait(false); + if (entity is null) + return null; + var dto = entity.Adapt(); dto.SectionType = entity.WellSectionType.Caption; return dto; @@ -74,18 +77,7 @@ namespace AsbCloudInfrastructure.Services public async Task InsertAsync(WellSectionDto item, int idWell, CancellationToken token = default) { - if(string.IsNullOrEmpty(item.SectionType)) - throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType)); - - var sectionType = await cachedSectionsTypes - .FirstOrDefaultAsync(s => s.Caption == item.SectionType, token) - .ConfigureAwait(false); - - if (sectionType is null) - { - throw new ArgumentException("Тип секции отсутствует в справочнике", nameof(WellSectionDto.SectionType)); - //sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token); - } + var sectionType = await GetWellSectionTypeFromCacheAndAssert(item.SectionType); var entity = item.Adapt(); entity.Id = default; @@ -93,28 +85,40 @@ namespace AsbCloudInfrastructure.Services entity.IdWellSectionType = sectionType.Id; var dbEntity = dbSet.Add(entity); await context.SaveChangesAsync(token).ConfigureAwait(false); - return dbEntity.Entity.Adapt(); + + var dto = dbEntity.Entity.Adapt(); + dto.SectionType = sectionType.Caption; + return dto; } - public Task> InsertRangeAsync(IEnumerable newItems, int idWell, CancellationToken token = default) + public async Task> InsertRangeAsync(IEnumerable items, int idWell, CancellationToken token = default) { - throw new NotImplementedException(); + var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry[items.Count()]; + + for (int i = 0; i < dbEntities.Length; i++) + { + var sectionType = await GetWellSectionTypeFromCacheAndAssert(items.ElementAt(i).SectionType); + var item = items.ElementAt(i).Adapt(); + item.IdWell = idWell; + item.IdWellSectionType = sectionType.Id; + dbEntities[i] = dbSet.Add(item); + } + + await context.SaveChangesAsync(token).ConfigureAwait(false); + + var dtos = dbEntities.Select((e) => { + var dto = e.Entity.Adapt(); + var sectionType = cachedSectionsTypes.FirstOrDefault(s => s.Id == e.Entity.IdWellSectionType); + dto.SectionType = sectionType.Caption; + return dto; + }); + + return dtos; } public async Task UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default) { - if (string.IsNullOrEmpty(item.SectionType)) - throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType)); - - var sectionType = await cachedSectionsTypes - .FirstOrDefaultAsync(s => s.Caption == item.SectionType, token) - .ConfigureAwait(false); - - if (sectionType is null) - { - throw new ArgumentException("Тип секции отсутствует в справочнике", nameof(WellSectionDto.SectionType)); - //sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token); - } + var sectionType = await GetWellSectionTypeFromCacheAndAssert(item.SectionType); var entity = item.Adapt(); entity.Id = idSection; @@ -122,21 +126,35 @@ namespace AsbCloudInfrastructure.Services entity.IdWellSectionType = sectionType.Id; var dbEntity = dbSet.Update(entity); await context.SaveChangesAsync(token).ConfigureAwait(false); - return dbEntity.Entity.Adapt(); - } - public Task DeleteAsync(int id, CancellationToken token = default) - { - var entity = dbSet.FirstOrDefault(e => e.Id == id); - if (entity == default) - return Task.FromResult(0); - dbSet.Remove(entity); - return context.SaveChangesAsync(token); + var dto = dbEntity.Entity.Adapt(); + dto.SectionType = sectionType.Caption; + return dto; } public Task DeleteAsync(IEnumerable ids, CancellationToken token = default) { - throw new NotImplementedException(); + var entities = dbSet.Where(e => ids.Contains(e.Id)); + dbSet.RemoveRange(entities); + return context.SaveChangesAsync(token); + } + + private async Task GetWellSectionTypeFromCacheAndAssert(string wellSectionType, CancellationToken token = default) + { + if (string.IsNullOrEmpty(wellSectionType)) + throw new ArgumentException("Тип секции должен быть указан", nameof(WellSectionDto.SectionType)); + + var sectionType = await cachedSectionsTypes + .FirstOrDefaultAsync(s => s.Caption.Equals(wellSectionType, StringComparison.OrdinalIgnoreCase), token) + .ConfigureAwait(false); + + if (sectionType is null) + { + throw new ArgumentException($"Тип секции '{wellSectionType}' отсутствует в справочнике", nameof(WellSectionDto.SectionType)) ; + //sectionType = await cachedSectionsTypes.InsertAsync(new WellSectionType { Caption = item.SectionType}, token); + } + + return sectionType; } } } diff --git a/AsbCloudWebApi/Controllers/WellSectionController.cs b/AsbCloudWebApi/Controllers/WellSectionController.cs index f7bf1dc8..26301fa6 100644 --- a/AsbCloudWebApi/Controllers/WellSectionController.cs +++ b/AsbCloudWebApi/Controllers/WellSectionController.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -12,48 +13,70 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class WellSectionController : ControllerBase { - private readonly IWellSectionService service; + private readonly IWellSectionService sectionsService; + private readonly IWellService wellService; - public WellSectionController(IWellSectionService service) + public WellSectionController(IWellSectionService sectionsService, IWellService wellService) { - this.service = service; + this.sectionsService = sectionsService; + this.wellService = wellService; } [HttpGet] - [Route("")] public async Task GetAllAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default) { - var result = await service.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); + if(!CanUserAccessToWell(idWell)) + return Forbid(); + + var result = await sectionsService.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); return Ok(result); } [HttpGet] [Route("{idSection}")] - public async Task GetAsync(int idSection,CancellationToken token = default) + public async Task GetAsync(int idSection, int idWell, CancellationToken token = default) { - var result = await service.GetAsync(idSection, token).ConfigureAwait(false); + if (!CanUserAccessToWell(idWell)) + return Forbid(); + + var result = await sectionsService.GetAsync(idSection, token).ConfigureAwait(false); return Ok(result); } [HttpPost] - public async Task Insert([FromBody] WellSectionDto value, int idWell, CancellationToken token = default) + public async Task Insert(int idWell, [FromBody] IEnumerable values, CancellationToken token = default) { - var result = await service.InsertAsync(value, idWell, token).ConfigureAwait(false); + if (!CanUserAccessToWell(idWell)) + return Forbid(); + + var result = await sectionsService.InsertRangeAsync(values, idWell, token).ConfigureAwait(false); return Ok(result); } [HttpPut("{id}")] - public async Task Put(int id, [FromBody] WellSectionDto value, int idWell, CancellationToken token = default) + public async Task Put(int id, int idWell, [FromBody] WellSectionDto value, CancellationToken token = default) { - var result = await service.UpdateAsync(value, id, idWell, token).ConfigureAwait(false); + if (!CanUserAccessToWell(idWell)) + return Forbid(); + + var result = await sectionsService.UpdateAsync(value, id, idWell, token).ConfigureAwait(false); return Ok(result); } [HttpDelete("{id}")] - public async Task Delete(int id, CancellationToken token = default) + public async Task Delete(int id, int idWell, CancellationToken token = default) { - var result = await service.DeleteAsync(id, token).ConfigureAwait(false); + if (!CanUserAccessToWell(idWell)) + return Forbid(); + + var result = await sectionsService.DeleteAsync(new int[] { id }, token).ConfigureAwait(false); return Ok(result); } + + private bool CanUserAccessToWell(int idWell) + { + int? idCompany = User.GetCompanyId(); + return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell); + } } }