diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index 33fc5b81..b107cca3 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -8,7 +8,6 @@ namespace AsbCloudApp.Services IEnumerable GetWellsByCompany(int idCompany); IEnumerable GetTransmittingWells(int idCompany); bool IsCompanyInvolvedInWell(int idCompany, int idWell); - IEnumerable GetSections(int idWell); IEnumerable GetOperations(int idWell); } } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index cf7d964f..64a4c7e9 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -36,15 +36,13 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - + services.AddTransient(); services.AddTransient, LastDataService>(); services.AddTransient, LastDataService>(); services.AddTransient, LastDataService>(); - - + return services; } - } } diff --git a/AsbCloudInfrastructure/Services/CrudService.cs b/AsbCloudInfrastructure/Services/CrudService.cs deleted file mode 100644 index 9d3f7e81..00000000 --- a/AsbCloudInfrastructure/Services/CrudService.cs +++ /dev/null @@ -1,80 +0,0 @@ -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Mapster; -using Microsoft.EntityFrameworkCore; -using System.Collections.Generic; -using System.Linq; - -namespace AsbCloudInfrastructure.Services -{ - public class CrudService : ICrudService - where TModel : class, AsbCloudDb.Model.IId - where Tdto : AsbCloudApp.Data.IId - { - private readonly IAsbCloudDbContext context; - private readonly DbSet dbSet; - - public CrudService(IAsbCloudDbContext context) - { - this.context = context; - dbSet = context.Set(); - } - - public IEnumerable GetAll(System.Linq.Expressions.Expression> predicate = null) - { - IQueryable entities = dbSet; - - if (predicate is not null) - entities = entities.Where(predicate).Cast(); - - var dto = entities.Adapt(); - return dto; - } - - public Tdto Get(int id) - { - var entity = dbSet.FirstOrDefault(e => e.Id == id); - - var dto = entity.Adapt(); - return dto; - } - - public Tdto Insert(Tdto newItem) - { - var newEntity = newItem.Adapt(); - var dbEntity = dbSet.Add(newEntity); - context.SaveChanges(); - return dbEntity.Entity.Adapt(); - } - - public IEnumerable InsertRange(IEnumerable newItems) - { - var newEntities = newItems.Adapt>(); - var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry[newItems.Count()]; - - for (int i = 0; i < dbEntities.Length; i++) - dbEntities[i] = dbSet.Add(newEntities.ElementAt(i)); - - context.SaveChanges(); - return dbEntities.Select(e => e.Entity.Adapt()); - } - - public Tdto Update(Tdto item) - { - var newEntity = item.Adapt(); - var dbEntity = dbSet.Update(newEntity); - context.SaveChanges(); - return dbEntity.Entity.Adapt(); - } - - public int Delete(int id) - { - var entity = dbSet.FirstOrDefault(e => e.Id == id); - if (entity == default) - return 0; - dbSet.Remove(entity); - return context.SaveChanges(); - } - } - -} diff --git a/AsbCloudInfrastructure/Services/CrudServiceBase.cs b/AsbCloudInfrastructure/Services/CrudServiceBase.cs new file mode 100644 index 00000000..4cfab609 --- /dev/null +++ b/AsbCloudInfrastructure/Services/CrudServiceBase.cs @@ -0,0 +1,78 @@ +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Services +{ + public class CrudServiceBase : ICrudService + where TModel : class, AsbCloudDb.Model.IId + where Tdto : AsbCloudApp.Data.IId + { + protected readonly IAsbCloudDbContext context; + protected readonly DbSet dbSet; + + public CrudServiceBase(IAsbCloudDbContext context) + { + this.context = context; + dbSet = context.Set(); + } + + public virtual async Task GetAsync(int id, CancellationToken token = default) + { + var entity = await dbSet.FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false); + var dto = entity.Adapt(); + return dto; + } + + public virtual async Task InsertAsync(Tdto newItem, CancellationToken token = default) + { + var newEntity = newItem.Adapt(); + var dbEntity = dbSet.Add(newEntity); + await context.SaveChangesAsync(token).ConfigureAwait(false); + return dbEntity.Entity.Adapt(); + } + + public virtual async Task> InsertRangeAsync(IEnumerable newItems, CancellationToken token = default) + { + var newEntities = newItems.Adapt>(); + var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry[newItems.Count()]; + + for (int i = 0; i < dbEntities.Length; i++) + dbEntities[i] = dbSet.Add(newEntities.ElementAt(i)); + + await context.SaveChangesAsync(token).ConfigureAwait(false); + return dbEntities.Select(e => e.Entity.Adapt()); + } + + public virtual async Task UpdateAsync(Tdto item, CancellationToken token = default) + { + var newEntity = item.Adapt(); + var dbEntity = dbSet.Update(newEntity); + await context.SaveChangesAsync(token).ConfigureAwait(false); + return dbEntity.Entity.Adapt(); + } + + public virtual 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 virtual Task DeleteAsync(IEnumerable ids, CancellationToken token = default) + { + var entities = dbSet.Where(e => ids.Contains(e.Id)); + if (entities == default) + return Task.FromResult(0); + dbSet.RemoveRange(entities); + return context.SaveChangesAsync(token); + } + } +} diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index ffebb274..908f1f1a 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Services if (!events.Any()) return null; - var messages = db.Messages.Where(m => m.IdTelemetry == telemetryId); + var query = db.Messages.Where(m => m.IdTelemetry == telemetryId); if ((categoryids?.Any() == true) || !string.IsNullOrEmpty(searchString)) { @@ -58,28 +58,28 @@ namespace AsbCloudInfrastructure.Services if (!eventIds.Any()) return null; - messages = messages.Where(m => eventIds.Contains(m.IdEvent)); + query = query.Where(m => eventIds.Contains(m.IdEvent)); } - messages = messages.OrderByDescending(m => m.Date); + query = query.OrderByDescending(m => m.Date); if (begin != default) - messages = messages.Where(m => m.Date >= begin); + query = query.Where(m => m.Date >= begin); if (end != default) - messages = messages.Where(m => m.Date <= end); + query = query.Where(m => m.Date <= end); var result = new PaginationContainer { Skip = skip, Take = take, - Count = messages.Count() + Count = query.Count() }; if (skip > 0) - messages = messages.Skip(skip); + query = query.Skip(skip); - var messagesList = messages.Take(take).ToList(); + var messagesList = query.Take(take).ToList(); if (messagesList.Count == 0) return result; diff --git a/AsbCloudInfrastructure/Services/WellSectionService.cs b/AsbCloudInfrastructure/Services/WellSectionService.cs new file mode 100644 index 00000000..2d237738 --- /dev/null +++ b/AsbCloudInfrastructure/Services/WellSectionService.cs @@ -0,0 +1,88 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; +using System.Threading; + +namespace AsbCloudInfrastructure.Services +{ + public class WellSectionService: IWellSectionService + { + private readonly IAsbCloudDbContext context; + private readonly DbSet dbSet; + + public WellSectionService(IAsbCloudDbContext context) + { + 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(); + } + + public async Task> GetAllByWellIdAsync(int idWell, int skip, int take, CancellationToken token = default) + { + var query = dbSet + .Include(s => s.WellSectionType) + .Where(s => s.IdWell == idWell) + .AsNoTracking(); + + var result = new PaginationContainer + { + Skip = skip, + Take = take, + Count = await query.CountAsync(token).ConfigureAwait(false), + }; + + query = query + .OrderBy(e => e.WellDepthPlan); + + if (skip > 0) + query = query.Skip(skip); + + query = query.Take(take); + + var entities = await query.Take(take).ToListAsync(token).ConfigureAwait(false); + + foreach (var item in entities) + result.Items.Add(item.Adapt()); + + return result; + } + + public Task DeleteAsync(int id, CancellationToken token = default) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(IEnumerable ids, CancellationToken token = default) + { + throw new NotImplementedException(); + } + } +} diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index f198f38f..42b79983 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -44,19 +44,6 @@ namespace AsbCloudInfrastructure.Services public bool IsCompanyInvolvedInWell(int idCompany, int idWell) => cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany); - public IEnumerable GetSections(int idWell) - { - var entities = db - .WellSections - .Where(s => s.IdWell == idWell) - .ToList(); - - var dtos = entities.Adapt( - (s, d) => { d.SectionType = s.WellSectionType.Caption; }); - - return dtos; - } - public IEnumerable GetOperations(int idWell) { var entities = db diff --git a/AsbCloudWebApi/Controllers/CrudController.cs b/AsbCloudWebApi/Controllers/CrudController.cs index ed05edc3..1b54b829 100644 --- a/AsbCloudWebApi/Controllers/CrudController.cs +++ b/AsbCloudWebApi/Controllers/CrudController.cs @@ -1,68 +1,54 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using Microsoft.AspNetCore.Mvc; +using System.Threading; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace AsbCloudWebApi.Controllers { [ApiController] - public abstract class CrudController : ControllerBase + public abstract class CrudController : ControllerBase where T : IId + where TService: ICrudService { - protected readonly ICrudService service; + protected readonly TService service; - public CrudController(ICrudService service) + public CrudController(TService service) { this.service = service; } - // GET: api/ - //[HttpGet] - //public virtual IActionResult GetAll() - //{ - // var result = service.GetAll(); - // return Ok(result); - //} - // GET api//5 [HttpGet("{id}")] - public virtual IActionResult Get(int id) + public virtual IActionResult Get(int id, CancellationToken token = default) { - var result = service.Get(id); + var result = service.GetAsync(id, token).ConfigureAwait(false); return Ok(result); } // POST api/ [HttpPost] - public virtual IActionResult Insert([FromBody] T value) + public virtual IActionResult Insert([FromBody] T value, CancellationToken token = default) { - var result = service.Insert(value); + var result = service.InsertAsync(value, token).ConfigureAwait(false); return Ok(result); } - //[HttpPost] - //[Route("Range/")] - //public virtual IActionResult InsertRange([FromBody] IEnumerable value) - //{ - // var result = service.InsertRange(value); - // return Ok(result); - //} - // PUT api//5 [HttpPut("{id}")] - public virtual IActionResult Put(int id, [FromBody] T value) + public virtual IActionResult Put(int id, [FromBody] T value, CancellationToken token = default) { - var result = service.Update(value); + var result = service.UpdateAsync(value, token).ConfigureAwait(false); return Ok(result); } // DELETE api//5 [HttpDelete("{id}")] - public virtual IActionResult Delete(int id) + public virtual IActionResult Delete(int id, CancellationToken token = default) { - var result = service.Delete(id); + var result = service.DeleteAsync(id, token).ConfigureAwait(false); return Ok(result); } } diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 42e9c1fd..8b918f97 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -38,23 +38,6 @@ namespace AsbCloudWebApi.Controllers return Ok(wells); } - [HttpGet("{idWell}/sections")] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public IActionResult GetSections(int idWell) - { - var idCompany = User.GetCompanyId(); - - if (idCompany is null) - return NoContent(); - - if (!wellService.IsCompanyInvolvedInWell((int)idCompany, idWell)) - return Forbid(); - - var dto = wellService.GetSections(idWell); - - return Ok(dto); - } - [HttpGet("{idWell}/operations")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public IActionResult GetOperations(int idWell) diff --git a/AsbCloudWebApi/Controllers/WellSectionController.cs b/AsbCloudWebApi/Controllers/WellSectionController.cs index 65ebb3dc..7767d4ae 100644 --- a/AsbCloudWebApi/Controllers/WellSectionController.cs +++ b/AsbCloudWebApi/Controllers/WellSectionController.cs @@ -2,25 +2,49 @@ using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { - [Route("api/well/{idWell}")] + [Route("api/well/{idWell}/sections")] [ApiController] [Authorize] - public class WellSectionController : CrudController + public class WellSectionController : ControllerBase { - public WellSectionController(ICrudService service) - : base(service) - { + private readonly IWellSectionService service; + public WellSectionController(IWellSectionService service) + { + this.service = service; } [HttpGet] [Route("")] - public virtual IActionResult GetAll() + public async Task GetAllAsync(int idWell, int skip = 0, int take = 32, CancellationToken token = default) { - var result = service.GetAll(); + var result = await service.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false); + return Ok(result); + } + + [HttpPost] + public async Task Insert([FromBody] WellSectionDto value, CancellationToken token = default) + { + var result = await service.InsertAsync(value, token).ConfigureAwait(false); + return Ok(result); + } + + [HttpPut("{id}")] + public async Task Put(int id, [FromBody] WellSectionDto value, CancellationToken token = default) + { + var result = await service.UpdateAsync(value, token).ConfigureAwait(false); + return Ok(result); + } + + [HttpDelete("{id}")] + public async Task Delete(int id, CancellationToken token = default) + { + var result = await service.DeleteAsync(id, token).ConfigureAwait(false); return Ok(result); } }