yavsc/src/Yavsc.Api/Controllers/Business/EstimateApiController.cs

218 lines
7.1 KiB
C#
Raw Normal View History

2019-01-01 16:28:47 +00:00
using System;
using System.Linq;
using System.Security.Claims;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2019-01-01 16:28:47 +00:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
2023-03-19 17:57:55 +00:00
using Yavsc.Helpers;
2019-01-01 16:28:47 +00:00
using Yavsc.Models;
using Yavsc.Models.Billing;
using Yavsc.Server.Helpers;
2019-01-01 16:28:47 +00:00
namespace Yavsc.Controllers
{
[Produces("application/json")]
2026-08-20 20:50:52 +01:00
[Route(Constants.APIPrefix + "/estimate"), Authorize]
2019-01-01 16:28:47 +00:00
public class EstimateApiController : Controller
{
2020-10-09 19:35:39 +01:00
private readonly ApplicationDbContext _context;
private readonly ILogger _logger;
2019-01-01 16:28:47 +00:00
public EstimateApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
{
_context = context;
_logger = loggerFactory.CreateLogger<EstimateApiController>();
}
bool UserIsAdminOrThis(string uid)
{
2026-08-20 20:50:52 +01:00
if (User.IsInRole(Constants.AdminGroupName)) return true;
2019-01-01 16:28:47 +00:00
return uid == User.GetUserId();
}
2020-10-09 19:35:39 +01:00
bool UserIsAdminOrInThese(string oid, string uid)
2019-01-01 16:28:47 +00:00
{
2026-08-20 20:50:52 +01:00
if (User.IsInRole(Constants.AdminGroupName)) return true;
2019-01-01 16:28:47 +00:00
var cuid = User.GetUserId();
2020-10-09 19:35:39 +01:00
return cuid == uid || cuid == oid;
2019-01-01 16:28:47 +00:00
}
// GET: api/Estimate{?ownerId=User.GetUserId()}
[HttpGet]
2025-07-07 07:49:18 +01:00
public IActionResult GetEstimates(string? ownerId = null)
2019-01-01 16:28:47 +00:00
{
2020-10-09 19:35:39 +01:00
if (ownerId == null) ownerId = User.GetUserId();
2019-01-01 16:28:47 +00:00
else if (!UserIsAdminOrThis(ownerId)) // throw new Exception("Not authorized") ;
2020-10-09 19:35:39 +01:00
// or just do nothing
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status403Forbidden);
2020-10-09 19:35:39 +01:00
return Ok(_context.Estimates.Include(e => e.Bill).Where(e => e.OwnerId == ownerId));
2019-01-01 16:28:47 +00:00
}
// GET: api/Estimate/5
[HttpGet("{id}", Name = "GetEstimate")]
public IActionResult GetEstimate([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2020-10-09 19:35:39 +01:00
Estimate estimate = _context.Estimates.Include(e => e.Bill).Single(m => m.Id == id);
2019-01-01 16:28:47 +00:00
if (estimate == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
2020-10-09 19:35:39 +01:00
if (UserIsAdminOrInThese(estimate.ClientId, estimate.OwnerId))
return Ok(estimate);
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status403Forbidden);
2019-01-01 16:28:47 +00:00
}
// PUT: api/Estimate/5
2020-10-09 19:35:39 +01:00
[HttpPut("{id}"), Produces("application/json")]
2019-01-01 16:28:47 +00:00
public IActionResult PutEstimate(long id, [FromBody] Estimate estimate)
{
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
if (id != estimate.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-01-01 16:28:47 +00:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2026-08-20 20:50:52 +01:00
if (!User.IsInRole(Constants.AdminGroupName))
2019-01-01 16:28:47 +00:00
{
if (uid != estimate.OwnerId)
{
2020-10-09 19:35:39 +01:00
ModelState.AddModelError("OwnerId", "You can only modify your own estimates");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
}
2020-10-09 19:35:39 +01:00
2019-01-01 16:28:47 +00:00
var entry = _context.Attach(estimate);
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateConcurrencyException)
{
if (!EstimateExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
2020-10-09 19:35:39 +01:00
return Ok(new { estimate.Id });
2019-01-01 16:28:47 +00:00
}
// POST: api/Estimate
2020-10-09 19:35:39 +01:00
[HttpPost, Produces("application/json")]
2019-01-01 16:28:47 +00:00
public IActionResult PostEstimate([FromBody] Estimate estimate)
{
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2020-10-09 19:35:39 +01:00
if (estimate.OwnerId == null) estimate.OwnerId = uid;
2026-08-20 20:50:52 +01:00
if (!User.IsInRole(Constants.AdminGroupName))
2020-10-09 19:35:39 +01:00
{
2019-01-01 16:28:47 +00:00
if (uid != estimate.OwnerId)
{
2020-10-09 19:35:39 +01:00
ModelState.AddModelError("OwnerId", "You can only create your own estimates");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
}
2020-10-09 19:35:39 +01:00
if (estimate.CommandId != null)
{
2019-01-01 16:28:47 +00:00
var query = _context.RdvQueries.FirstOrDefault(q => q.Id == estimate.CommandId);
2020-10-09 19:35:39 +01:00
if (query == null)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2026-07-11 03:26:13 +01:00
query.ValidationDate = DateTime.UtcNow;
2019-01-01 16:28:47 +00:00
_context.SaveChanges(User.GetUserId());
_context.Entry(query).State = EntityState.Detached;
}
if (!ModelState.IsValid)
{
_logger.LogError(JsonConvert.SerializeObject(ModelState));
2020-10-09 19:35:39 +01:00
return Json(ModelState);
2019-01-01 16:28:47 +00:00
}
_context.Estimates.Add(estimate);
2020-10-09 19:35:39 +01:00
/* _context.AttachRange(estimate.Bill);
_context.Attach(estimate);
_context.Entry(estimate).State = EntityState.Added;
foreach (var line in estimate.Bill)
_context.Entry(line).State = EntityState.Added;
// foreach (var l in estimate.Bill) _context.Attach<CommandLine>(l);
*/
2019-01-01 16:28:47 +00:00
try
{
_context.SaveChanges(User.GetUserId());
}
catch (DbUpdateException)
{
if (EstimateExists(estimate.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-01-01 16:28:47 +00:00
}
else
{
throw;
}
}
2020-10-09 19:35:39 +01:00
return Ok(new { estimate.Id, estimate.Bill });
2019-01-01 16:28:47 +00:00
}
// DELETE: api/Estimate/5
[HttpDelete("{id}")]
public IActionResult DeleteEstimate(long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
2020-10-09 19:35:39 +01:00
Estimate estimate = _context.Estimates.Include(e => e.Bill).Single(m => m.Id == id);
2019-01-01 16:28:47 +00:00
if (estimate == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-01-01 16:28:47 +00:00
}
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2026-08-20 20:50:52 +01:00
if (!User.IsInRole(Constants.AdminGroupName))
2019-01-01 16:28:47 +00:00
{
if (uid != estimate.OwnerId)
{
2020-10-09 19:35:39 +01:00
ModelState.AddModelError("OwnerId", "You can only create your own estimates");
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-01-01 16:28:47 +00:00
}
}
_context.Estimates.Remove(estimate);
_context.SaveChanges(User.GetUserId());
return Ok(estimate);
}
2020-10-09 19:35:39 +01:00
protected override void Dispose(bool disposing)
2019-01-01 16:28:47 +00:00
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool EstimateExists(long id)
{
return _context.Estimates.Count(e => e.Id == id) > 0;
}
}
2020-10-09 19:35:39 +01:00
}