implement async WellSection controller

This commit is contained in:
Фролов 2021-08-10 14:36:35 +05:00
parent ddbf3ff2d5
commit e96a9820db
10 changed files with 220 additions and 157 deletions

View File

@ -8,7 +8,6 @@ namespace AsbCloudApp.Services
IEnumerable<WellDto> GetWellsByCompany(int idCompany); IEnumerable<WellDto> GetWellsByCompany(int idCompany);
IEnumerable<WellDto> GetTransmittingWells(int idCompany); IEnumerable<WellDto> GetTransmittingWells(int idCompany);
bool IsCompanyInvolvedInWell(int idCompany, int idWell); bool IsCompanyInvolvedInWell(int idCompany, int idWell);
IEnumerable<WellSectionDto> GetSections(int idWell);
IEnumerable<WellOperationDto> GetOperations(int idWell); IEnumerable<WellOperationDto> GetOperations(int idWell);
} }
} }

View File

@ -36,15 +36,13 @@ namespace AsbCloudInfrastructure
services.AddTransient<IReportService, ReportService>(); services.AddTransient<IReportService, ReportService>();
services.AddTransient<IAnalyticsService, AnalyticsService>(); services.AddTransient<IAnalyticsService, AnalyticsService>();
services.AddTransient<IFileService, FileService>(); services.AddTransient<IFileService, FileService>();
services.AddTransient<IWellSectionService, WellSectionService>();
services.AddTransient<ILastDataService<FluidDataDto>, LastDataService<FluidDataDto, FluidData>>(); services.AddTransient<ILastDataService<FluidDataDto>, LastDataService<FluidDataDto, FluidData>>();
services.AddTransient<ILastDataService<MudDiagramDataDto>, LastDataService<MudDiagramDataDto, MudDiagramData>>(); services.AddTransient<ILastDataService<MudDiagramDataDto>, LastDataService<MudDiagramDataDto, MudDiagramData>>();
services.AddTransient<ILastDataService<NnbDataDto>, LastDataService<NnbDataDto, NnbData>>(); services.AddTransient<ILastDataService<NnbDataDto>, LastDataService<NnbDataDto, NnbData>>();
return services; return services;
} }
} }
} }

View File

@ -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<Tdto, TModel> : ICrudService<Tdto>
where TModel : class, AsbCloudDb.Model.IId
where Tdto : AsbCloudApp.Data.IId
{
private readonly IAsbCloudDbContext context;
private readonly DbSet<TModel> dbSet;
public CrudService(IAsbCloudDbContext context)
{
this.context = context;
dbSet = context.Set<TModel>();
}
public IEnumerable<Tdto> GetAll(System.Linq.Expressions.Expression<System.Func<object, bool>> predicate = null)
{
IQueryable<TModel> entities = dbSet;
if (predicate is not null)
entities = entities.Where(predicate).Cast<TModel>();
var dto = entities.Adapt<Tdto>();
return dto;
}
public Tdto Get(int id)
{
var entity = dbSet.FirstOrDefault(e => e.Id == id);
var dto = entity.Adapt<Tdto>();
return dto;
}
public Tdto Insert(Tdto newItem)
{
var newEntity = newItem.Adapt<TModel>();
var dbEntity = dbSet.Add(newEntity);
context.SaveChanges();
return dbEntity.Entity.Adapt<Tdto>();
}
public IEnumerable<Tdto> InsertRange(IEnumerable<Tdto> newItems)
{
var newEntities = newItems.Adapt<IEnumerable<TModel>>();
var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TModel>[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<Tdto>());
}
public Tdto Update(Tdto item)
{
var newEntity = item.Adapt<TModel>();
var dbEntity = dbSet.Update(newEntity);
context.SaveChanges();
return dbEntity.Entity.Adapt<Tdto>();
}
public int Delete(int id)
{
var entity = dbSet.FirstOrDefault(e => e.Id == id);
if (entity == default)
return 0;
dbSet.Remove(entity);
return context.SaveChanges();
}
}
}

View File

@ -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<Tdto, TModel> : ICrudService<Tdto>
where TModel : class, AsbCloudDb.Model.IId
where Tdto : AsbCloudApp.Data.IId
{
protected readonly IAsbCloudDbContext context;
protected readonly DbSet<TModel> dbSet;
public CrudServiceBase(IAsbCloudDbContext context)
{
this.context = context;
dbSet = context.Set<TModel>();
}
public virtual async Task<Tdto> GetAsync(int id, CancellationToken token = default)
{
var entity = await dbSet.FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false);
var dto = entity.Adapt<Tdto>();
return dto;
}
public virtual async Task<Tdto> InsertAsync(Tdto newItem, CancellationToken token = default)
{
var newEntity = newItem.Adapt<TModel>();
var dbEntity = dbSet.Add(newEntity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
return dbEntity.Entity.Adapt<Tdto>();
}
public virtual async Task<IEnumerable<Tdto>> InsertRangeAsync(IEnumerable<Tdto> newItems, CancellationToken token = default)
{
var newEntities = newItems.Adapt<IEnumerable<TModel>>();
var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TModel>[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<Tdto>());
}
public virtual async Task<Tdto> UpdateAsync(Tdto item, CancellationToken token = default)
{
var newEntity = item.Adapt<TModel>();
var dbEntity = dbSet.Update(newEntity);
await context.SaveChangesAsync(token).ConfigureAwait(false);
return dbEntity.Entity.Adapt<Tdto>();
}
public virtual 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 virtual Task<int> DeleteAsync(IEnumerable<int> 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);
}
}
}

