forked from ddrilling/AsbCloudServer
WellSectionController almost done
This commit is contained in:
parent
0f08d71ddb
commit
50fc46e007
@ -5,8 +5,14 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudApp.Services
|
||||
{
|
||||
public interface IWellSectionService: ICrudService<WellSectionDto>
|
||||
public interface IWellSectionService
|
||||
{
|
||||
Task<PaginationContainer<WellSectionDto>> GetAllByWellIdAsync(int idWell, int skip=0, int take=32, CancellationToken token = default);
|
||||
Task<PaginationContainer<WellSectionDto>> GetAllByWellIdAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default);
|
||||
Task<WellSectionDto> GetAsync(int id, 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<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);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using AsbCloudInfrastructure.Services.Cache;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
@ -16,33 +17,13 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
private readonly IAsbCloudDbContext context;
|
||||
private readonly DbSet<WellSection> dbSet;
|
||||
private readonly CacheTable<WellSectionType> cachedSectionsTypes;
|
||||
|
||||
public WellSectionService(IAsbCloudDbContext context)
|
||||
public WellSectionService(IAsbCloudDbContext context, Cache.CacheDb cache)
|
||||
{
|
||||
this.context = context;
|
||||
dbSet = context.Set<WellSection>();
|
||||
}
|
||||
|
||||
|
||||
public Task<WellSectionDto> GetAsync(int id, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<WellSectionDto> InsertAsync(WellSectionDto newItem, CancellationToken token = default)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> newItems, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<WellSectionDto> UpdateAsync(WellSectionDto item, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
cachedSectionsTypes = cache.GetCachedTable<WellSectionType>((DbContext)context);
|
||||
}
|
||||
|
||||
public async Task<PaginationContainer<WellSectionDto>> GetAllByWellIdAsync(int idWell, int skip, int take, CancellationToken token = default)
|
||||
@ -70,16 +51,89 @@ namespace AsbCloudInfrastructure.Services
|
||||
var entities = await query.Take(take).ToListAsync(token).ConfigureAwait(false);
|
||||
|
||||
foreach (var item in entities)
|
||||
result.Items.Add(item.Adapt<WellSectionDto>());
|
||||
{
|
||||
var dto = item.Adapt<WellSectionDto>();
|
||||
dto.SectionType = item.WellSectionType.Caption;
|
||||
result.Items.Add(dto);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<int> DeleteAsync(int id, CancellationToken token = default)
|
||||
public async Task<WellSectionDto> GetAsync(int id, CancellationToken token = default)
|
||||
{
|
||||
var entity = await dbSet
|
||||
.Include(s => s.WellSectionType)
|
||||
.FirstOrDefaultAsync(e => e.Id == id, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var dto = entity.Adapt<WellSectionDto>();
|
||||
dto.SectionType = entity.WellSectionType.Caption;
|
||||
return dto;
|
||||
}
|
||||
|
||||
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 entity = item.Adapt<WellSection>();
|
||||
entity.Id = default;
|
||||
entity.IdWell = idWell;
|
||||
entity.IdWellSectionType = sectionType.Id;
|
||||
var dbEntity = dbSet.Add(entity);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
return dbEntity.Entity.Adapt<WellSectionDto>();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<WellSectionDto>> InsertRangeAsync(IEnumerable<WellSectionDto> newItems, int idWell, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
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 entity = item.Adapt<WellSection>();
|
||||
entity.Id = idSection;
|
||||
entity.IdWell = idWell;
|
||||
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);
|
||||
}
|
||||
|
||||
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
@ -27,17 +27,25 @@ namespace AsbCloudWebApi.Controllers
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Insert([FromBody] WellSectionDto value, CancellationToken token = default)
|
||||
[HttpGet]
|
||||
[Route("{idSection}")]
|
||||
public async Task<IActionResult> GetAsync(int idSection,CancellationToken token = default)
|
||||
{
|
||||
var result = await service.InsertAsync(value, token).ConfigureAwait(false);
|
||||
var result = await service.GetAsync(idSection, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Insert([FromBody] WellSectionDto value, int idWell, CancellationToken token = default)
|
||||
{
|
||||
var result = await service.InsertAsync(value, idWell, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> Put(int id, [FromBody] WellSectionDto value, CancellationToken token = default)
|
||||
public async Task<IActionResult> Put(int id, [FromBody] WellSectionDto value, int idWell, CancellationToken token = default)
|
||||
{
|
||||
var result = await service.UpdateAsync(value, token).ConfigureAwait(false);
|
||||
var result = await service.UpdateAsync(value, id, idWell, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,6 @@ using System;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user