Bael 1277 (#3379)
* BAEL-1277: RESTFul CRUD application with JavaLite. * BAEL-1277: RESTFul CRUD application with JavaLite. Adding exception handling. * BAEL-1277: Changes after editors review.
This commit is contained in:
@@ -9,7 +9,7 @@ import org.javalite.activeweb.controller_filters.TimingFilter;
|
||||
public class AppControllerConfig extends AbstractControllerConfig {
|
||||
@Override
|
||||
public void init(AppContext appContext) {
|
||||
addGlobalFilters(new TimingFilter());
|
||||
add(new DBConnectionFilter()).to(ProductsController.class);
|
||||
addGlobalFilters(new TimingFilter());
|
||||
add(new DBConnectionFilter()).to(ProductsController.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,6 @@ import org.javalite.activeweb.AppContext;
|
||||
public class DbConfig extends AbstractDBConfig {
|
||||
@Override
|
||||
public void init(AppContext appContext) {
|
||||
this.configFile("/database.properties");
|
||||
this.configFile("/database.properties");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,92 +10,94 @@ import java.util.Map;
|
||||
@RESTful
|
||||
public class ProductsController extends AppController {
|
||||
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public void index() {
|
||||
try {
|
||||
view("products", Product.findAll());
|
||||
render().contentType("application/json");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
try {
|
||||
view("products", Product.findAll());
|
||||
render().contentType("application/json");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
}
|
||||
|
||||
public void create() {
|
||||
try {
|
||||
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
|
||||
Product p = new Product();
|
||||
p.fromMap(payload);
|
||||
p.saveIt();
|
||||
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
try {
|
||||
Map payload = mapper.readValue(getRequestString(), Map.class);
|
||||
Product p = new Product();
|
||||
p.fromMap(payload);
|
||||
p.saveIt();
|
||||
view("message", "Successfully saved product id " + p.get("id"), "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
try {
|
||||
Map payload = new ObjectMapper().readValue(getRequestString(), Map.class);
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
try {
|
||||
Map payload = mapper.readValue(getRequestString(), Map.class);
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
}
|
||||
p.fromMap(payload);
|
||||
p.saveIt();
|
||||
view("message", "Successfully updated product id " + id, "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
p.fromMap(payload);
|
||||
p.saveIt();
|
||||
view("message", "Successfully updated product id " + id, "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
}
|
||||
|
||||
public void show() {
|
||||
try {
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
try {
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
}
|
||||
view("product", p);
|
||||
render("_product");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
view("product", p);
|
||||
render("_product");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
try {
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
try {
|
||||
String id = getId();
|
||||
Product p = Product.findById(id);
|
||||
if (p == null) {
|
||||
view("message", "Product id " + id + " not found.", "code", 200);
|
||||
render("message");
|
||||
return;
|
||||
}
|
||||
p.delete();
|
||||
view("message", "Successfully deleted product id " + id, "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
p.delete();
|
||||
view("message", "Successfully deleted product id " + id, "code", 200);
|
||||
render("message");
|
||||
} catch (Exception e) {
|
||||
view("message", "There was an error.", "code", 200);
|
||||
render("message");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getContentType() {
|
||||
return "application/json";
|
||||
return "application/json";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLayout() {
|
||||
return null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,18 +8,18 @@ public class ProductTest {
|
||||
|
||||
//@Test
|
||||
public void givenSavedProduct_WhenFindFirst_ThenSavedProductIsReturned() {
|
||||
//Open DB connection
|
||||
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
|
||||
//Open DB connection
|
||||
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/dbname", "user", "password");
|
||||
|
||||
//Create a product and save it
|
||||
Product toSaveProduct = new Product();
|
||||
toSaveProduct.set("name", "Bread");
|
||||
toSaveProduct.saveIt();
|
||||
//Create a product and save it
|
||||
Product toSaveProduct = new Product();
|
||||
toSaveProduct.set("name", "Bread");
|
||||
toSaveProduct.saveIt();
|
||||
|
||||
//Find product
|
||||
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
||||
//Find product
|
||||
Product savedProduct = Product.findFirst("name = ?", "Bread");
|
||||
|
||||
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
||||
Assert.assertEquals(toSaveProduct.get("name"), savedProduct.get("name"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user