refactoring

vnext
Paul Schneider 8 years ago
parent 3e7947b79e
commit ae32fc0ede
3 changed files with 40 additions and 35 deletions

@ -96,7 +96,7 @@ namespace Yavsc.Controllers
); );
var pro = _context.Performers.Include( var pro = _context.Performers.Include(
x => x.Performer).FirstOrDefault( x => x.Performer).FirstOrDefault(
x => x.PerfomerId == id x => x.PerformerId == id
); );
if (pro == null) if (pro == null)
return HttpNotFound(); return HttpNotFound();
@ -107,7 +107,7 @@ namespace Yavsc.Controllers
return View(new BookQuery(new Location(),DateTime.Now.AddHours(4)) return View(new BookQuery(new Location(),DateTime.Now.AddHours(4))
{ {
PerformerProfile = pro, PerformerProfile = pro,
PerformerId = pro.PerfomerId, PerformerId = pro.PerformerId,
ClientId = userid, ClientId = userid,
Client = user Client = user
}); });
@ -129,7 +129,7 @@ namespace Yavsc.Controllers
u => u.Performer u => u.Performer
).Include( u => u.Performer.Devices) ).Include( u => u.Performer.Devices)
.FirstOrDefault( .FirstOrDefault(
x => x.PerfomerId == command.PerformerId x => x.PerformerId == command.PerformerId
); );
command.PerformerProfile = pro; command.PerformerProfile = pro;
var user = await _userManager.FindByIdAsync( var user = await _userManager.FindByIdAsync(

@ -37,7 +37,7 @@ namespace Yavsc.Controllers
return HttpNotFound(); return HttpNotFound();
} }
RDVEstimate estimate = _context.Estimates Estimate estimate = _context.Estimates
.Include(e => e.Query) .Include(e => e.Query)
.Include(e => e.Query.PerformerProfile) .Include(e => e.Query.PerformerProfile)
.Include(e => e.Query.PerformerProfile.Performer) .Include(e => e.Query.PerformerProfile.Performer)
@ -61,7 +61,7 @@ namespace Yavsc.Controllers
// POST: Estimate/Create // POST: Estimate/Create
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public IActionResult Create(RDVEstimate estimate, public IActionResult Create(Estimate estimate,
ICollection<IFormFile> newGraphics, ICollection<IFormFile> newGraphics,
ICollection<IFormFile> newFiles ICollection<IFormFile> newFiles
) )
@ -75,7 +75,7 @@ namespace Yavsc.Controllers
var perfomerProfile = _context.Performers var perfomerProfile = _context.Performers
.Include( .Include(
perpr => perpr.Performer).FirstOrDefault( perpr => perpr.Performer).FirstOrDefault(
x=>x.PerfomerId == estimate.Query.PerformerId x=>x.PerformerId == estimate.Query.PerformerId
); );
var command = _context.BookQueries.FirstOrDefault( var command = _context.BookQueries.FirstOrDefault(
cmd => cmd.Id == estimate.CommandId cmd => cmd.Id == estimate.CommandId
@ -115,7 +115,7 @@ namespace Yavsc.Controllers
return HttpNotFound(); return HttpNotFound();
} }
RDVEstimate estimate = _context.Estimates.Single(m => m.Id == id); Estimate estimate = _context.Estimates.Single(m => m.Id == id);
if (estimate == null) if (estimate == null)
{ {
return HttpNotFound(); return HttpNotFound();
@ -127,7 +127,7 @@ namespace Yavsc.Controllers
// POST: Estimate/Edit/5 // POST: Estimate/Edit/5
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public IActionResult Edit(RDVEstimate estimate) public IActionResult Edit(Estimate estimate)
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
@ -147,7 +147,7 @@ namespace Yavsc.Controllers
return HttpNotFound(); return HttpNotFound();
} }
RDVEstimate estimate = _context.Estimates.Single(m => m.Id == id); Estimate estimate = _context.Estimates.Single(m => m.Id == id);
if (estimate == null) if (estimate == null)
{ {
return HttpNotFound(); return HttpNotFound();
@ -161,7 +161,7 @@ namespace Yavsc.Controllers
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public IActionResult DeleteConfirmed(long id) public IActionResult DeleteConfirmed(long id)
{ {
RDVEstimate estimate = _context.Estimates.Single(m => m.Id == id); Estimate estimate = _context.Estimates.Single(m => m.Id == id);
_context.Estimates.Remove(estimate); _context.Estimates.Remove(estimate);
_context.SaveChanges(); _context.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");

@ -102,9 +102,9 @@ namespace Yavsc.Controllers
HasDedicatedCalendar = !string.IsNullOrEmpty(user.DedicatedGoogleCalendar), HasDedicatedCalendar = !string.IsNullOrEmpty(user.DedicatedGoogleCalendar),
Roles = await _userManager.GetRolesAsync (user) Roles = await _userManager.GetRolesAsync (user)
}; };
if (_dbContext.Performers.Any(x => x.PerfomerId == user.Id)) if (_dbContext.Performers.Any(x => x.PerformerId == user.Id))
{ {
var code = _dbContext.Performers.First(x => x.PerfomerId == user.Id).ActivityCode; var code = _dbContext.Performers.First(x => x.PerformerId == user.Id).ActivityCode;
model.Activity = _dbContext.Activities.First(x => x.Code == code); model.Activity = _dbContext.Activities.First(x => x.Code == code);
} }
return View(model); return View(model);
@ -461,52 +461,57 @@ namespace Yavsc.Controllers
{ {
var user = GetCurrentUserAsync().Result; var user = GetCurrentUserAsync().Result;
var uid = user.Id; var uid = user.Id;
bool existing = _dbContext.Performers.Any(x => x.PerfomerId == uid); bool existing = _dbContext.Performers.Any(x => x.PerformerId == uid);
ViewBag.Activities = _dbContext.ActivityItems(null); ViewBag.Activities = _dbContext.ActivityItems(null);
ViewBag.GoogleSettings = _googleSettings; ViewBag.GoogleSettings = _googleSettings;
if (existing) if (existing)
{ {
var currentProfile = _dbContext.Performers.Include(x => x.OrganisationAddress) var currentProfile = _dbContext.Performers.Include(x => x.OrganizationAddress)
.First(x => x.PerfomerId == uid); .First(x => x.PerformerId == uid);
string currentCode = currentProfile.ActivityCode; string currentCode = currentProfile.ActivityCode;
return View(currentProfile); return View(currentProfile);
} }
return View(new PerformerProfile return View(new PerformerProfile
{ {
PerfomerId = user.Id, PerformerId = user.Id,
OrganisationAddress = new Location() OrganizationAddress = new Location()
}); });
} }
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken, Authorize] [Authorize]
public async Task<IActionResult> SetActivity(PerformerProfile model) public async Task<IActionResult> SetActivity(PerformerProfile model)
{ {
var user = GetCurrentUserAsync().Result; var user = GetCurrentUserAsync().Result;
var uid = user.Id; var uid = user.Id;
try { 
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
var taskCheck = await _cchecker.CheckAsync(model.SIREN); var taskCheck = await _cchecker.CheckAsync(model.SIREN);
if (!taskCheck.success) { if (!taskCheck.success) {
ModelState.AddModelError( ModelState.AddModelError(
"SIREN", "SIREN",
_SR["Invalid company number"]+" ("+taskCheck.errorCode+")" _SR["Invalid company number"]+" ("+taskCheck.errorCode+")"
); );
_logger.LogWarning("Invalid company number, using key:"+_cinfoSettings.ApiKey); _logger.LogWarning("Invalid company number, using key:"+_cinfoSettings.ApiKey);
}
} }
} }
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
if (uid == model.PerfomerId) if (uid == model.PerformerId)
{ {
bool addrexists = _dbContext.Map.Any(x => model.OrganisationAddress.Id == x.Id); bool addrexists = _dbContext.Map.Any(x => model.OrganizationAddress.Id == x.Id);
if (!addrexists) if (!addrexists)
{ {
_dbContext.Map.Add(model.OrganisationAddress); _dbContext.Map.Add(model.OrganizationAddress);
} }
bool existing = _dbContext.Performers.Any(x => x.PerfomerId == uid); bool existing = _dbContext.Performers.Any(x => x.PerformerId == uid);
if (existing) if (existing)
{ {
_dbContext.Update(model); _dbContext.Update(model);
@ -518,7 +523,7 @@ namespace Yavsc.Controllers
return RedirectToAction(nameof(Index), new { Message = message }); return RedirectToAction(nameof(Index), new { Message = message });
} }
else ModelState.AddModelError(string.Empty, "Acces denied"); else ModelState.AddModelError(string.Empty, $"Access denied ({uid} vs {model.PerformerId})");
} }
ViewBag.GoogleSettings = _googleSettings; ViewBag.GoogleSettings = _googleSettings;
ViewBag.Activities = _dbContext.ActivityItems(model.ActivityCode); ViewBag.Activities = _dbContext.ActivityItems(model.ActivityCode);
@ -530,11 +535,11 @@ namespace Yavsc.Controllers
{ {
var user = GetCurrentUserAsync().Result; var user = GetCurrentUserAsync().Result;
var uid = user.Id; var uid = user.Id;
bool existing = _dbContext.Performers.Any(x => x.PerfomerId == uid); bool existing = _dbContext.Performers.Any(x => x.PerformerId == uid);
if (existing) if (existing)
{ {
_dbContext.Performers.Remove( _dbContext.Performers.Remove(
_dbContext.Performers.First(x => x.PerfomerId == uid) _dbContext.Performers.First(x => x.PerformerId == uid)
); );
_dbContext.SaveChanges(); _dbContext.SaveChanges();
} }

Loading…