public async Task <IHttpActionResult> Put(Guid id, [FromBody] Data.Entities.Job job) { try { var validation = m_db.GetValidationResult(job); if (validation.IsValid) { m_db.Jobs.Attach(job); m_db.SetModified(job); await m_db.SaveChangesAsync(); return(Ok(job)); } else { AddErrorsToModelState(validation, ModelState); return(BadRequest(ModelState)); } } catch (Exception e) { m_logger.Error(e); return(new ExceptionResult(e, this)); } }
public async Task <ActionResult <Data.Models.Job> > CreateNewJob(Data.Models.JobForCreate newJob) { Data.Entities.Job dbNewJob = null; try { dbNewJob = _mapper.Map <Data.Entities.Job>(newJob); } catch (Exception ex) { return(BadRequest("Input is in invalid format: " + ex.Message)); } if (dbNewJob == null) { return(BadRequest("Input is in invalid format")); } await _repository.AddAsync <Data.Entities.Job>(dbNewJob); await _repository.SaveChangesAsync(); Data.Models.Job addedJob = _mapper.Map <Data.Models.Job>(dbNewJob); var url = _linkgenerator.GetPathByAction(HttpContext, "GetJobByJobId", "Jobs", addedJob); return(this.Created(url, addedJob)); }
public async Task <ActionResult <Data.Models.Job> > PatchJob(short jobId, JsonPatchDocument <Data.Models.JobForUpdate> patchDocument) { try { Data.Entities.Job dbJob = await _repository.GetJobAsync(jobId); if (dbJob == null) { return(NotFound()); } var updatedJob = _mapper.Map <Data.Models.JobForUpdate>(dbJob); patchDocument.ApplyTo(updatedJob, ModelState); _mapper.Map(updatedJob, dbJob); if (await _repository.SaveChangesAsync()) { Data.Models.Job savedJob = _mapper.Map <Data.Models.Job>(await _repository.GetJobAsync(jobId)); return(Ok(savedJob)); } else { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to save to database")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to patch job " + ex.Message)); } }
public async Task <ActionResult <Data.Models.Job> > UpdateJob(short jobId, Data.Models.JobForUpdate updatedJob) { try { Data.Entities.Job dbJob = await _repository.GetJobAsync(jobId); if (dbJob == null) { return(NotFound()); } _mapper.Map(updatedJob, dbJob); if (await _repository.SaveChangesAsync()) { Data.Models.Job savedJob = _mapper.Map <Data.Models.Job>(dbJob); return(Ok(savedJob)); } else { return(BadRequest("Failed to update.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message)); } }
public async Task <IActionResult> DeleteJob(short jobId) { try { Data.Entities.Job dbJob = await _repository.GetJobAsync(jobId); if (dbJob == null) { return(NotFound()); } _repository.Delete <Data.Entities.Job>(dbJob); await _repository.SaveChangesAsync(); return(NoContent()); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message)); } }