public async Task <IActionResult> UpdateService(string id, [FromBody] JsonPatchDocument <ServiceTable> patch) { try { var row = await _context.ServiceTable.Where(u => u.ServiceId == id).FirstOrDefaultAsync(); if (row == null) { return(StatusCode(404)); } patch.ApplyTo(row, ModelState); _context.SaveChanges(); if (!ModelState.IsValid) { string modelStateJson = _service.ToJson(ModelState); _logger.LogInformation("Bad" + modelStateJson); return(new ContentResult { Content = modelStateJson, ContentType = "application/json", StatusCode = 400 }); } return(StatusCode(204)); } catch (DbUpdateException ex) { _logger.LogDebug(ex.ToString()); return(StatusCode(400)); } catch (Exception ex) { _logger.LogDebug(ex.ToString()); return(StatusCode(500)); } }
public RecertCycleController(PAML01Context context, ControllerService service, ILogger <RecertCycleController> logger) { _context = context; _service = service; _logger = logger; if (!_context.RecertCycleTable.Any()) { _context.RecertCycleTable.Add( new RecertCycleTable { RecertCycleTitle = "Initial cycle", RecertStartedDate = DateTime.Now, RecertEnabled = false } ); _context.SaveChanges(); } }
public ActionResult <RecertCycleDTO> AddRecertCycle(RecertCyclePostDTO newRecertCycle) { try { if (!_context.RoleTable.Any()) { return(StatusCode(400, "Unable to begin a new recertification cycle: RoleTable in the database is not populated. Please go to the upload .csv page and upload a .csv of RoleTable data.")); } else if (!_context.ServiceTable.Any()) { return(StatusCode(400, "Unable to begin a new recertification cycle: ServiceTable in the database is not populated. Please go to the upload .csv page and upload a .csv of ServiceTable data.")); } else if (!_context.PrivTable.Any()) { return(StatusCode(400, "Unable to begin a new recertification cycle: PrivTable in the database is not populated. Please go to the upload .csv page and upload a .csv of PrivTable data.")); } else if (!_context.RolePrivLink.Any()) { return(StatusCode(400, "Unable to begin a new recertification cycle: RolePrivLink in the database is not populated. Please go to the upload .csv page and upload a .csv of RolePrivLink data.")); } else if (!_context.UserTable.Any()) { return(StatusCode(400, "Unable to begin a new recertification cycle: UserTable in the database is not populated. Please go to the upload .csv page and upload a .csv of UserTable data.")); } // Set the end date of the soon to be old recert cycle, this effectively locks out changes // since posts/patches etc check the current cycle end date is null before making amendments var currentRecertCycle = _context.RecertCycleTable.OrderByDescending(rc => rc.RecertCycleId).FirstOrDefault(); if (currentRecertCycle != null) { currentRecertCycle.RecertEndedDate = DateTime.Now; _context.SaveChanges(); } // Now set the certified states for role service privs of service owners and role owner to false var exists = _context.RolePrivLink.ToList(); if (exists != null) { foreach (RolePrivLink rp in exists) { rp.RoleOwner_IsCertified = false; rp.ServiceOwner_IsCertified = false; rp.RiskIsAssessed = false; } } _context.SaveChanges(); // Now we'll make the new cycle. If we ever do a lookup of data at the start of this // cycle it will start off with blank recert/risk data which is what we want _context.RecertCycleTable.Add( new RecertCycleTable { RecertCycleTitle = newRecertCycle.RecertCycleTitle, RecertStartedDate = DateTime.Now, RecertEnabled = newRecertCycle.RecertEnabled } ); _context.SaveChanges(); } catch (Exception ex) { _logger.LogDebug(ex.ToString()); return(StatusCode(500)); } return(CreatedAtAction(nameof(GetMostRecentRecertCycle), new { offset = 0 }, newRecertCycle)); }