* 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:
Magdalena Krause
2018-01-08 18:34:16 -03:00
committed by maibin
parent 727554bf47
commit cbd1a9dfbf
5 changed files with 79 additions and 86 deletions

View File

@@ -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);
}
}

View File

@@ -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");
}
}

View File

@@ -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;
}
}

View File

@@ -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"));
}
}