public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Entities.Course>(courseDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } var courseToPatch = _mapper.Map <CourseForUpdateDto>(courseForAuthorFromRepo); //add invalid request will be result in 400 request(default) patchDocument.ApplyTo(courseToPatch, ModelState); if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Course>(courseDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); } var courseToPatch = _mapper.Map <CourseForUpdateDto>(courseForAuthorFromRepo); patchDocument.ApplyTo(courseToPatch, ModelState); if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseAuthorFromRepo = courseLibraryRepository.GetCourse(authorId, courseId); if (courseAuthorFromRepo == null) { return(NotFound()); } mapper.Map(course, courseAuthorFromRepo); courseLibraryRepository.UpdateCourse(courseAuthorFromRepo); courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourse(Guid id, [FromBody] CourseForUpdateDto course) { if (course == null) { _logger.LogError("CourseForUpdateDto object sent from client is null."); return(BadRequest("CourseForUpdateDto object is null")); } var courseEntity = _repository.Course.GetCourse(id, trackChanges: true); if (courseEntity == null) { _logger.LogInfo($"Course with id: {id} doesn't exist in the database."); return(NotFound()); } _mapper.Map(course, courseEntity); _repository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { // return NotFound(); // Upsert code var courseToAdd = _mapper.Map <Entities.Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseFromAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // map the entity to a CourseForUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseFromAuthorRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseFromAuthorRepo == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Course>(courseDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } var courseToPatch = _mapper.Map <CourseForUpdateDto>(courseFromAuthorRepo); patchDocument.ApplyTo(courseToPatch, ModelState); // PATCH validation (only handles existing resource paths and not non -existent ones. hence we need to override. Retrun InvalidModelStateResponseFactory) if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, courseFromAuthorRepo); _courseLibraryRepository.UpdateCourse(courseFromAuthorRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public async Task <IActionResult> PatchCourse(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!await mRepository.AuthorExistsAsync(authorId)) { return(NotFound()); } var targetEntity = await mRepository.GetCourseAsync(authorId, courseId); if (targetEntity == null) { var newCourseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(newCourseDto, ModelState); if (!TryValidateModel(newCourseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = mMapper.Map <Course>(newCourseDto); courseToAdd.Id = courseId; mRepository.AddCourse(authorId, courseToAdd); await mRepository.SaveChangesAsync(); var createdDto = mMapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseOfAnAuthor", new { authorId, courseId = createdDto.Id }, createdDto)); } var courseToPatch = mMapper.Map <CourseForUpdateDto>(targetEntity); patchDocument.ApplyTo(courseToPatch, ModelState); if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } mMapper.Map(courseToPatch, targetEntity); await mRepository.SaveChangesAsync(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdateDto) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseFromRepo == null) { // Put this back if you don't want PUTs to be able to create new resources // return NotFound(); var courseToAdd = _mapper.Map <Course>(courseForUpdateDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToAdd.Id }, courseToReturn)); } // map the entity to a CourseForUpdateDto // apply the updated field values to the dto // map the CourseForUpdateDto back to an entity _mapper.Map(courseForUpdateDto, courseFromRepo); _courseLibraryRepository.UpdateCourse(courseFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourse(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdating) { if (!_repository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _repository.GetCourse(authorId, courseId); if (courseFromRepo == null) { return(UpsertFromPut(authorId, courseId, courseForUpdating)); } // The following statement will apply all the fields of courseForUpdating to courseFromRepo _ = _mapper.Map(courseForUpdating, courseFromRepo); _repository.UpdateCourse(courseFromRepo); _repository.Save(); return(NoContent()); }
private IActionResult UpsertFromPatch(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Course>(courseDto); courseToAdd.Id = courseId; _repository.AddCourse(authorId, courseToAdd); _repository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute(nameof(GetCourseForAuthor), new { authorId, courseId = courseToReturn.Id }, courseToReturn)); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdateDto) { //user input validation if (!this.courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorEntity = this.courseLibraryRepository.GetCourse(authorId, courseId); //If course does not exist, create it if (courseForAuthorEntity == null) { //map to entity and set the ID var courseAdd = this.mapper.Map <Entities.Course>(courseForUpdateDto); courseAdd.Id = courseId; //Add course this.courseLibraryRepository.AddCourse(authorId, courseAdd); this.courseLibraryRepository.Save(); //Return saved output var courseReturn = this.mapper.Map <CourseDto>(courseAdd); return(CreatedAtRoute( "GetCourseForAuthor", new { authorId, courseId = courseReturn.Id }, //notice because of the same authorId name and order, no mapping required courseReturn )); } //map to the entity, this is another way to update the existing var without creating a new var this.mapper.Map(courseForUpdateDto, courseForAuthorEntity); //apply the update this.courseLibraryRepository.UpdateCourse(courseForAuthorEntity); this.courseLibraryRepository.Save(); //return no content, standard for an httpput return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdateDto) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseEntity == null) { var courseForCreation = _mapper.Map <Course>(courseForUpdateDto); courseForCreation.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseForCreation); _courseLibraryRepository.Save(); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseForCreation.Id }, _mapper.Map <CourseDto>(courseForCreation))); } _mapper.Map(courseForUpdateDto, courseEntity); _courseLibraryRepository.UpdateCourse(courseEntity); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); // Implementing the concept of upserting // client can create a resource by generating a new guid if (courseForAuthorFromRepo == null) { var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // Auto mapper taking care of these steps // map the entity to a CourseForUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(course, courseForAuthorFromRepo); // This underlying implementation of this method is empty because // In Entity Framework core, these entities are tracked by the context, So by executing the // mapper . Map statement, the entity has changed to a modifies state and executing to Save // will write changes to our database. SO all we have to do is to call the Save method into the // repository. _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDTO>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // map the entity to a CourseForUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity // Method below takes care of all three steps mentioned above: _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); // Returning 204 No Content will be valid the same as a 200 Ok return(NoContent()); }
public IActionResult UpdateCourseForAuthor( Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseToCreate = _mapper.Map <Course>(course); courseToCreate.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToCreate); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToCreate); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToCreate.Id }, courseToReturn)); } // map entity to CourseForUpdateDto // apply updated fields // map back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDoc) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseEntity == null) { var courseDto = new CourseForUpdateDto(); patchDoc.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem()); } courseEntity = _mapper.Map <Course>(courseDto); courseEntity.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseEntity); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseEntity); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, _mapper.Map <CourseDto>(courseToReturn))); } var courseForPatch = _mapper.Map <CourseForUpdateDto>(courseEntity); patchDoc.ApplyTo(courseForPatch, ModelState); if (!TryValidateModel(courseForPatch)) { return(ValidationProblem()); } _mapper.Map(courseForPatch, courseEntity); _courseLibraryRepository.UpdateCourse(courseEntity); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRep = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRep == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); //To check if a model is valid after Apply patch document if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseIdToAdd = _mapper.Map <Course>(courseDto); courseIdToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseIdToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseIdToAdd); return(CreatedAtAction("GetCourseForAuthor", new { authorId, CourseId = courseToReturn.Id }, courseToReturn)); } var courseToPath = _mapper.Map <CourseForUpdateDto>(courseForAuthorFromRep); patchDocument.ApplyTo(courseToPath, ModelState); //To check if a model is valid after Apply patch document if (!TryValidateModel(courseToPath)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPath, courseForAuthorFromRep); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRep); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult UpdateCoursesForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto updateCourseDto) { if (!courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var course = courseLibraryRepository.GetCourse(authorId, courseId); if (course == null) { var courseToAdd = mapper.Map <Course>(updateCourseDto); courseToAdd.Id = courseId; courseLibraryRepository.AddCourse(authorId, courseToAdd); courseLibraryRepository.Save(); var courseToReturn = mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourse", new { authorId, courseId = courseId, courseToReturn })); } mapper.Map(course, updateCourseDto); courseLibraryRepository.UpdateCourse(course); courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult Patch(Guid authorId, Guid courseId, [FromBody] JsonPatchDocument <CourseForUpdateDto> course) { if (!_repository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _repository.GetCourse(authorId, courseId); // Insert if (courseFromRepo == null) { var courseForUpdateDto = new CourseForUpdateDto(); course.ApplyTo(courseForUpdateDto, ModelState); if (!TryValidateModel(courseForUpdateDto)) { return(UnprocessableEntity()); } var courseToAdd = _mapper.Map <Course>(courseForUpdateDto); courseToAdd.Id = courseId; _repository.AddCourse(authorId, courseToAdd); _repository.Save(); return(CreatedAtRoute("GetCourse", new { authorId, courseId }, courseToAdd)); } else // Update { var courseForUpdateDto = _mapper.Map <CourseForUpdateDto>(courseFromRepo); course.ApplyTo(courseForUpdateDto, ModelState); if (!TryValidateModel(courseForUpdateDto)) { return(UnprocessableEntity()); } _mapper.Map(courseForUpdateDto, courseFromRepo); _repository.UpdateCourse(courseFromRepo); _repository.Save(); return(NoContent()); } }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _libraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _libraryRepository.AddCourse(authorId, courseToAdd); _libraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtAction( nameof(GetCourseForAuthor), new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // update steps // 1. map the entity to a CourseForUpdateDto // 2. apply the updated field values to that dto // 3. mapt the CourseForUpdateDto back to an entity // all the steps completed by one statement _mapper.Map(course, courseForAuthorFromRepo); _libraryRepository.UpdateCourse(courseForAuthorFromRepo); _libraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor( [FromRoute] Guid authorId, [FromRoute] Guid courseId, [FromBody] CourseForUpdateDto courseForUpdate) { if (!AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseFromRepo == null) { Course courseToAdd = _mapper.Map <Course>(courseForUpdate); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); CourseDto courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute( routeName: "GetCourseForAuthor", routeValues: new { authorId = authorId, courseId = courseToReturn.Id }, value: courseToReturn )); } // Maps the values from the source to destination // This allows us to update the values with out doing it piecewise manually _mapper.Map(source: courseForUpdate, destination: courseFromRepo); _courseLibraryRepository.UpdateCourse(courseFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public async Task <IActionResult> Update(CourseForUpdateDto courseForUpdateDto) { try { var courseFromRepo = await _courseRepo.GetOne(courseForUpdateDto.Id); if (courseFromRepo == null) { return(BadRequest("There is no course like this")); } _mapper.Map(courseForUpdateDto, courseFromRepo); if (await _courseRepo.SaveAll()) { return(Ok("Successfully updated the course")); } return(BadRequest("Updating the course failed")); } catch (Exception e) { return(BadRequest("Updating the course failed")); } }
[HttpPut("{courseId}")] //Update or upsert! public IActionResult UpdateCourseForAuthor( Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId).Result; if (courseForAuthorFromRepo == null) {//If the course doesn't exist yet, we "update it" by creating it: var courseToAdd = _mapper.Map <Entities.Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } // Map the entity to a CourseForUpdateDto // Apply the updated field values to that dto // Map the CourseForUpdateDto back to an entity. _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); // For Upserting if (courseForAuthorFromRepo == null) { //return NotFound(); var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); //we should send 200 var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { /*authorId =*/ authorId, courseId = courseToReturn.Id }, courseToReturn)); } // map the entity to a CourseForUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); // we acn return 200 ok contain updated resource representation in response body or 204 no content return(NoContent()); }
public IActionResult UpdateCoursesForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRep = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRep == null) { var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, CourseId = courseToReturn.Id }, courseToReturn)); } _mapper.Map(course, courseForAuthorFromRep); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRep); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); /* ************************************ UPSERT Processing ****************************************** */ if (courseForAuthorFromRepo == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Entities.Course>(courseDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } /* ************************************ UPSERT Processing ****************************************** */ var courseToPatch = _mapper.Map <CourseForUpdateDto>(courseForAuthorFromRepo); /* (1) Validate the patch document */ /* Any errors inside the patch document (eg referencing a field name that does not exist)) will make the ModelState invalid */ /* It will then be picked up by TryValidateModel below */ patchDocument.ApplyTo(courseToPatch, ModelState); /* (2) Validate the contents of the updated DTO */ /*** manually validating because the action parameter is a patch document * and we can't set any validation attributes etc on it. So, after we apply the patch to the DTO, we can then check * and see if all the field values are valid before continuing. */ if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public async Task <IActionResult> UpdateEmployeeForCompany(Guid companyId, Guid id, [FromBody] CourseForUpdateDto employee) { var employeeEntity = HttpContext.Items["employee"] as Courses; _mapper.Map(employee, employeeEntity); await _repository.SaveAsync(); return(NoContent()); }
public IActionResult PartiallyUpdateCourseForStudent( Guid studentId, Guid id, [FromBody] JsonPatchDocument <CourseForUpdateDto> patchDoc) { if (patchDoc == null) { return(this.BadRequest()); } if (!this.courseService.StudentExists(studentId)) { return(this.NotFound()); } var course = this.courseService.GetCourse(studentId, id); if (course == null) { var courseForUpdateDto = new CourseForUpdateDto(); patchDoc.ApplyTo(courseForUpdateDto, this.ModelState); if (courseForUpdateDto.Description == courseForUpdateDto.Title) { this.ModelState.AddModelError(nameof(CourseForUpdateDto), "The provided description should be different from the title."); } this.TryValidateModel(courseForUpdateDto); if (!this.ModelState.IsValid) { return(new UnprocessableEntityObjectResult(this.ModelState)); } var mapCourse = this.mapper.Map <Course>(courseForUpdateDto); mapCourse.Id = id; if (!this.courseService.AddCourse(studentId, mapCourse)) { throw new Exception($"Upserting course {id} for student {studentId} failed on save."); } var courseDto = this.mapper.Map <CourseDto>(mapCourse); return(this.CreatedAtRoute( RouteName.GetCourseForStudent, new { studentId, courseDto.Id }, courseDto)); } var courseToPatch = this.mapper.Map <CourseForUpdateDto>(course); patchDoc.ApplyTo(courseToPatch, this.ModelState); if (courseToPatch.Description == courseToPatch.Title) { this.ModelState.AddModelError(nameof(CourseForUpdateDto), "The provided description should be different from the title."); } this.TryValidateModel(courseToPatch); if (!this.ModelState.IsValid) { return(new UnprocessableEntityObjectResult(this.ModelState)); } this.mapper.Map(courseToPatch, course); if (!this.courseService.UpdateCourse(course)) { throw new Exception($"Patching course {id} for student {studentId} failed on save."); } return(this.NoContent()); }
public async Task <IActionResult> UpdateCourseForAuthorAsync(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdateDto) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = await _courseLibraryRepository.GetCourseAsync(authorId, courseId); if (courseForAuthorFromRepo == null) { var courseToAdd = _mapper.Map <Course>(courseForUpdateDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); await _courseLibraryRepository.SaveAsync(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } //map the entity courseFromRepo to a CourseForUpdateDto //apply the updated fields //map courseForUpdateDto back to Entity //(source, destination) _mapper.Map(courseForUpdateDto, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); await _courseLibraryRepository.SaveAsync(); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpdateDto> patchDocument) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseFromAuthorRepo = _courseLibraryRepository.GetCourse(authorId, courseId); /// Applying Upserting if (courseFromAuthorRepo == null) { var courseDto = new CourseForUpdateDto(); patchDocument.ApplyTo(courseDto, ModelState); if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAdd = _mapper.Map <Entities.Course>(courseDto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // Since patch is being applied to CourseForUpdateDto we need to map it from the entity // and apply the patch CourseForUpdateDto courseToPatch = _mapper.Map <CourseForUpdateDto>(courseFromAuthorRepo); /* * We need to pass the ModelState, so that if patchDocument result in error the model state * becomes invalid and TryValidateModel will catch the error * [ * { * "op": "remove", * "path": "/thisdoesnotexist" * } * ] */ patchDocument.ApplyTo(courseToPatch, ModelState); // We need to validate the model, since it is a patch document that is applied // hence the validation required by us for example description can not be empty for // update did not occur. // By applying the TryValidateModel we make sure our rules are applied if not // we return the ValidationProblem // So we use the following patch action it will return in error /* * [ * { * "op": "remove", * "path": "/description" * } * ] */ if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, courseFromAuthorRepo); _courseLibraryRepository.UpdateCourse(courseFromAuthorRepo); _courseLibraryRepository.Save(); return(NoContent()); }