diff --git a/AsbCloudApp/Services/IWellSectionService.cs b/AsbCloudApp/Services/IWellSectionService.cs index a7b22f19..67dfe012 100644 --- a/AsbCloudApp/Services/IWellSectionService.cs +++ b/AsbCloudApp/Services/IWellSectionService.cs @@ -5,8 +5,14 @@ using System.Threading.Tasks; namespace AsbCloudApp.Services { - public interface IWellSectionService: ICrudService + public interface IWellSectionService { - Task> GetAllByWellIdAsync(int idWell, int skip=0, int take=32, CancellationToken token = default); + Task> GetAllByWellIdAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default); + Task GetAsync(int id, CancellationToken token = default); + 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 2d237738..6fdbeb79 100644 --- a/AsbCloudInfrastructure/Services/WellSectionService.cs +++ b/AsbCloudInfrastructure/Services/WellSectionService.cs @@ -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 dbSet; + private readonly CacheTable cachedSectionsTypes; - public WellSectionService(IAsbCloudDbContext context) + public WellSectionService(IAsbCloudDbContext context, Cache.CacheDb cache) { this.context = context; dbSet = context.Set(); - } - - - public Task GetAsync(int id, CancellationToken token = default) - { - throw new NotImplementedException(); - } - - public Task InsertAsync(WellSectionDto newItem, CancellationToken token = default) - { - - throw new NotImplementedException(); - } - - public Task> InsertRangeAsync(IEnumerable newItems, CancellationToken token = default) - { - throw new NotImplementedException(); - } - - public Task UpdateAsync(WellSectionDto item, CancellationToken token = default) - { - throw new NotImplementedException(); + cachedSectionsTypes = cache.GetCachedTable((DbContext)context); } public async Task> 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()); + { + var dto = item.Adapt(); + dto.SectionType = item.WellSectionType.Caption; + result.Items.Add(dto); + } return result; } - public Task DeleteAsync(int id, CancellationToken token = default) + public async Task 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(); + dto.SectionType = entity.WellSectionType.Caption; + return dto; + } + + 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 entity = item.Adapt(); + 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(); + } + + public Task> InsertRangeAsync(IEnumerable newItems, int idWell, CancellationToken token = default) { throw new NotImplementedException(); } + 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 entity = item.Adapt(); + 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(); + } + + 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); + } + public Task DeleteAsync(IEnumerable ids, CancellationToken token = default) { throw new NotImplementedException(); diff --git a/AsbCloudWebApi/Controllers/WellSectionController.cs b/AsbCloudWebApi/Controllers/WellSectionController.cs index 7767d4ae..f7bf1dc8 100644 --- a/AsbCloudWebApi/Controllers/WellSectionController.cs +++ b/AsbCloudWebApi/Controllers/WellSectionController.cs @@ -27,17 +27,25 @@ namespace AsbCloudWebApi.Controllers return Ok(result); } - [HttpPost] - public async Task Insert([FromBody] WellSectionDto value, CancellationToken token = default) + [HttpGet] + [Route("{idSection}")] + public async Task 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 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 Put(int id, [FromBody] WellSectionDto value, CancellationToken token = default) + public async Task 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); } diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index cb075929..224d7bf8 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -9,9 +9,6 @@ using System; namespace ConsoleApp1 { - - - class Program {