yavsc/Yavsc/ApiControllers/EstimateApiController.cs

211 lines
7.1 KiB
C#
Raw Normal View History

2016-10-20 20:15:48 +02:00
using System;
2016-08-03 23:32:07 +02:00
using System.Linq;
2016-08-13 01:26:32 +02:00
using System.Security.Claims;
2016-08-04 00:06:59 +02:00
using Microsoft.AspNet.Authorization;
2016-08-03 23:32:07 +02:00
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
2016-10-23 01:31:09 +02:00
using Microsoft.Extensions.Logging;
2017-03-03 02:12:15 +01:00
using Newtonsoft.Json;
2016-08-03 23:32:07 +02:00
using Yavsc.Models;
using Yavsc.Models.Billing;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/estimate"),Authorize()]
2016-08-03 23:32:07 +02:00
public class EstimateApiController : Controller
{
private ApplicationDbContext _context;
2016-10-23 01:31:09 +02:00
private ILogger _logger;
public EstimateApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
2016-08-03 23:32:07 +02:00
{
_context = context;
2016-10-23 01:31:09 +02:00
_logger = loggerFactory.CreateLogger<EstimateApiController>();
2016-08-03 23:32:07 +02:00
}
2016-08-13 01:26:32 +02:00
bool UserIsAdminOrThis(string uid)
{
if (User.IsInRole(Constants.AdminGroupName)) return true;
return uid == User.GetUserId();
}
bool UserIsAdminOrInThese (string oid, string uid)
{
if (User.IsInRole(Constants.AdminGroupName)) return true;
var cuid = User.GetUserId();
return cuid == uid || cuid == oid;
}
// GET: api/Estimate{?ownerId=User.GetUserId()}
2016-08-03 23:32:07 +02:00
[HttpGet]
2016-08-13 01:26:32 +02:00
public IActionResult GetEstimates(string ownerId=null)
2016-08-03 23:32:07 +02:00
{
2016-08-13 01:26:32 +02:00
if ( ownerId == null ) ownerId = User.GetUserId();
else if (!UserIsAdminOrThis(ownerId)) // throw new Exception("Not authorized") ;
// or just do nothing
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
2016-10-23 01:31:09 +02:00
return Ok(_context.Estimates.Include(e=>e.Bill).Where(e=>e.OwnerId == ownerId));
2016-08-03 23:32:07 +02:00
}
// GET: api/Estimate/5
[HttpGet("{id}", Name = "GetEstimate")]
public IActionResult GetEstimate([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
2016-10-23 01:31:09 +02:00
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
2016-08-03 23:32:07 +02:00
if (estimate == null)
{
return HttpNotFound();
}
2016-08-13 01:26:32 +02:00
if (UserIsAdminOrInThese(estimate.ClientId,estimate.OwnerId))
2016-08-03 23:32:07 +02:00
return Ok(estimate);
2016-08-13 01:26:32 +02:00
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
2016-08-03 23:32:07 +02:00
}
// PUT: api/Estimate/5
2017-03-03 01:17:00 +01:00
[HttpPut("{id}"),Produces("application/json")]
2016-08-03 23:32:07 +02:00
public IActionResult PutEstimate(long id, [FromBody] Estimate estimate)
{
2016-11-24 01:01:41 +01:00
2016-08-03 23:32:07 +02:00
if (!ModelState.IsValid)
{
2017-03-03 01:17:00 +01:00
return new BadRequestObjectResult(ModelState);
2016-08-03 23:32:07 +02:00
}
if (id != estimate.Id)
{
return HttpBadRequest();
}
2016-08-13 01:26:32 +02:00
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only modify your own estimates");
return HttpBadRequest(ModelState);
}
}
2016-10-23 01:31:09 +02:00
var entry = _context.Attach(estimate);
2016-08-03 23:32:07 +02:00
try
{
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
}
catch (DbUpdateConcurrencyException)
{
if (!EstimateExists(id))
{
return HttpNotFound();
}
else
{
throw;
}
}
return Ok( new { Id = estimate.Id });
2016-08-03 23:32:07 +02:00
}
// POST: api/Estimate
2017-03-03 01:17:00 +01:00
[HttpPost,Produces("application/json")]
2016-08-03 23:32:07 +02:00
public IActionResult PostEstimate([FromBody] Estimate estimate)
{
2016-08-13 01:26:32 +02:00
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
if (estimate.CommandId!=null) {
2017-02-27 19:28:32 +01:00
var query = _context.RdvQueries.FirstOrDefault(q => q.Id == estimate.CommandId);
2017-03-03 02:12:15 +01:00
if (query == null) {
2017-03-03 01:17:00 +01:00
return HttpBadRequest(ModelState);
2017-03-03 02:12:15 +01:00
}
query.ValidationDate = DateTime.Now;
2017-03-03 02:12:15 +01:00
_context.SaveChanges(User.GetUserId());
_context.Entry(query).State = EntityState.Detached;
}
2017-03-03 01:17:00 +01:00
if (!ModelState.IsValid)
{
2017-03-03 02:12:15 +01:00
_logger.LogError(JsonConvert.SerializeObject(ModelState));
return Json(ModelState);
2017-03-03 01:17:00 +01:00
}
2016-08-03 23:32:07 +02:00
_context.Estimates.Add(estimate);
2016-10-23 01:31:09 +02: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);
*/
2016-08-03 23:32:07 +02:00
try
{
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
}
catch (DbUpdateException)
{
if (EstimateExists(estimate.Id))
{
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
}
else
{
throw;
}
}
return Ok( new { Id = estimate.Id, Bill = estimate.Bill });
2016-08-03 23:32:07 +02:00
}
// DELETE: api/Estimate/5
[HttpDelete("{id}")]
public IActionResult DeleteEstimate(long id)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
2016-10-23 01:31:09 +02:00
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
2016-08-13 01:26:32 +02:00
2016-08-03 23:32:07 +02:00
if (estimate == null)
{
return HttpNotFound();
}
2016-08-13 01:26:32 +02:00
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
2016-08-03 23:32:07 +02:00
_context.Estimates.Remove(estimate);
2017-02-23 03:10:30 +01:00
_context.SaveChanges(User.GetUserId());
2016-08-03 23:32:07 +02:00
return Ok(estimate);
}
2016-11-24 01:01:41 +01:00
protected override void Dispose (bool disposing)
2016-08-03 23:32:07 +02:00
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool EstimateExists(long id)
{
return _context.Estimates.Count(e => e.Id == id) > 0;
}
}
}