This commit is contained in:
2016-10-23 01:31:09 +02:00
parent 1b89e65ee0
commit 690a5f8ee4
17 changed files with 2143 additions and 25 deletions

View File

@ -5,6 +5,7 @@ using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Logging;
using Yavsc.Models;
using Yavsc.Models.Billing;
@ -15,10 +16,11 @@ namespace Yavsc.Controllers
public class EstimateApiController : Controller
{
private ApplicationDbContext _context;
public EstimateApiController(ApplicationDbContext context)
private ILogger _logger;
public EstimateApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
{
_context = context;
_logger = loggerFactory.CreateLogger<EstimateApiController>();
}
bool UserIsAdminOrThis(string uid)
{
@ -39,7 +41,7 @@ namespace Yavsc.Controllers
else if (!UserIsAdminOrThis(ownerId)) // throw new Exception("Not authorized") ;
// or just do nothing
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
return Ok(_context.Estimates.Where(e=>e.OwnerId == ownerId));
return Ok(_context.Estimates.Include(e=>e.Bill).Where(e=>e.OwnerId == ownerId));
}
// GET: api/Estimate/5
[HttpGet("{id}", Name = "GetEstimate")]
@ -50,7 +52,7 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
Estimate estimate = _context.Estimates.Single(m => m.Id == id);
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
if (estimate == null)
{
@ -84,9 +86,9 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
}
var entry = _context.Attach(estimate);
estimate.LatestValidationDate = DateTime.Now;
_context.Entry(estimate).State = EntityState.Modified;
try
{
_context.SaveChanges();
@ -103,7 +105,7 @@ namespace Yavsc.Controllers
}
}
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
return Ok( new { Id = estimate.Id, LatestValidationDate = estimate.LatestValidationDate });
}
// POST: api/Estimate
@ -125,7 +127,13 @@ namespace Yavsc.Controllers
}
estimate.LatestValidationDate = DateTime.Now;
_context.Estimates.Add(estimate);
foreach (var l in estimate.Bill) _context.Attach<CommandLine>(l);
/* _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);
*/
try
{
_context.SaveChanges();
@ -141,8 +149,7 @@ namespace Yavsc.Controllers
throw;
}
}
return CreatedAtRoute("GetEstimate", new { Id = estimate.Id }, estimate);
return Ok( new { Id = estimate.Id, Bill = estimate.Bill , LatestValidationDate = estimate.LatestValidationDate });
}
// DELETE: api/Estimate/5
@ -154,7 +161,7 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
Estimate estimate = _context.Estimates.Single(m => m.Id == id);
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
if (estimate == null)
{