diff --git a/AsbCloudApp/Services/ILastDataService.cs b/AsbCloudApp/Services/ILastDataService.cs new file mode 100644 index 00000000..036bd3b9 --- /dev/null +++ b/AsbCloudApp/Services/ILastDataService.cs @@ -0,0 +1,8 @@ +namespace AsbCloudApp.Services +{ + public interface ILastDataService + { + Tdto Get(int idWell, int idCategory); + int Upsert(int idCategory, Tdto value); + } +} diff --git a/AsbCloudInfrastructure/Services/LastDataService.cs b/AsbCloudInfrastructure/Services/LastDataService.cs new file mode 100644 index 00000000..6cf4cdaa --- /dev/null +++ b/AsbCloudInfrastructure/Services/LastDataService.cs @@ -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 : ILastDataService + 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(); + var entity = dbSet.FirstOrDefault(e => + e.IdWell == idWell && e.IdCategory == idCategory); + + var dto = entity.Adapt(); + return dto; + } + + public int Upsert(int idCategory, Tdto value) + { + //var dbSet = context.Set(); + //var dbSet = db.LastData; + var model = value.Adapt(); + dbSet.Update(model); + return context.SaveChanges(); + } + } + +} diff --git a/AsbCloudWebApi/Controllers/CrudController.cs b/AsbCloudWebApi/Controllers/CrudController.cs new file mode 100644 index 00000000..3f19871f --- /dev/null +++ b/AsbCloudWebApi/Controllers/CrudController.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using AsbCloudApp.Services; + +namespace AsbCloudWebApi.Controllers +{ + [ApiController] + [Authorize] + public abstract class CrudController : ControllerBase where T : class + { + private readonly ILastDataService lastDataService; + private readonly IWellService wellService; + + public CrudController(ILastDataService 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(); + } + } +}