forked from ddrilling/AsbCloudServer
WellSectionController tested and fixed
This commit is contained in:
parent
50fc46e007
commit
33f545c7b3
@ -12,7 +12,6 @@ namespace AsbCloudApp.Services
|
||||
Task<WellSectionDto> InsertAsync(WellSectionDto newItem, 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<int> DeleteAsync(int id, CancellationToken token = default);
|
||||
Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default);
|
||||
}
|
||||
}
|
||||
|
@ -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<WellSectionDto>();
|
||||
dto.SectionType = entity.WellSectionType.Caption;
|
||||
return dto;
|
||||
@ -74,18 +77,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
public async Task<WellSectionDto> 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<WellSection>();
|
||||
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<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)
|
||||
{
|
||||
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<WellSection>();
|
||||
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<WellSectionDto>();
|
||||
}
|
||||
|
||||
public Task<int> 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<WellSectionDto>();
|
||||
dto.SectionType = sectionType.Caption;
|
||||
return dto;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<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);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
private bool CanUserAccessToWell(int idWell)
|
||||
{
|
||||
int? idCompany = User.GetCompanyId();
|
||||
return idCompany is not null && wellService.IsCompanyInvolvedInWell((int)idCompany, idWell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user