CS2-43: Контроллер и сервис для 'Последних данных'

This commit is contained in:
KharchenkoVV 2021-08-02 11:39:39 +05:00
parent 77f1f54c30
commit 4f703fb53e
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,8 @@
namespace AsbCloudApp.Services
{
public interface ILastDataService<Tdto>
{
Tdto Get(int idWell, int idCategory);
int Upsert(int idCategory, Tdto value);
}
}

View 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();
}
}
}

View 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();
}
}
}