code guidelines

This commit is contained in:
Paul Schneider 2020-10-09 19:35:39 +01:00
commit 492427f4b8
82 changed files with 470 additions and 375 deletions

View file

@ -25,15 +25,15 @@ namespace Yavsc.ApiControllers
[Route("api/bill"), Authorize]
public class BillingController : Controller
{
ApplicationDbContext dbContext;
private IStringLocalizer _localizer;
private GoogleAuthSettings _googleSettings;
private IYavscMessageSender _GCMSender;
private IAuthorizationService authorizationService;
readonly ApplicationDbContext dbContext;
private readonly IStringLocalizer _localizer;
private readonly GoogleAuthSettings _googleSettings;
private readonly IYavscMessageSender _GCMSender;
private readonly IAuthorizationService authorizationService;
private ILogger logger;
private IBillingService billingService;
private readonly ILogger logger;
private readonly IBillingService billingService;
public BillingController(
IAuthorizationService authorizationService,

View file

@ -13,11 +13,11 @@ using Yavsc.Models.Billing;
namespace Yavsc.Controllers
{
[Produces("application/json")]
[Route("api/estimate"),Authorize()]
[Route("api/estimate"), Authorize()]
public class EstimateApiController : Controller
{
private ApplicationDbContext _context;
private ILogger _logger;
private readonly ApplicationDbContext _context;
private readonly ILogger _logger;
public EstimateApiController(ApplicationDbContext context, ILoggerFactory loggerFactory)
{
_context = context;
@ -28,21 +28,21 @@ namespace Yavsc.Controllers
if (User.IsInRole(Constants.AdminGroupName)) return true;
return uid == User.GetUserId();
}
bool UserIsAdminOrInThese (string oid, string uid)
bool UserIsAdminOrInThese(string oid, string uid)
{
if (User.IsInRole(Constants.AdminGroupName)) return true;
var cuid = User.GetUserId();
return cuid == uid || cuid == oid;
return cuid == uid || cuid == oid;
}
// GET: api/Estimate{?ownerId=User.GetUserId()}
[HttpGet]
public IActionResult GetEstimates(string ownerId=null)
public IActionResult GetEstimates(string ownerId = null)
{
if ( ownerId == null ) ownerId = User.GetUserId();
if (ownerId == null) ownerId = User.GetUserId();
else if (!UserIsAdminOrThis(ownerId)) // throw new Exception("Not authorized") ;
// or just do nothing
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
return Ok(_context.Estimates.Include(e=>e.Bill).Where(e=>e.OwnerId == ownerId));
// or just do nothing
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
return Ok(_context.Estimates.Include(e => e.Bill).Where(e => e.OwnerId == ownerId));
}
// GET: api/Estimate/5
[HttpGet("{id}", Name = "GetEstimate")]
@ -53,20 +53,20 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
Estimate estimate = _context.Estimates.Include(e => e.Bill).Single(m => m.Id == id);
if (estimate == null)
{
return HttpNotFound();
}
if (UserIsAdminOrInThese(estimate.ClientId,estimate.OwnerId))
return Ok(estimate);
if (UserIsAdminOrInThese(estimate.ClientId, estimate.OwnerId))
return Ok(estimate);
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
}
// PUT: api/Estimate/5
[HttpPut("{id}"),Produces("application/json")]
[HttpPut("{id}"), Produces("application/json")]
public IActionResult PutEstimate(long id, [FromBody] Estimate estimate)
{
@ -84,11 +84,11 @@ namespace Yavsc.Controllers
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only modify your own estimates");
ModelState.AddModelError("OwnerId", "You can only modify your own estimates");
return HttpBadRequest(ModelState);
}
}
var entry = _context.Attach(estimate);
try
{
@ -106,27 +106,30 @@ namespace Yavsc.Controllers
}
}
return Ok( new { Id = estimate.Id });
return Ok(new { estimate.Id });
}
// POST: api/Estimate
[HttpPost,Produces("application/json")]
[HttpPost, Produces("application/json")]
public IActionResult PostEstimate([FromBody] Estimate estimate)
{
var uid = User.GetUserId();
if (estimate.OwnerId==null) estimate.OwnerId = uid;
if (!User.IsInRole(Constants.AdminGroupName)) {
if (estimate.OwnerId == null) estimate.OwnerId = uid;
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
ModelState.AddModelError("OwnerId", "You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
if (estimate.CommandId!=null) {
if (estimate.CommandId != null)
{
var query = _context.RdvQueries.FirstOrDefault(q => q.Id == estimate.CommandId);
if (query == null) {
if (query == null)
{
return HttpBadRequest(ModelState);
}
query.ValidationDate = DateTime.Now;
@ -136,18 +139,18 @@ namespace Yavsc.Controllers
if (!ModelState.IsValid)
{
_logger.LogError(JsonConvert.SerializeObject(ModelState));
return Json(ModelState);
return Json(ModelState);
}
_context.Estimates.Add(estimate);
/* _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);
*/
/* _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(User.GetUserId());
@ -163,7 +166,7 @@ namespace Yavsc.Controllers
throw;
}
}
return Ok( new { Id = estimate.Id, Bill = estimate.Bill });
return Ok(new { estimate.Id, estimate.Bill });
}
// DELETE: api/Estimate/5
@ -175,8 +178,8 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
Estimate estimate = _context.Estimates.Include(e=>e.Bill).Single(m => m.Id == id);
Estimate estimate = _context.Estimates.Include(e => e.Bill).Single(m => m.Id == id);
if (estimate == null)
{
return HttpNotFound();
@ -186,7 +189,7 @@ namespace Yavsc.Controllers
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
ModelState.AddModelError("OwnerId", "You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
@ -195,8 +198,8 @@ namespace Yavsc.Controllers
return Ok(estimate);
}
protected override void Dispose (bool disposing)
protected override void Dispose(bool disposing)
{
if (disposing)
{
@ -210,4 +213,4 @@ namespace Yavsc.Controllers
return _context.Estimates.Count(e => e.Id == id) > 0;
}
}
}
}

View file

@ -9,9 +9,10 @@ using Yavsc.ViewModels.FrontOffice;
namespace Yavsc.ApiControllers
{
[Route("api/front")]
public class FrontOfficeApiController: Controller
public class FrontOfficeApiController : Controller
{
ApplicationDbContext dbContext;
private IBillingService billing;
public FrontOfficeApiController(ApplicationDbContext context, IBillingService billing)
@ -20,19 +21,19 @@ namespace Yavsc.ApiControllers
this.billing = billing;
}
[HttpGet("profiles/{actCode}")]
IEnumerable<PerformerProfileViewModel> Profiles (string actCode)
[HttpGet("profiles/{actCode}")]
IEnumerable<PerformerProfileViewModel> Profiles(string actCode)
{
return dbContext.ListPerformers(billing, actCode);
}
[HttpPost("query/reject")]
public IActionResult RejectQuery (string billingCode, long queryId)
public IActionResult RejectQuery(string billingCode, long queryId)
{
if (billingCode==null) return HttpBadRequest("billingCode");
if (queryId==0) return HttpBadRequest("queryId");
var billing = BillingService.GetBillable(dbContext, billingCode, queryId);
if (billing==null) return HttpBadRequest();
if (billingCode == null) return HttpBadRequest("billingCode");
if (queryId == 0) return HttpBadRequest("queryId");
var billing = BillingService.GetBillable(dbContext, billingCode, queryId);
if (billing == null) return HttpBadRequest();
billing.Rejected = true;
billing.RejectedAt = DateTime.Now;
dbContext.SaveChanges();

View file

@ -11,8 +11,8 @@ namespace Yavsc.ApiControllers
[Route("api/payment")]
public class PaymentApiController : Controller
{
private ApplicationDbContext dbContext;
private SiteSettings siteSettings;
private readonly ApplicationDbContext dbContext;
private readonly SiteSettings siteSettings;
private readonly ILogger _logger;
public PaymentApiController(
ApplicationDbContext dbContext,

View file

@ -16,7 +16,7 @@ namespace Yavsc.Controllers
public class PerformersApiController : Controller
{
ApplicationDbContext dbContext;
private IBillingService billing;
private readonly IBillingService billing;
public PerformersApiController(ApplicationDbContext context, IBillingService billing)
{

View file

@ -14,7 +14,7 @@ namespace Yavsc.Controllers
[Route("api/ProductApi")]
public class ProductApiController : Controller
{
private ApplicationDbContext _context;
private readonly ApplicationDbContext _context;
public ProductApiController(ApplicationDbContext context)
{
@ -146,4 +146,4 @@ namespace Yavsc.Controllers
return _context.Products.Count(e => e.Id == id) > 0;
}
}
}
}