This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.pivotal.springSchool; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | |
import org.springframework.web.bind.annotation.*; | |
import org.springframework.dao.DataIntegrityViolationException; | |
import java.net.URI; | |
import java.util.Optional; | |
@RestController | |
@RequestMapping("/users/{userId}/lessons/{lessonId}/feedback") | |
public class FeedbackController { | |
@Autowired | |
private FeedbackRepository feedbackRepository; | |
@Autowired | |
private UserLessonRepository userLessonRepository; | |
@RequestMapping(method = RequestMethod.POST) | |
ResponseEntity<?> createFeedback(@PathVariable Long userId, @PathVariable Long lessonId, @RequestBody Feedback feedback) { | |
Optional<UserLesson> userLesson = this.userLessonRepository.findByUserIdAndLessonId(userId, lessonId).stream().findFirst(); | |
if (!userLesson.isPresent()) { | |
throw new FeedbackNotCreatedException(userId, lessonId, "the lesson has not been started by this user"); | |
} | |
feedback.setUserLesson(userLesson.get()); | |
Feedback savedFeedback; | |
try { | |
savedFeedback = this.feedbackRepository.save(feedback); | |
} | |
catch (DataIntegrityViolationException ex) { | |
throw new FeedbackNotCreatedException(userId, lessonId, "feedback already exists"); | |
} | |
FeedbackResource feedbackResource = new FeedbackResource(savedFeedback); | |
HttpHeaders httpHeaders = new HttpHeaders(); | |
httpHeaders.setLocation(URI.create(feedbackResource.getLink("self").getHref())); | |
return new ResponseEntity<FeedbackResource>(feedbackResource, httpHeaders, HttpStatus.CREATED); | |
} | |
@RequestMapping(method = RequestMethod.PUT) | |
FeedbackResource updateFeedback(@PathVariable Long userId, @PathVariable Long lessonId, @RequestBody Feedback feedback) { | |
Feedback currentFeedback = this.feedbackRepository.findOne(feedback.getId()); | |
if (currentFeedback == null) { | |
throw new FeedbackNotFoundException(userId, lessonId); | |
} | |
feedback.setUserLesson(currentFeedback.getUserLesson()); | |
this.feedbackRepository.save(feedback); | |
return new FeedbackResource(feedback); | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
FeedbackResource findFeedback(@PathVariable Long userId, @PathVariable Long lessonId) { | |
Optional<Feedback> feedback = this.feedbackRepository.findByUserLessonUserIdAndUserLessonLessonId(userId, lessonId).stream().findFirst(); | |
if (!feedback.isPresent()) { | |
throw new FeedbackNotFoundException(userId, lessonId); | |
} | |
return new FeedbackResource(feedback.get()); | |
} | |
@ResponseStatus(HttpStatus.NOT_FOUND) | |
class FeedbackNotFoundException extends RuntimeException { | |
public FeedbackNotFoundException(Long userId, Long lessonId) { | |
super("Feedback not found for user '" + userId + "' on lesson '" + lessonId + "'"); | |
} | |
} | |
@ResponseStatus(HttpStatus.FORBIDDEN) | |
class FeedbackNotCreatedException extends RuntimeException { | |
public FeedbackNotCreatedException(Long userId, Long lessonId, String reason) { | |
super("Feedback cannot be created for user '" + userId + "' on lesson '" + lessonId + "' because " + reason); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment