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

SQLite database and proper JSON response.

parent f3b016ae
No related merge requests found
...@@ -35,3 +35,6 @@ out/ ...@@ -35,3 +35,6 @@ out/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
### SQLite ###
dev.db
\ No newline at end of file
...@@ -14,6 +14,9 @@ repositories { ...@@ -14,6 +14,9 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.xerial:sqlite-jdbc:3.36.0.3'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
......
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back;
import fr.unistra.sil.erp.back.db.DatabaseConnectionException;
import fr.unistra.sil.erp.back.db.DatabaseInterface;
import fr.unistra.sil.erp.back.db.DatabaseSQLiteImpl;
/**
* Manages the database implementation to use.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
public class DatabaseSystem {
/**
* The database implementation object.
*/
private static DatabaseInterface instance;
/**
* Returns the database implementation in use.In order to change the
* implementation, simply change this method's implementation construction
* to the desired one.
*
* @return the instance of the database implementation.
* @throws fr.unistra.sil.erp.back.db.DatabaseConnectionException
*/
public static DatabaseInterface getInstance() throws DatabaseConnectionException
{
if(DatabaseSystem.instance == null)
DatabaseSystem.instance = new DatabaseSQLiteImpl();
if(DatabaseSystem.instance == null)
throw new DatabaseConnectionException();
return DatabaseSystem.instance;
}
}
...@@ -4,7 +4,13 @@ ...@@ -4,7 +4,13 @@
*/ */
package fr.unistra.sil.erp.back.api.controller; package fr.unistra.sil.erp.back.api.controller;
import fr.unistra.sil.erp.back.DatabaseSystem;
import fr.unistra.sil.erp.back.api.model.Item; import fr.unistra.sil.erp.back.api.model.Item;
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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -20,8 +26,21 @@ public class ApiRetrieveInfoController { ...@@ -20,8 +26,21 @@ public class ApiRetrieveInfoController {
* @return an Item. * @return an Item.
*/ */
@GetMapping("/erp-back/api/v1/retrieveInfo") @GetMapping("/erp-back/api/v1/retrieveInfo")
public Item retrieveInfo() public List<Item> retrieveInfo()
{ {
return new Item(1, "Test", 1.0, 0.5); DatabaseInterface db;
try {
db = DatabaseSystem.getInstance();
} catch (DatabaseConnectionException ex) {
Logger.getLogger(ApiRetrieveInfoController.class.getName()).log(
Level.SEVERE, "Database failure.", ex);
throw new ApiServerErrorController("Connection to database failed");
}
List<Item> res = db.getAllItems();
if(res == null)
throw new ApiServerErrorController("Failed to retrieve list.");
return 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.controller;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Returns HTTP 500 error page.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class ApiServerErrorController extends RuntimeException {
public ApiServerErrorController(String errMsg)
{
super(errMsg);
}
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.db;
/**
*
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
public class DatabaseConnectionException extends Exception {
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.api.model.Item;
import java.util.List;
/**
* Standard interface for database implementations.
*
* This interface allows the application to be able to use different
* database systems (CSV, SQLite, MySQL...) by simply switching
* implementations.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
public interface DatabaseInterface {
/**
* Returns the list of all items.
* @return the list of all items.
*/
public List<Item> getAllItems();
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.api.model.Item;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
public class DatabaseSQLiteImpl implements DatabaseInterface {
private static final String CONNECTION_URL = "jdbc:sqlite:dev.db";
private static final String SQL_GETALLITEMS =
"SELECT ref, name, price, subscriberPrice FROM items";
private Connection conn;
public DatabaseSQLiteImpl()
{
try {
conn = DriverManager.getConnection(CONNECTION_URL);
} catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, "Failed to connect to SQLite file.", ex);
}
}
private ResultSet query(String query)
{
Statement stmt;
ResultSet rs;
try {
stmt = this.conn.createStatement();
rs = stmt.executeQuery(SQL_GETALLITEMS);
} catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, "Failed to run query: " + query, ex);
return null;
}
return rs;
}
@Override
public List<Item> getAllItems() {
ResultSet rs = query(SQL_GETALLITEMS);
if(rs == null)
return null;
List<Item> res = new ArrayList<>();
try {
while(rs.next())
{
Item item = new Item(rs.getInt("ref"), rs.getString("name"),
rs.getDouble("price"),
rs.getDouble("subscriberPrice"));
res.add(item);
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, null, 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