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

Added ability to retrieve categories.

parent f4c87d12
No related merge requests found
...@@ -16,5 +16,7 @@ public class Config { ...@@ -16,5 +16,7 @@ public class Config {
public static final String MAPPING_RETRIEVEALL = URL_PREFIX + public static final String MAPPING_RETRIEVEALL = URL_PREFIX +
"/retrieveAll"; "/retrieveAll";
public static final String MAPPING_GETCATEGORIES = URL_PREFIX +
"/retrieveCategories";
} }
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.api.controller;
import static fr.unistra.sil.erp.back.Config.MAPPING_GETCATEGORIES;
import fr.unistra.sil.erp.back.DatabaseSystem;
import fr.unistra.sil.erp.back.api.model.Category;
import fr.unistra.sil.erp.back.db.DatabaseConnectionException;
import fr.unistra.sil.erp.back.db.DatabaseInterface;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* REST controller for the category list.
* @author BEAUVAIS ANTOINE
*/
@RestController
public class ApiRetrieveCategoriesController {
@GetMapping(MAPPING_GETCATEGORIES)
public ResponseEntity<Object> getCategories() throws ApiServerErrorException
{
DatabaseInterface db;
try {
db = DatabaseSystem.getInstance();
} catch (DatabaseConnectionException ex) {
Logger.getLogger(ApiRetrieveCategoriesController.class.getName())
.log(Level.SEVERE, "Failed to connect to database.", ex);
throw new ApiServerErrorException("Database failure.");
}
List<Category> res = db.getCategories();
if(res == null)
throw new ApiServerErrorException("Database failure.");
return new ResponseEntity<>(res, HttpStatus.OK);
}
}
...@@ -42,8 +42,8 @@ public class ApiRetrieveInfoController { ...@@ -42,8 +42,8 @@ public class ApiRetrieveInfoController {
db = DatabaseSystem.getInstance(); db = DatabaseSystem.getInstance();
} catch (DatabaseConnectionException ex) { } catch (DatabaseConnectionException ex) {
Logger.getLogger(ApiRetrieveInfoController.class.getName()).log( Logger.getLogger(ApiRetrieveInfoController.class.getName()).log(
Level.SEVERE, "Database failure.", ex); Level.SEVERE, "Could not connect to database.", ex);
throw new ApiServerErrorException("Failed to connect to database."); throw new ApiServerErrorException("Database failure.");
} }
List<Item> res; List<Item> res;
......
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.api.model;
/**
* Category entity.
*
* Categories allow the API caller to sort items by category.
* @author BEAUVAIS ANTOINE
*/
public class Category {
/**
* The category's identifier.
*/
private final int id;
/**
* The category's name.
*/
private final String name;
/**
* Class constructor.
* @param id the category's identifier.
* @param name the category's name.
*/
public Category(int id, String name)
{
this.id = id;
this.name = name;
}
/**
* Returns the current category's identifier.
* @return the category's identifier.
*/
public int getId()
{
return this.id;
}
/**
* Returns the current category's name.
* @return the category's name.
*/
public String getName()
{
return this.name;
}
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
package fr.unistra.sil.erp.back.db; package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.api.model.Category;
import fr.unistra.sil.erp.back.api.model.Item; import fr.unistra.sil.erp.back.api.model.Item;
import java.util.List; import java.util.List;
...@@ -19,15 +20,22 @@ public interface DatabaseInterface { ...@@ -19,15 +20,22 @@ public interface DatabaseInterface {
/** /**
* Returns the list of all items. * Returns the list of all items.
* @return the list of all items. * @return the list of all items, or null if an error occurred.
*/ */
public List<Item> getAllItems(); public List<Item> getAllItems();
/** /**
* Returns the list of all items for a given category. * Returns the list of all items for a given category.
* @param category the category's identifier. * @param category the category's identifier.
* @return the list of items. * @return the list of items, or null if an error occurred.
*/ */
public List<Item> getItemsFromCategory(int category); public List<Item> getItemsFromCategory(int category);
/**
* Returns the list of all categories.
*
* @return the list of categories, or null if an error occurred.
*/
public List<Category> getCategories();
} }
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
*/ */
package fr.unistra.sil.erp.back.db; package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.api.model.Category;
import fr.unistra.sil.erp.back.api.model.Item; import fr.unistra.sil.erp.back.api.model.Item;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
...@@ -29,6 +30,9 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { ...@@ -29,6 +30,9 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
private static final String SQL_GETITEMSFROMCATEGORY = SQL_GETALLITEMS + private static final String SQL_GETITEMSFROMCATEGORY = SQL_GETALLITEMS +
" WHERE category = ?"; " WHERE category = ?";
private static final String SQL_GETCATEGORIES =
"SELECT id, name FROM categories";
private Connection conn; private Connection conn;
public DatabaseSQLiteImpl() public DatabaseSQLiteImpl()
...@@ -47,7 +51,7 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { ...@@ -47,7 +51,7 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
ResultSet rs; ResultSet rs;
try { try {
stmt = this.conn.createStatement(); stmt = this.conn.createStatement();
rs = stmt.executeQuery(SQL_GETALLITEMS); rs = stmt.executeQuery(query);
} catch (SQLException ex) { } catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, "Failed to run query: " + query, ex); Level.SEVERE, "Failed to run query: " + query, ex);
...@@ -130,5 +134,27 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { ...@@ -130,5 +134,27 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
return res; return res;
} }
@Override
public List<Category> getCategories() {
ResultSet rs = this.query(SQL_GETCATEGORIES);
if(rs == null)
return null;
List<Category> res = new ArrayList<>();
try {
while(rs.next())
{
Category c = new Category(rs.getInt("id"), rs.getString("name"));
res.add(c);
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, "Failed to fetch results.", ex);
return null;
}
return res;
}
} }
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