forked from ddrilling/AsbCloudServer
CS2-43: Контроллер и сервис для 'Последних данных'
This commit is contained in:
parent
77f1f54c30
commit
4f703fb53e
8
AsbCloudApp/Services/ILastDataService.cs
Normal file
8
AsbCloudApp/Services/ILastDataService.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace AsbCloudApp.Services
|
||||||
|
{
|
||||||
|
public interface ILastDataService<Tdto>
|
||||||
|
{
|
||||||
|
Tdto Get(int idWell, int idCategory);
|
||||||
|
int Upsert(int idCategory, Tdto value);
|
||||||
|
}
|
||||||
|
}
|
44
AsbCloudInfrastructure/Services/LastDataService.cs
Normal file
44
AsbCloudInfrastructure/Services/LastDataService.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
|
using Mapster;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Services
|
||||||
|
{
|
||||||
|
public class LastDataService<Tdto, TModel> : ILastDataService<Tdto>
|
||||||
|
where TModel : class, IIdWellCategory
|
||||||
|
{
|
||||||
|
private readonly DbContext context;
|
||||||
|
private readonly IAsbCloudDbContext db;
|
||||||
|
|
||||||
|
//private Dictioanary dict =
|
||||||
|
|
||||||
|
public LastDataService(IConfiguration configuration, IAsbCloudDbContext db)
|
||||||
|
{
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tdto Get(int idWell, int idCategory)
|
||||||
|
{
|
||||||
|
var dbSet = context.Set<TModel>();
|
||||||
|
var entity = dbSet.FirstOrDefault(e =>
|
||||||
|
e.IdWell == idWell && e.IdCategory == idCategory);
|
||||||
|
|
||||||
|
var dto = entity.Adapt<Tdto>();
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Upsert(int idCategory, Tdto value)
|
||||||
|
{
|
||||||
|
//var dbSet = context.Set<TModel>();
|
||||||
|
//var dbSet = db.LastData;
|
||||||
|
var model = value.Adapt<TModel>();
|
||||||
|
dbSet.Update(model);
|
||||||
|
return context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
AsbCloudWebApi/Controllers/CrudController.cs
Normal file
44
AsbCloudWebApi/Controllers/CrudController.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Authorize]
|
||||||
|
public abstract class CrudController<T> : ControllerBase where T : class
|
||||||
|
{
|
||||||
|
private readonly ILastDataService<T> lastDataService;
|
||||||
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
|
public CrudController(ILastDataService<T> lastDataService, IWellService wellService)
|
||||||
|
{
|
||||||
|
this.lastDataService = lastDataService;
|
||||||
|
this.wellService = wellService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public IActionResult Get([FromRoute] int idWell, [FromQuery] int idCategory)
|
||||||
|
{
|
||||||
|
int? idCompany = User.GetCompanyId();
|
||||||
|
|
||||||
|
if (idCompany is null || !wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
|
||||||
|
return Forbid();
|
||||||
|
|
||||||
|
lastDataService.Get(idWell, idCategory);
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult Put([FromRoute] int idWell, [FromQuery] int idCategory, [FromForm] T data)
|
||||||
|
{
|
||||||
|
int? idCompany = User.GetCompanyId();
|
||||||
|
|
||||||
|
if (idCompany is null || !wellService.IsCompanyInvolvedInWell((int)idCompany, idWell))
|
||||||
|
return Forbid();
|
||||||
|
|
||||||
|
lastDataService.Upsert(idCategory, data);
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user