public async Task <ActionResult <Data.Models.MuscleGroup> > CreateNewMuscleGroup(Data.Models.MuscleGroupForCreate newMuscleGroup) { Data.Entities.MuscleGroup dbNewMuscleGroup = null; try { dbNewMuscleGroup = _mapper.Map <Data.Entities.MuscleGroup>(newMuscleGroup); } catch (Exception ex) { return(BadRequest("Input is in invalid format: " + ex.Message)); } if (dbNewMuscleGroup == null) { return(BadRequest("Input is in invalid format")); } await _repository.AddAsync <Data.Entities.MuscleGroup>(dbNewMuscleGroup); await _repository.SaveChangesAsync(); Data.Models.MuscleGroup addedMuscleGroup = _mapper.Map <Data.Models.MuscleGroup>(dbNewMuscleGroup); var url = _linkgenerator.GetPathByAction(HttpContext, "GetMuscleGroupByMuscleGroupId", "MuscleGroups", addedMuscleGroup); return(this.Created(url, addedMuscleGroup)); }
public async Task <ActionResult <Data.Models.MuscleGroup> > PatchMuscleGroup(int muscleGroupId, JsonPatchDocument <Data.Models.MuscleGroupForUpdate> patchDocument) { try { Data.Entities.MuscleGroup dbMuscleGroup = await _repository.GetMuscleGroupAsync(muscleGroupId); if (dbMuscleGroup == null) { return(NotFound()); } var updatedMuscleGroup = _mapper.Map <Data.Models.MuscleGroupForUpdate>(dbMuscleGroup); patchDocument.ApplyTo(updatedMuscleGroup, ModelState); _mapper.Map(updatedMuscleGroup, dbMuscleGroup); if (await _repository.SaveChangesAsync()) { Data.Models.MuscleGroup savedMuscleGroup = _mapper.Map <Data.Models.MuscleGroup>(await _repository.GetMuscleGroupAsync(muscleGroupId)); return(Ok(savedMuscleGroup)); } else { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to save to database")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to patch muscleGroup " + ex.Message)); } }
public async Task <ActionResult <Data.Models.MuscleGroup> > UpdateMuscleGroup(int muscleGroupId, Data.Models.MuscleGroupForUpdate updatedMuscleGroup) { try { Data.Entities.MuscleGroup dbMuscleGroup = await _repository.GetMuscleGroupAsync(muscleGroupId); if (dbMuscleGroup == null) { return(NotFound()); } _mapper.Map(updatedMuscleGroup, dbMuscleGroup); if (await _repository.SaveChangesAsync()) { Data.Models.MuscleGroup savedMuscleGroup = _mapper.Map <Data.Models.MuscleGroup>(dbMuscleGroup); return(Ok(savedMuscleGroup)); } else { return(BadRequest("Failed to update.")); } } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message)); } }
public async Task <IActionResult> DeleteMuscleGroup(int muscleGroupId) { try { Data.Entities.MuscleGroup dbMuscleGroup = await _repository.GetMuscleGroupAsync(muscleGroupId); if (dbMuscleGroup == null) { return(NotFound()); } _repository.Delete <Data.Entities.MuscleGroup>(dbMuscleGroup); await _repository.SaveChangesAsync(); return(NoContent()); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message)); } }