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> > 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> > 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)); } }