Last active
December 25, 2017 05:23
-
-
Save dusanstanojeviccs/b73563dc4d0eacf789c437e2aca61b61 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 controllers; | |
| import com.google.inject.Inject; | |
| import play.libs.Json; | |
| import play.mvc.Controller; | |
| import play.mvc.Result; | |
| public class BookController extends Controller { | |
| @Inject | |
| public BookRepository bookRepository; | |
| public Result findAll() { | |
| return ok(Json.toJson(bookRepository.findAll())).as("application/json"); | |
| } | |
| public Result findById(int id) { | |
| return ok(Json.toJson(bookRepository.findById(id))).as("application/json"); | |
| } | |
| public Result create() { | |
| Book bookRequest = Json.fromJson(request().body().asJson(), Book.class); | |
| bookRepository.add(bookRequest); | |
| return ok(Json.toJson(bookRequest)).as("application/json"); | |
| } | |
| public Result update(int id) { | |
| Book bookRequest = Json.fromJson(request().body().asJson(), Book.class); | |
| bookRequest.setId(id); | |
| bookRepository.update(bookRequest); | |
| return ok(Json.toJson(bookRequest)).as("application/json"); | |
| } | |
| public Result delete(int id) { | |
| bookRepository.delete(id); | |
| return ok("{}").as("application/json"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment