public ActionResult Put(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_repository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _repository.GetCourse(authorId, courseId); // Insert. if (courseFromRepo == null) { var newCourse = _mapper.Map <Course>(course); newCourse.Id = courseId; _repository.AddCourse(authorId, newCourse); _repository.Save(); var courseDto = _mapper.Map <CourseDto>(newCourse); return(CreatedAtRoute("GetCourse", new { authorId, courseId }, courseDto)); } else // Update. { // As per REST, we are updating the representation. // Therefore, we should: // 1. Get representation of entity. // 2. Map incoming Dto to that representation. // 3. Map updated representation to entity. // We can mimick those steps with below code directly: _mapper.Map(course, courseFromRepo); _repository.UpdateCourse(courseFromRepo); _repository.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 IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (_courseLibraryRepository.AuthorExists(authorId) == false) { return(NotFound()); } var existingCourse = _courseLibraryRepository.GetCourse(authorId, courseId); if (existingCourse == null) { //return NotFound() => if we don't want to allow Upsertion //Upsertion allowed var courseToAdd = _mapper.Map <Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute(nameof(GetCourseForAuthor), new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } _mapper.Map(course, existingCourse); //UpdateCourse method is empty, the AutoMapper.Map is already setting the entity's state to changed //therefore all that's left is to call Save() on the context and it will update the entity _courseLibraryRepository.UpdateCourse(existingCourse); _courseLibraryRepository.Save(); //consider if we don't want to return 200 ok with the updated resource in the body return(NoContent()); }
public async Task <ActionResult <CourseForReturn> > UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdate course) { if (!await _repository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = await _repository.GetCourse(authorId, courseId); if (courseFromRepo == null) { var courseToAdd = _mapper.Map <Course>(course); _repository.AddCourse(authorId, courseToAdd); if (!await _repository.Save()) { return(BadRequest("Error Happens when saving in Data Base")); } var courseToReturn = _mapper.Map <CourseForReturn>(courseToAdd); return(CreatedAtRoute("getCourse", new { authorId = authorId, courseId = courseToAdd.Id }, courseToReturn)); } _mapper.Map(course, courseFromRepo); _repository.UpdateCourse(courseFromRepo); if (!await _repository.Save()) { return(BadRequest("Error Happens when saving in Data Base")); } return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); // implement upserting for put request if (courseForAuthorFromRepo == 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 entity to CurseForUpdateDto // apply the updated field values to that dto // map the courseUpdateDto back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); 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 courseEntityToAdd = _mapper.Map <Entities.Course>(courseForUpdateDto); courseEntityToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseEntityToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseEntityToAdd); return(CreatedAtRoute(nameof(GetCourseForAuthor), new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } //automapper will: //1) map the entity to a CourseForUpdateDto //2) apply the updated field values to that dto //3) map the CourseForUpdateDto back to an entity _mapper.Map(courseForUpdateDto, courseEntity); _courseLibraryRepository.UpdateCourse(courseEntity); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdate) { if (!courseRepo.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthor = courseRepo.GetCourse(authorId, courseId); if (courseForAuthor == null) { var courseToAdd = mapper.Map <Course>(courseForUpdate); courseToAdd.Id = courseId; courseRepo.AddCourse(authorId, courseToAdd); courseRepo.Save(); var courseToReturn = mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAuthor", new[] { authorId, courseId = courseToReturn.Id }, courseToReturn)); } mapper.Map(courseForUpdate, courseForAuthor); courseRepo.UpdateCourse(courseForAuthor); courseRepo.Save(); return(NoContent()); }
public ActionResult UpdateCourse(Guid authorId, Guid courseId, UpdateCourseDto updateCourseDto) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var course = _courseLibraryRepository.GetCourse(authorId, courseId); if (course == null) { var newCourse = _mapper.Map <Course>(updateCourseDto); newCourse.Id = courseId; _courseLibraryRepository.AddCourse(authorId, newCourse); _courseLibraryRepository.Save(); var courseDto = _mapper.Map <CourseDto>(newCourse); return(CreatedAtRoute( "GetCourse", new { authorId, courseId = courseDto.Id }, courseDto )); } _mapper.Map(updateCourseDto, course); _courseLibraryRepository.UpdateCourse(course); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdate) { //Check if author exists if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var authorCourse = _courseLibraryRepository.GetCourse(authorId, courseId); if (authorCourse == null) { //Upserting var courseToAdd = _mapper.Map <Course>(courseForUpdate); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CoursesDto>(courseToAdd); return(CreatedAtRoute("GetCourse", new { authorId = authorId, courseId = courseToReturn.Id }, courseToReturn)); } _mapper.Map(courseForUpdate, authorCourse); _courseLibraryRepository.UpdateCourse(authorCourse); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibaryService.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromEntity = _courseLibaryService.GetCourse(authorId, courseId); if (courseForAuthorFromEntity == null) { //maps course for input to entity Course var courserToAdd = _mapper.Map <Course>(course); courserToAdd.Id = courseId; _courseLibaryService.AddCourse(authorId, courserToAdd); _courseLibaryService.Save(); var courseToReturn = _mapper.Map <CourseDto>(courserToAdd); return(CreatedAtRoute("getCourseForAuthor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } //updates key values from one model to the other _mapper.Map(course, courseForAuthorFromEntity); _courseLibaryService.UpdateCourse(courseForAuthorFromEntity); _courseLibaryService.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorRepo == 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 _mapper.Map(course, courseForAuthorRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseEntity == null) { // Upserting course if there is no course existing for provided id var newCourse = _mapper.Map <Entities.Course>(course); newCourse.Id = courseId; _courseLibraryRepository.AddCourse(authorId, newCourse); _courseLibraryRepository.Save(); return(CreatedAtRoute("GetCourseForAuthor", new { authorId = authorId, courseId = newCourse.Id }, newCourse)); } // map the entity to a CourseForUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(course, 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); if (courseForAuthorFromRepo == null) { var courseToAdd = _mapper.Map <Entities.Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtAction(nameof(GetCourseForAuthor), new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } // map entity to CourseForUpdateDto // apply updated fields to that dto // map CourseForUpdateDto back to entity _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 courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseForAuthorFromRepo == 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)); } _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
public ActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto courseForUpdate) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var course = _courseLibraryRepository.GetCourse(authorId, courseId); if (course == null) { var courseForCreation = _mapper.Map <Course>(courseForUpdate); courseForCreation.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseForCreation); _courseLibraryRepository.Save(); var result = _mapper.Map <CourseDto>(courseForCreation); return(CreatedAtRoute("GetAuthorCourse", new { authorId = authorId, courseId = courseForCreation.Id }, result)); } _mapper.Map(courseForUpdate, course); _courseLibraryRepository.UpdateCourse(course); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, [FromBody] CourseForUpdateDto dto) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseEntity = _courseLibraryRepository.GetCourse(authorId, courseId); // Allow upserting if (courseEntity == null) { var courseToAdd = _mapper.Map <Course>(dto); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); var courseToReturn = _mapper.Map <CourseDto>(courseToAdd); return(CreatedAtRoute("GetCourseForAutor", new { authorId, courseId = courseToReturn.Id }, courseToReturn)); } _mapper.Map(dto, courseEntity); _courseLibraryRepository.UpdateCourse(courseEntity); _courseLibraryRepository.Save(); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpateDto course) //changed to IAction due to we are allowing to insert { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); //changing implementation to support also inserts if (courseForAuthorFromRepo == 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 updated field values to that dto // map the courseForAuthorFromRepo back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); _courseLibraryRepository.Save(); return(NoContent()); }
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 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 <Entities.Course>(course); courseToAdd.Id = courseId; _courseLibraryRepository.AddCourse(authorId, courseToAdd); _courseLibraryRepository.Save(); } //map the entity to a CourseForUpdateDto //applay 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 IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseFromRepo == null) { // return NotFound(); // to use put for creation => upsert 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 = authorId, courseId = courseToReturn.Id }, courseToReturn )); } // use auto mapper to map the entity tp a courseForUpdateDto //applay the update field values to that dto // map the courseForUpdateDto back to an entity (all in one step) _mapper.Map(course, courseFromRepo); _courseLibraryRepository.UpdateCourse(courseFromRepo); _courseLibraryRepository.Save(); // return Ok(_mapper.Map<CourseDto>(courseFromRepo)); return(NoContent()); }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseFromAuthorRepo = _courseLibraryRepository.GetCourse(authorId, courseId); if (courseFromAuthorRepo == null) { // Support Upsert. Add course if it does not exist 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 = authorId, courseId = courseToReturn.Id }, courseToReturn)); } //map entity to CourseForUpdateDto //apply updated field values to dto // map dto back to entity _mapper.Map(course, courseFromAuthorRepo); _courseLibraryRepository.UpdateCourse(courseFromAuthorRepo); _courseLibraryRepository.Save(); return(NoContent()); //204 No Content. Consumer can decide whether to retrieve the updated resource or not }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { try { if (!_courseLibraryRepository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = _courseLibraryRepository.GetCourse(authorId, courseId); //if (courseForAuthorFromRepo == null) //{ // _logger.LogError($"Owner with id: {id}, hasn't been found in db."); // return NotFound(); //} 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 update field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(course, courseForAuthorFromRepo); _courseLibraryRepository.UpdateCourse(courseForAuthorFromRepo); bool alterado = _courseLibraryRepository.Save(); if (alterado == false) { return(NotFound()); } return(Ok(course)); //return (alterado == true) ? Ok(course) as IActionResult : NotFound() as IActionResult; } catch (Exception ex) { // _logger.LogError($"Something went wrong inside UpdateOwner action: {ex.Message}"); return(StatusCode(500, $"Internal server error: {ex.Message}")); } }
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. }
public IActionResult UpdateCourseForAuthor(Guid authorId, Guid courseId, CourseForUpdateDto course) { if (!repository.AuthorExists(authorId)) { return(NotFound()); } var courseForAuthorFromRepo = repository.GetCourse(authorId, courseId); // if the course not found, new resource will be created (upserting with PUT) if (courseForAuthorFromRepo == null) { var courseToAdd = mapper.Map <Course>(course); courseToAdd.Id = courseId; repository.AddCourse(authorId, courseToAdd); repository.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(course, courseForAuthorFromRepo); repository.UpdateCourse(courseForAuthorFromRepo); repository.Save(); //return Ok(mapper.Map<CourseDto>(courseForAuthorFromRepo)); //or // return NoContent(); 204 status code with return type of ActionResult // both are valid return(NoContent()); //return Ok(new //{ // Message = "No updates has happend", // CourseDetails = mapper.Map<CourseDto>(courseForAuthorFromRepo) //}); }
public IActionResult Put(Guid authorId, Guid courseId, CourseUpdateDto courseUpdateDto) { if (!_repository.AuthorExists(authorId)) { return(NotFound()); } var course = _repository.GetCourse(authorId, courseId); if (course == null) { course = _mapper.Map <Course>(courseUpdateDto); course.Id = courseId; _repository.AddCourse(authorId, course); _repository.Save(); return(base.CreatedAtAction( "GetCourseForAuthor", new { authorId, courseId }, _mapper.Map <CourseDto>(course))); } _mapper.Map(courseUpdateDto, course); _repository.UpdateCourse(course); _repository.Save(); return(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 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 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 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()); }