public async Task <IActionResult> UpdateWorkhoursAsync([FromRoute] Guid workhoursId, [FromBody] WorkhoursUpdateDto workhoursToUpdate) { var foundWorkhours = await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursId); if (foundWorkhours == null) { var workhoursToAdd = Mapper.Map <Workhours>(workhoursToUpdate); workhoursToAdd.Id = Guid.NewGuid(); _workhoursRepository.Create(workhoursToAdd); await _workhoursRepository.SaveChangesAsync(); var workhoursToReturn = await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursToAdd.Id); return(CreatedAtRoute("GetWorkhours", new { workhoursId = workhoursToReturn.Id }, workhoursToReturn)); } Mapper.Map(workhoursToUpdate, foundWorkhours); _workhoursRepository.Update(foundWorkhours); await _workhoursRepository.SaveChangesAsync(); // check if it returns "old" workhours before update. Else find and return? return(Ok(foundWorkhours)); //return NoContent(); }
public async Task <IActionResult> UpdateWorkhoursAsync( [FromRoute, SwaggerParameter("Id of workhours to update", Required = true)] Guid workhoursId, [FromBody, SwaggerParameter("Object with the updates", Required = true)] WorkhoursUpdateDto workhoursToUpdate, [FromHeader(Name = "accept"), SwaggerParameter("Request Hateoas")] string mediaType) { var foundWorkhours = await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursId); if (foundWorkhours == null) { var workhoursToAdd = Mapper.Map <Workhours>(workhoursToUpdate); workhoursToAdd.Id = Guid.NewGuid(); _workhoursRepository.Create(workhoursToAdd); if (!await _workhoursRepository.SaveChangesAsync()) { throw new Exception("Failed to save an upserting"); } var workhoursToReturn = Mapper.Map <WorkhoursDto>(await _workhoursRepository.GetFirstByConditionAsync(j => j.Id == workhoursId)); if (mediaType == "application/vnd.AO.json+hateoas") { var shapedWorkhours = _controllerHelper.ShapeAndAddLinkToObject(workhoursToReturn, "Workhours", null); return(CreatedAtRoute("GetWorkhours", new { workhoursId = workhoursToReturn.Id }, shapedWorkhours)); } return(CreatedAtRoute("GetWorkhours", new { workhoursId = workhoursToReturn.Id }, workhoursToReturn)); } Mapper.Map(workhoursToUpdate, foundWorkhours); _workhoursRepository.Update(foundWorkhours); if (!await _workhoursRepository.SaveChangesAsync()) { throw new Exception("Failed to save on upserting"); } return(NoContent()); }