From 4be9fd3cec03a09ea284a7a8644be27a8be26895 Mon Sep 17 00:00:00 2001 From: BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr> Date: Tue, 21 Sep 2021 15:04:47 +0200 Subject: [PATCH] Added ability to retrieve categories. --- .../java/fr/unistra/sil/erp/back/Config.java | 2 + .../ApiRetrieveCategoriesController.java | 46 ++++++++++++++++ .../controller/ApiRetrieveInfoController.java | 4 +- .../sil/erp/back/api/model/Category.java | 53 +++++++++++++++++++ .../sil/erp/back/db/DatabaseInterface.java | 12 ++++- .../sil/erp/back/db/DatabaseSQLiteImpl.java | 28 +++++++++- 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveCategoriesController.java create mode 100644 src/main/java/fr/unistra/sil/erp/back/api/model/Category.java diff --git a/src/main/java/fr/unistra/sil/erp/back/Config.java b/src/main/java/fr/unistra/sil/erp/back/Config.java index 87aba07..a3de839 100644 --- a/src/main/java/fr/unistra/sil/erp/back/Config.java +++ b/src/main/java/fr/unistra/sil/erp/back/Config.java @@ -16,5 +16,7 @@ public class Config { public static final String MAPPING_RETRIEVEALL = URL_PREFIX + "/retrieveAll"; + public static final String MAPPING_GETCATEGORIES = URL_PREFIX + + "/retrieveCategories"; } diff --git a/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveCategoriesController.java b/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveCategoriesController.java new file mode 100644 index 0000000..7e512b5 --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveCategoriesController.java @@ -0,0 +1,46 @@ +/* + * 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); + } + +} diff --git a/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveInfoController.java b/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveInfoController.java index 5607af3..3593e99 100644 --- a/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveInfoController.java +++ b/src/main/java/fr/unistra/sil/erp/back/api/controller/ApiRetrieveInfoController.java @@ -42,8 +42,8 @@ public class ApiRetrieveInfoController { db = DatabaseSystem.getInstance(); } catch (DatabaseConnectionException ex) { Logger.getLogger(ApiRetrieveInfoController.class.getName()).log( - Level.SEVERE, "Database failure.", ex); - throw new ApiServerErrorException("Failed to connect to database."); + Level.SEVERE, "Could not connect to database.", ex); + throw new ApiServerErrorException("Database failure."); } List<Item> res; diff --git a/src/main/java/fr/unistra/sil/erp/back/api/model/Category.java b/src/main/java/fr/unistra/sil/erp/back/api/model/Category.java new file mode 100644 index 0000000..d27c80f --- /dev/null +++ b/src/main/java/fr/unistra/sil/erp/back/api/model/Category.java @@ -0,0 +1,53 @@ +/* + * 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; + } +} diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java index 645037e..4c4b617 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseInterface.java @@ -4,6 +4,7 @@ */ 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 java.util.List; @@ -19,15 +20,22 @@ public interface DatabaseInterface { /** * 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(); /** * Returns the list of all items for a given category. * @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); + /** + * Returns the list of all categories. + * + * @return the list of categories, or null if an error occurred. + */ + public List<Category> getCategories(); + } diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java index e8f5143..ce99530 100644 --- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java +++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java @@ -4,6 +4,7 @@ */ 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 java.sql.Connection; import java.sql.DriverManager; @@ -29,6 +30,9 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { private static final String SQL_GETITEMSFROMCATEGORY = SQL_GETALLITEMS + " WHERE category = ?"; + private static final String SQL_GETCATEGORIES = + "SELECT id, name FROM categories"; + private Connection conn; public DatabaseSQLiteImpl() @@ -47,7 +51,7 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { ResultSet rs; try { stmt = this.conn.createStatement(); - rs = stmt.executeQuery(SQL_GETALLITEMS); + rs = stmt.executeQuery(query); } catch (SQLException ex) { Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log( Level.SEVERE, "Failed to run query: " + query, ex); @@ -130,5 +134,27 @@ public class DatabaseSQLiteImpl implements DatabaseInterface { 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; + } } -- GitLab