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

Fixed stock insertion.

parent d2e6032a
Branches main
2 merge requests!38Fixed stock insertion.,!37Fixed stock insertion.
......@@ -14,6 +14,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import javax.validation.Valid;
import static fr.unistra.sil.erp.back.WebMvcConfig.MAPPING_ITEMS;
import fr.unistra.sil.erp.back.model.Stock;
import fr.unistra.sil.erp.back.service.StoreStockService;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* REST API to create a new item.
......@@ -25,13 +30,17 @@ public class ApiPostItemController {
* Service for storing POJO items.
*/
private final StoreItemService service;
private final StoreStockService stockService;
/**
* Constructor.
* @param service the service to store POJO items with.
* @param stockService
*/
public ApiPostItemController(StoreItemService service) {
public ApiPostItemController(StoreItemService service, StoreStockService stockService) {
this.service = service;
this.stockService = stockService;
}
/**
......@@ -56,6 +65,15 @@ public class ApiPostItemController {
} catch (CantStoreItemException e) {
throw new ApiServerErrorException(e.getMessage());
}
Stock s = new Stock(item, 0);
try
{
stockService.store(s);
} catch (SQLException ex) {
Logger.getLogger(ApiPostItemController.class.getName()).log(Level.SEVERE, null, ex);
throw new ApiServerErrorException(ex.getMessage());
}
return new ResponseEntity<>(item, HttpStatus.CREATED);
}
......
......@@ -40,6 +40,13 @@ public class Stock {
this.quantity = quantity;
}
public Stock(Item item, int quantity)
{
this.id = 0;
this.item = item;
this.quantity = quantity;
}
/**
* Returns the stock ID.
* @return the stock's ID.
......
package fr.unistra.sil.erp.back.repository;
import fr.unistra.sil.erp.back.model.Stock;
import java.sql.SQLException;
import java.util.List;
......@@ -23,6 +24,8 @@ public interface IStocksRepository {
* @return the stock line.
*/
Stock getStockForItem(int itemId);
public Stock insertStock(Stock s) throws SQLException;
/**
* Updates the specified stock's quantity.
......
......@@ -45,6 +45,8 @@ public class SqliteStocksRepository extends SqliteRepository implements IStocksR
private static final String SQL_GETSTOCKFORITEM = SQL_GETSTOCKS + " WHERE item = ?";
private static final String SQL_UPDATESTOCK = "UPDATE stocks SET quantity = ? WHERE item = ?";
private static final String SQL_INSERTSTOCK = "INSERT INTO stocks (item, quantity) VALUES (?, ?)";
/**
* Class constructor.
......@@ -130,6 +132,27 @@ public class SqliteStocksRepository extends SqliteRepository implements IStocksR
return s;
}
@Override
public Stock insertStock(Stock s) throws SQLException
{
PreparedStatement ps;
try {
ps = this.conn.prepareStatement(SQL_INSERTSTOCK);
ps.setInt(1, s.getItem().getId());
ps.setInt(2, s.getQuantity());
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Failed to connect to database.", ex);
return null;
}
try {
ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Failed to create item: " + ex.getMessage());
return null;
}
return s;
}
/**
* Updates the specified stock entry with the new quantity.
......
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CECILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.service;
import fr.unistra.sil.erp.back.model.Stock;
import fr.unistra.sil.erp.back.repository.IStocksRepository;
import java.sql.SQLException;
import org.springframework.stereotype.Service;
/**
*
* @author antoine.beauvais
*/
@Service
public class StoreStockService {
private final IStocksRepository repo;
public StoreStockService(IStocksRepository s)
{
this.repo = s;
}
public Stock store(Stock s) throws SQLException
{
this.repo.insertStock(s);
return s;
}
}
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