public IActionResult UpdateCourseForAuthor(int authorId, int courseId, CourseForUpdateDTO courseForUpdate) { var AuthorExist = _courseLibrary.AuthorExists(authorId); if (!AuthorExist) { return(NotFound(new JsonResponse <string>() { Success = false, ErrorMessage = "AuthorId is Invalid." })); } var AuthorsCourse = _courseLibrary.GetCourse(authorId, courseId); if (AuthorsCourse == null) { return(NotFound(new JsonResponse <string>() { Success = false, ErrorMessage = "CourseId is Invalid." })); } courseForUpdate.ID = courseId; var CourseforAuthor = _mapper.Map <Entities.Course>(courseForUpdate); _courseLibrary.UpdateCourse(CourseforAuthor); _courseLibrary.Save(); return(NoContent()); // return null; }
public ActionResult UpdateCourseForAuthor( Guid authorId, Guid courseId, CourseForUpdateDTO course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { return(NotFound()); } _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(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 <Entities.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 update field values to that dto //map the CourseForUpdateDTO back to an entity _mapper.Map(course, courseForAuthorFromRep); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRep); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult UpdateCourseForAuthor(Guid courseId, Guid authorId, CourseForUpdateDTO courseForUpdateDTO) { if (!courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseReturnFromRepo = courseLibraryRepository.GetCourse(authorId, courseId); if (courseReturnFromRepo == null) { var CourseToAdd = _mapper.Map <Course>(courseForUpdateDTO); courseLibraryRepository.AddCourse(authorId, CourseToAdd); courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDTO>(CourseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } // Map the Entity to the CourseForUpdate // Apply the Update Field Value to the DTo // Map the CourseForUpdate Back to the Entity _mapper.Map(courseForUpdateDTO, courseReturnFromRepo); courseLibraryRepository.UpdateCourse(courseReturnFromRepo); courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult PartiallyUpdateCourseForAuthor(Guid courseId, Guid authorId, 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); // Validating the ModelState if (!TryValidateModel(courseDto)) { return(ValidationProblem(ModelState)); } var courseToAddd = _mapper.Map <Course>(courseDto); courseToAddd.Id = courseId; courseLibraryRepository.AddCourse(authorId, courseToAddd); courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDTO>(courseToAddd); return (CreatedAtRoute( "GetCourseForAuthor", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } var courseToPatch = _mapper.Map <CourseForUpdateDTO>(courseForAuthorFromRepo); // add Validation patchDocument.ApplyTo(courseToPatch, ModelState); if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(courseToPatch, 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 courseForAuthorRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorRepo == null) { // rather than return not found, let's upsert and create the course // return NotFound(); 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>(courseForAuthorRepo); // add validation before using ApplyTo() patchDocument.ApplyTo(courseToPatch, ModelState); if (!TryValidateModel(courseToPatch)) { return(ValidationProblem(ModelState)); } // map the dto back to the entity _mapper.Map(courseToPatch, courseForAuthorRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDTO newCourse) { if (!_clRepo.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _clRepo.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { // The line below is used for POST request, below this the logic for UPSERTING is implemented //return NotFound(); // Upserting is when the client is allowed to create resources, even providing the unique identifier to be // set up in the DB // if the DB uses int as identifier, than the next one needs to be chosen, but here we use GUIDs so upserting works. // the first time the resorce is created, and the second time it is updated. // UPSERTING is done by PUT requests, hence it is idempotent. var courseToAdd = _mapper.Map <Course>(newCourse); courseToAdd.Id = courseId; _clRepo.AddCourse(authorId, courseToAdd); _clRepo.Save(); var courseToReturn = _mapper.Map <CourseDTO>(courseToAdd); return(CreatedAtRoute( "GetCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn )); } // map the entity to a CourseForUpdateDTO // apply the update field values to the DTO // map the CourseForUpdateDTO back to an entity // the statement below carries out all the above 3 for us _mapper.Map(newCourse, courseForAuthorFromRepo); // The above statement copied the updates from the incoming object to the entity. // But, we need to update the resourse, an entity is just a representation of the resource _clRepo.UpdateCourse(courseForAuthorFromRepo); _clRepo.Save(); return(NoContent()); // can also return 200 Ok with updated resource }
public IActionResult UpdateCourseForAuthor( Guid authorId, Guid courseId, CourseForUpdateDTO course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == null) { // Implement Upsert logic, and insert the course // Client is generating the GUID for courseId in this case. var courseToAdd = _mapper.Map <Entities.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 update field values to that DTO // Map the courseForUpdateDTO back to an entity _mapper.Map(course, courseForAuthorFromRepo); // After the Map() above, the Entity will now contain the updated fields, // adhering to any projections we defined in the profile // Update the repo _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); // Note: Add a optimistic concurrency check here and return 412 Precondition Failed, telling user record was updated // This is implemented with "If-Match <etag> <> previous etag, give concurrency error. _courseLibraryRepository.Save(); return(NoContent()); // here we're not return the resource. // Some APIs might need the resource. // In our implementation, it's up to the client // to decide on GET to update the resource. // Notice too, how the ActionResult does not contain a <T> to return. }