public IActionResult UpsertCourseForAuthor(Guid authorId, Guid courseId, CourseForUpsertDto courseUpsertDto) { if (!this._repos.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = this._repos.GetCourse(authorId, courseId); if (courseEntity == null) { var course = this._mapper.Map <Course>(courseUpsertDto); course.Id = courseId; this._repos.AddCourse(authorId, course); this._repos.Save(); var courseDto = this._mapper.Map <CourseDto>(course); return(CreatedAtRoute("GetCourse", new { authorId, courseId = courseDto.Id }, courseDto)); } // map the entity to a CourseForUpsertDto // apply the updated field values to that dto // map the CourseForUpsertDto back to an entity. // Scheme: courseUpsertDto<CourseForUpsertDto> -> courseEntity<Course> -> Set dto parameters // -> returns to courseEntity<Course> with new parameters this._mapper.Map(courseUpsertDto, courseEntity); // This decorated method. Can be work without him. // There not have code implementation. this._repos.UpdateCourse(courseEntity); // Saves changes entity to db without using repository method, because we have reference for him this._repos.Save(); return(NoContent()); }
public IActionResult PatchCourseForAuthor(Guid authorId, Guid courseId, JsonPatchDocument <CourseForUpsertDto> patchDocument) { /* * For example what we need to send (in postman we use Text format): * Headers : * - Content-Type: application/json-patch+json * - Accept: application/json * [ * { * "op": "replace", // "add", "copy", "remove", "move" * "path": "/title", * "value": "Title from patch" * } * ] */ if (!this._repos.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = this._repos.GetCourse(authorId, courseId); if (courseEntity == null) { /* * For request parameters example: * [ * { * "op": "add", // replace * "path": "/title", * "value": "Title from patch" * }, * { * "op": "add", // replace * "path": "/description", * "value": "Description from patch" * } * ] */ var courseForUpsertDto = new CourseForUpsertDto(); patchDocument.ApplyTo(courseForUpsertDto, ModelState); if (!TryValidateModel(courseForUpsertDto)) { return(ValidationProblem(ModelState)); } var course = this._mapper.Map <Course>(courseForUpsertDto); course.Id = courseId; this._repos.AddCourse(authorId, course); this._repos.Save(); var courseDto = this._mapper.Map <CourseDto>(course); return(CreatedAtRoute("GetCourse", new { authorId, courseId = courseDto.Id }, courseDto)); } var courseUpsertDto = this._mapper.Map <CourseForUpsertDto>(courseEntity); // + Newtonsoft package. // Changes courseUpsertDto field. patchDocument.ApplyTo(courseUpsertDto, ModelState); if (!TryValidateModel(courseUpsertDto)) { return(ValidationProblem(ModelState)); } this._mapper.Map(courseUpsertDto, courseEntity); this._repos.UpdateCourse(courseEntity); this._repos.Save(); return(NoContent()); }