View File

@ -43,7 +43,7 @@ namespace AsbCloudInfrastructure.Services
if (!events.Any()) if (!events.Any())
return null; 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)) if ((categoryids?.Any() == true) || !string.IsNullOrEmpty(searchString))
{ {
@ -58,28 +58,28 @@ namespace AsbCloudInfrastructure.Services
if (!eventIds.Any()) if (!eventIds.Any())
return null; 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) if (begin != default)
messages = messages.Where(m => m.Date >= begin); query = query.Where(m => m.Date >= begin);
if (end != default) if (end != default)
messages = messages.Where(m => m.Date <= end); query = query.Where(m => m.Date <= end);
var result = new PaginationContainer<MessageDto> var result = new PaginationContainer<MessageDto>
{ {
Skip = skip, Skip = skip,
Take = take, Take = take,
Count = messages.Count() Count = query.Count()
}; };
if (skip > 0) 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) if (messagesList.Count == 0)
return result; return result;

View File

@ -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<WellSection> dbSet;
public WellSectionService(IAsbCloudDbContext context)
{
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();
}
public async Task<PaginationContainer<WellSectionDto>> 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<WellSectionDto>
{
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<WellSectionDto>());
return result;
}
public Task<int> DeleteAsync(int id, CancellationToken token = default)
{
throw new NotImplementedException();
}
public Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
{
throw new NotImplementedException();
}
}
}

View File

@ -44,19 +44,6 @@ namespace AsbCloudInfrastructure.Services
public bool IsCompanyInvolvedInWell(int idCompany, int idWell) public bool IsCompanyInvolvedInWell(int idCompany, int idWell)
=> cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany); => cacheRelationCompaniesWells.Contains(r => r.IdWell == idWell && r.IdCompany == idCompany);
public IEnumerable<WellSectionDto> GetSections(int idWell)
{
var entities = db
.WellSections
.Where(s => s.IdWell == idWell)
.ToList();
var dtos = entities.Adapt<WellSection, WellSectionDto>(
(s, d) => { d.SectionType = s.WellSectionType.Caption; });
return dtos;
}
public IEnumerable<WellOperationDto> GetOperations(int idWell) public IEnumerable<WellOperationDto> GetOperations(int idWell)
{ {
var entities = db var entities = db

View File

@ -1,68 +1,54 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc; 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 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace AsbCloudWebApi.Controllers namespace AsbCloudWebApi.Controllers
{ {
[ApiController] [ApiController]
public abstract class CrudController<T> : ControllerBase public abstract class CrudController<T, TService> : ControllerBase
where T : IId where T : IId
where TService: ICrudService<T>
{ {
protected readonly ICrudService<T> service; protected readonly TService service;
public CrudController(ICrudService<T> service) public CrudController(TService service)
{ {
this.service = service; this.service = service;
} }
// GET: api/<CrudController>
//[HttpGet]
//public virtual IActionResult GetAll()
//{
// var result = service.GetAll();
// return Ok(result);
//}
// GET api/<CrudController>/5 // GET api/<CrudController>/5
[HttpGet("{id}")] [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); return Ok(result);
} }
// POST api/<CrudController> // POST api/<CrudController>
[HttpPost] [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); return Ok(result);
} }
//[HttpPost]
//[Route("Range/")]
//public virtual IActionResult InsertRange([FromBody] IEnumerable<T> value)
//{
// var result = service.InsertRange(value);
// return Ok(result);
//}
// PUT api/<CrudController>/5 // PUT api/<CrudController>/5
[HttpPut("{id}")] [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); return Ok(result);
} }
// DELETE api/<CrudController>/5 // DELETE api/<CrudController>/5
[HttpDelete("{id}")] [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); return Ok(result);
} }
} }

View File

@ -38,23 +38,6 @@ namespace AsbCloudWebApi.Controllers
return Ok(wells); return Ok(wells);
} }
[HttpGet("{idWell}/sections")]
[ProducesResponseType(typeof(IEnumerable<WellSectionDto>), (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")] [HttpGet("{idWell}/operations")]
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(IEnumerable<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetOperations(int idWell) public IActionResult GetOperations(int idWell)

View File

@ -2,25 +2,49 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers namespace AsbCloudWebApi.Controllers
{ {
[Route("api/well/{idWell}")] [Route("api/well/{idWell}/sections")]
[ApiController] [ApiController]
[Authorize] [Authorize]
public class WellSectionController : CrudController<WellSectionDto> public class WellSectionController : ControllerBase
{ {
public WellSectionController(ICrudService<WellSectionDto> service) private readonly IWellSectionService service;
: base(service)
{
public WellSectionController(IWellSectionService service)
{
this.service = service;
} }
[HttpGet] [HttpGet]
[Route("")] [Route("")]
public virtual IActionResult GetAll() public async Task<IActionResult> 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<IActionResult> Insert([FromBody] WellSectionDto value, CancellationToken token = default)
{
var result = await service.InsertAsync(value, token).ConfigureAwait(false);
return Ok(result);
}
[HttpPut("{id}")]
public async Task<IActionResult> 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<IActionResult> Delete(int id, CancellationToken token = default)
{
var result = await service.DeleteAsync(id, token).ConfigureAwait(false);
return Ok(result); return Ok(result);
} }
} }