Skip to content
Snippets Groups Projects
Commit 3851e471 authored by BEAUVAIS ANTOINE's avatar BEAUVAIS ANTOINE
Browse files

Added category names in Item.

parent 85adeb75
Branches
1 merge request!31Added category names to Item JSON.
......@@ -50,7 +50,7 @@ public class ApiRetrieveItemsController implements IRetrieveInfoController {
) throws ApiServerErrorException, ApiBadRequestException {
List<Item> res;
if (cat.equals(""))
res = repository.getAllItems();
res = repository.getAllItemsWithCategory();
else {
int category;
try {
......
......@@ -47,6 +47,11 @@ public class Item {
* This product's category identifier.
*/
private final int category;
/**
Category name for JSON response.
*/
private String categoryName;
/**
* Class constructor.
......@@ -64,6 +69,27 @@ public class Item {
this.price = price;
this.subscriberPrice = subscriberPrice;
this.category = category;
this.categoryName = null;
}
/**
* Class constructor.
* @param id Item ID.
* @param name Item name.
* @param price Item price.
* @param subscriberPrice The item's price.
* @param category Item category.
* @param categoryName Item category name for JSON.
*/
public Item(Integer id, String name, BigDecimal price,
BigDecimal subscriberPrice, int category, String categoryName)
{
this.id = id;
this.name = name;
this.price = price;
this.subscriberPrice = subscriberPrice;
this.category = category;
this.categoryName = categoryName;
}
/**
......@@ -110,4 +136,13 @@ public class Item {
{
return this.category;
}
/**
* Returns the category name.
* @return the category name.
*/
public String getCategoryName()
{
return this.categoryName;
}
}
......@@ -16,6 +16,12 @@ public interface IItemsRepository {
*/
List<Item> getAllItems();
/**
* Returns the list of all items, with their category names.
* @return the list of all items with the categories, or null if error.
*/
List<Item> getAllItemsWithCategory();
/**
* Returns the list of all items for a given category.
* @param category the category's identifier.
......
......@@ -37,6 +37,10 @@ public class SqliteItemsRepository extends SqliteRepository implements IItemsRep
private static final String SQL_GETITEMSFROMCATEGORY = SQL_GETALLITEMS +
" WHERE category = ?";
private static final String SQL_GETITEMSWITHCATEGORY =
"SELECT i.id, i.name, i.price, i.subscriberPrice, i.category, c.name AS cName"
+ " FROM items i LEFT JOIN categories c ON i.category = c.id";
/**
* Class constructor.
* @param conn
......@@ -70,6 +74,32 @@ public class SqliteItemsRepository extends SqliteRepository implements IItemsRep
return res;
}
@Override
public List<Item> getAllItemsWithCategory() {
ResultSet rs = query(SQL_GETITEMSWITHCATEGORY);
if (rs == null)
return null;
List<Item> res = new ArrayList<>();
try {
while (rs.next()) {
Item item = new Item(rs.getInt("id"), rs.getString("name"),
rs.getBigDecimal("price"),
rs.getBigDecimal("subscriberPrice"),
rs.getInt("category"), rs.getString("cName"));
res.add(item);
}
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, null, ex);
return null;
}
return res;
}
@Override
public List<Item> getItemsFromCategory(int category) {
PreparedStatement ps;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment