This commit is contained in:
KharchenkoVV 2021-08-11 10:16:09 +05:00
commit e4800bf281
3 changed files with 91 additions and 51 deletions

View File

@ -12,7 +12,6 @@ namespace AsbCloudApp.Services
Task<WellSectionDto> InsertAsync(WellSectionDto newItem, int idWell, CancellationToken token = default); Task<WellSectionDto> InsertAsync(WellSectionDto newItem, int idWell, CancellationToken token = default);
Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> newItems, int idWell, CancellationToken token = default); Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> newItems, int idWell, CancellationToken token = default);
Task<WellSectionDto> UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default); Task<WellSectionDto> UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default);
Task<int> DeleteAsync(int id, CancellationToken token = default);
Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default); Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default);
} }
} }

View File

@ -66,6 +66,9 @@ namespace AsbCloudInfrastructure.Services
.FirstOrDefaultAsync(e => e.Id == id, token) .FirstOrDefaultAsync(e => e.Id == id, token)
.ConfigureAwait(false); .ConfigureAwait(false);
if (entity is null)
return null;
var dto = entity.Adapt<WellSectionDto>(); var dto = entity.Adapt<WellSectionDto>();
dto.SectionType = entity.WellSectionType.Caption; dto.SectionType = entity.WellSectionType.Caption;
return dto; return dto;
@ -73,18 +76,7 @@ namespace AsbCloudInfrastructure.Services
public async Task<WellSectionDto> InsertAsync(WellSectionDto item, int idWell, CancellationToken token = default) public async Task<WellSectionDto> InsertAsync(WellSectionDto item, int idWell, CancellationToken token = default)
{ {
if(string.IsNullOrEmpty(item.SectionType)) var sectionType = await GetWellSectionTypeFromCacheAndAssert(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 entity = item.Adapt<WellSection>(); var entity = item.Adapt<WellSection>();
entity.Id = default; entity.Id = default;
@ -92,28 +84,40 @@ namespace AsbCloudInfrastructure.Services
entity.IdWellSectionType = sectionType.Id; entity.IdWellSectionType = sectionType.Id;
var dbEntity = dbSet.Add(entity); var dbEntity = dbSet.Add(entity);
await context.SaveChangesAsync(token).ConfigureAwait(false); await context.SaveChangesAsync(token).ConfigureAwait(false);
return dbEntity.Entity.Adapt<WellSectionDto>();
var dto = dbEntity.Entity.Adapt<WellSectionDto>();
dto.SectionType = sectionType.Caption;
return dto;
} }
public Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> newItems, int idWell, CancellationToken token = default) public async Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> items, int idWell, CancellationToken token = default)
{ {
throw new NotImplementedException(); var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<WellSection>[items.Count()];
for (int i = 0; i < dbEntities.Length; i++)
{
var sectionType = await GetWellSectionTypeFromCacheAndAssert(items.ElementAt(i).SectionType);
var item = items.ElementAt(i).Adapt<WellSection>();
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<WellSectionDto>();
var sectionType = cachedSectionsTypes.FirstOrDefault(s => s.Id == e.Entity.IdWellSectionType);
dto.SectionType = sectionType.Caption;
return dto;
});
return dtos;
} }
public async Task<WellSectionDto> UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default) public async Task<WellSectionDto> UpdateAsync(WellSectionDto item, int idSection, int idWell, CancellationToken token = default)
{ {
if (string.IsNullOrEmpty(item.SectionType)) var sectionType = await GetWellSectionTypeFromCacheAndAssert(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 entity = item.Adapt<WellSection>(); var entity = item.Adapt<WellSection>();
entity.Id = idSection; entity.Id = idSection;
@ -121,21 +125,35 @@ namespace AsbCloudInfrastructure.Services
entity.IdWellSectionType = sectionType.Id; entity.IdWellSectionType = sectionType.Id;
var dbEntity = dbSet.Update(entity); var dbEntity = dbSet.Update(entity);
await context.SaveChangesAsync(token).ConfigureAwait(false); await context.SaveChangesAsync(token).ConfigureAwait(false);
return dbEntity.Entity.Adapt<WellSectionDto>();
}
public Task<int> DeleteAsync(int id, CancellationToken token = default) var dto = dbEntity.Entity.Adapt<WellSectionDto>();
{ dto.SectionType = sectionType.Caption;
var entity = dbSet.FirstOrDefault(e => e.Id == id); return dto;
if (entity == default)
return Task.FromResult(0);
dbSet.Remove(entity);
return context.SaveChangesAsync(token);
} }
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default) public Task<int> DeleteAsync(IEnumerable<int> 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<WellSectionType> 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;
} }
} }
} }

View File

@ -2,6 +2,7 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,48 +13,70 @@ namespace AsbCloudWebApi.Controllers
[Authorize] [Authorize]
public class WellSectionController : ControllerBase 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] [HttpGet]
[Route("")]
public async Task<IActionResult> GetAllAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default) public async Task<IActionResult> 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); return Ok(result);
} }
[HttpGet] [HttpGet]
[Route("{idSection}")] [Route("{idSection}")]
public async Task<IActionResult> GetAsync(int idSection,CancellationToken token = default) public async Task<IActionResult> 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); return Ok(result);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> Insert([FromBody] WellSectionDto value, int idWell, CancellationToken token = default) public async Task<IActionResult> Insert(int idWell, [FromBody] IEnumerable<WellSectionDto> 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); return Ok(result);
} }
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> Put(int id, [FromBody] WellSectionDto value, int idWell, CancellationToken token = default) public async Task<IActionResult> 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); return Ok(result);
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id, CancellationToken token = default) public async Task<IActionResult> 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); return Ok(result);
} }
private bool CanUserAccessToWell(int idWell)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell);
}
} }
} }