Last active
December 25, 2017 05:30
-
-
Save dusanstanojeviccs/bc0f72470a9b54f0d03256d16b0fb372 to your computer and use it in GitHub Desktop.
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 controllers; | |
import com.google.inject.Inject; | |
import play.db.Database; | |
import play.libs.Json; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
public class BookController extends Controller { | |
@Inject public BookRepository bookRepository; | |
@Inject Database database; | |
public Result findAll() { | |
return database.withConnection(conn -> { | |
return ok(Json.toJson(bookRepository.findAll())).as("application/json"); | |
}); | |
} | |
public Result findById(int id) { | |
return database.withConnection(conn -> { | |
return ok(Json.toJson(bookRepository.findById(id))).as("application/json"); | |
}); | |
} | |
public Result create() { | |
return database.withConnection(conn -> { | |
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) { | |
return database.withConnection(conn -> { | |
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) { | |
return database.withConnection(conn -> { | |
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