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

Added a mapping to retrieve stocks info.

parent 037cdf5d
1 merge request!1Merge 'develop' changes into 'master'.
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
......@@ -89,5 +89,5 @@ INSERT INTO registry (id, dt, type, account_id, debit, credit, remarks)
INSERT INTO stocks (id, item, quantity)
VALUES(1, 1, 25),
VALUES(2, 2, 12),
VALUES(3, 3, 50);
\ No newline at end of file
(2, 2, 12),
(3, 3, 50);
\ No newline at end of file
......@@ -22,4 +22,6 @@ public class Config {
public static final String MAPPING_SUBTRANSAC = URL_PREFIX +
"/submitTransaction";
public static final String MAPPING_GETSTOCKS = URL_PREFIX +
"/retrieveStocks";
}
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
/**
* Interface for designing stocks access.
* @author BEAUVAIS ANTOINE
*/
public interface IRetrieveStocks {
public ResponseEntity<Object> retrieveStocks(HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
......@@ -5,6 +5,7 @@
package fr.unistra.sil.erp.back.controller.api;
import static fr.unistra.sil.erp.back.Config.MAPPING_GETCATEGORIES;
import static fr.unistra.sil.erp.back.Config.MAPPING_GETSTOCKS;
import fr.unistra.sil.erp.back.DatabaseSystem;
import fr.unistra.sil.erp.back.controller.IRetrieveCategoriesController;
import fr.unistra.sil.erp.back.model.Category;
......
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.controller.api;
import static fr.unistra.sil.erp.back.Config.MAPPING_GETSTOCKS;
import fr.unistra.sil.erp.back.DatabaseSystem;
import fr.unistra.sil.erp.back.controller.IRetrieveStocks;
import fr.unistra.sil.erp.back.db.DatabaseConnectionException;
import fr.unistra.sil.erp.back.db.DatabaseInterface;
import fr.unistra.sil.erp.back.model.Stock;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Retrieves stocks in JSON format.
* @author BEAUVAIS ANTOINE
*/
@RestController
public class ApiRetrieveStocks implements IRetrieveStocks {
/**
* Sends the stocks as JSON format back to the client.
* @param request the HTTP request.
* @param response the HTTP response.
* @return the response.
* @throws fr.unistra.sil.erp.back.controller.api.ApiServerErrorException
*/
@GetMapping(MAPPING_GETSTOCKS)
@Override
public ResponseEntity<Object> retrieveStocks(HttpServletRequest request,
HttpServletResponse response) 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<Stock> res = db.getStocks();
if(res == null)
throw new ApiServerErrorException("Database failure.");
return new ResponseEntity<>(res, HttpStatus.OK);
}
}
......@@ -6,6 +6,7 @@ package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.model.Category;
import fr.unistra.sil.erp.back.model.Item;
import fr.unistra.sil.erp.back.model.Stock;
import java.util.List;
/**
......@@ -38,4 +39,11 @@ public interface DatabaseInterface {
*/
public List<Category> getCategories();
/**
* Returns the list of all stocks.
*
* @return the list of stocks.
*/
public List<Stock> getStocks();
}
......@@ -6,6 +6,7 @@ package fr.unistra.sil.erp.back.db;
import fr.unistra.sil.erp.back.model.Category;
import fr.unistra.sil.erp.back.model.Item;
import fr.unistra.sil.erp.back.model.Stock;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
......@@ -18,7 +19,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* SQLite implementation of the Database interface.
* @author BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
*/
public class DatabaseSQLiteImpl implements DatabaseInterface {
......@@ -33,6 +34,11 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
private static final String SQL_GETCATEGORIES =
"SELECT id, name FROM categories";
private static final String SQL_GETSTOCKS =
"SELECT s.id AS id, s.item AS item, " +
"i.name AS name, s.quantity AS quantity FROM stocks s " +
"INNER JOIN items i ON s.item = i.ref";
private Connection conn;
public DatabaseSQLiteImpl()
......@@ -156,5 +162,29 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
return res;
}
@Override
public List<Stock> getStocks() {
ResultSet rs = this.query(SQL_GETSTOCKS);
if(rs == null)
return null;
List<Stock> res = new ArrayList<>();
try {
while(rs.next())
{
Stock s = new Stock(rs.getInt("id"),
rs.getInt("item"), rs.getString("name"),
rs.getInt("quantity"));
res.add(s);
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseSQLiteImpl.class.getName()).log(
Level.SEVERE, "Failed to fetch results.", ex);
return null;
}
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.model;
/**
* Representation of a stock entry.
* @author BEAUVAIS ANTOINE
*/
public class Stock {
private final int id;
private final int itemId;
private final String itemName;
private final int quantity;
public Stock(int id, int itemId, String itemName, int quantity)
{
this.id = id;
this.itemId = itemId;
this.itemName = itemName;
this.quantity = quantity;
}
public int getId()
{
return this.id;
}
public int getItemId()
{
return this.itemId;
}
public String getItemName()
{
return this.itemName;
}
public int getQuantity()
{
return this.quantity;
}
}
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