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

Added POST Category.

parent 029b39b5
1 merge request!28Category and doc update to Master.
......@@ -55,7 +55,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
/**
* API Mapping for retrieving all categories.
*/
public static final String MAPPING_GETCATEGORIES = API_FULL_PREFIX +
public static final String MAPPING_CATEGORY = API_FULL_PREFIX +
"/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 com.google.gson.Gson;
import com.google.gson.JsonParseException;
import static fr.unistra.sil.erp.back.WebMvcConfig.MAPPING_CATEGORY;
import fr.unistra.sil.erp.back.model.Category;
import fr.unistra.sil.erp.back.repository.DatabaseConnectionException;
import fr.unistra.sil.erp.back.repository.DatabaseUpdateException;
import fr.unistra.sil.erp.back.repository.ICategoriesRepository;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* REST Controller for creating a category.
* @author BEAUVAIS ANTOINE
*/
@RestController
public class ApiPostCategoryController {
private ICategoriesRepository repository;
public ApiPostCategoryController(ICategoriesRepository repo)
{
this.repository = repo;
}
@RequestMapping(value = MAPPING_CATEGORY, method = RequestMethod.POST)
public ResponseEntity<Object> postCategory(HttpServletRequest request,
HttpServletResponse response)
throws ApiServerErrorException, ApiBadRequestException
{
Gson gson = new Gson();
String body;
try {
body = request.getReader().lines()
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException ex) {
Logger.getLogger(ApiSubmitTransactionController.class.getName())
.log(Level.SEVERE, "Unparseable body.", ex);
throw new ApiBadRequestException("Unparseable body.");
}
if (body == null)
throw new ApiBadRequestException("Missing JSON body.");
Category c;
try {
c = gson.fromJson(body, Category.class);
} catch (JsonParseException ex) {
throw new ApiBadRequestException("Invalid JSON: syntax error.");
}
if (c == null)
throw new ApiBadRequestException("Missing JSON body.");
if(c.getName() == null || c.getName().equals("")) {
throw new ApiBadRequestException("Missing category name.");
}
try {
this.repository.createCategory(c);
} catch (DatabaseConnectionException ex) {
Logger.getLogger(ApiPostCategoryController.class.getName()).log(Level.SEVERE, null, ex);
throw new ApiServerErrorException("Database connection failure.");
} catch (DatabaseUpdateException ex) {
Logger.getLogger(ApiPostCategoryController.class.getName()).log(Level.SEVERE, null, ex);
throw new ApiServerErrorException("Database update failure.");
}
return new ResponseEntity<>(this.repository.getLatestCategory(),
HttpStatus.CREATED);
}
}
......@@ -4,7 +4,7 @@
*/
package fr.unistra.sil.erp.back.controller.api;
import static fr.unistra.sil.erp.back.WebMvcConfig.MAPPING_GETCATEGORIES;
import static fr.unistra.sil.erp.back.WebMvcConfig.MAPPING_CATEGORY;
import fr.unistra.sil.erp.back.controller.IRetrieveCategoriesController;
import fr.unistra.sil.erp.back.model.Category;
......@@ -37,7 +37,7 @@ public class ApiRetrieveCategoriesController implements IRetrieveCategoriesContr
* @return the HTTP response.
* @throws ApiServerErrorException Database failure.
*/
@GetMapping(MAPPING_GETCATEGORIES)
@GetMapping(MAPPING_CATEGORY)
@Override
public ResponseEntity<Object> getCategories() throws ApiServerErrorException {
List<Category> res = repository.getCategories();
......
......@@ -12,4 +12,21 @@ public interface ICategoriesRepository {
* @return the list of categories, or null if an error occurred.
*/
List<Category> getCategories();
/**
* Retrieves the category with the highest ID.
* @return the category.
*/
Category getLatestCategory();
/**
* Creates a new category in the database.
*
* @param name the name for the new Category.
* @return the created Category.
* @throws fr.unistra.sil.erp.back.repository.DatabaseConnectionException
* @throws fr.unistra.sil.erp.back.repository.DatabaseUpdateException
*/
Category createCategory(Category c)
throws DatabaseConnectionException, DatabaseUpdateException;
}
package fr.unistra.sil.erp.back.repository.category;
import fr.unistra.sil.erp.back.model.Category;
import fr.unistra.sil.erp.back.repository.DatabaseConnectionException;
import fr.unistra.sil.erp.back.repository.DatabaseResourceNotFoundException;
import fr.unistra.sil.erp.back.repository.DatabaseUpdateException;
import fr.unistra.sil.erp.back.repository.ICategoriesRepository;
import fr.unistra.sil.erp.back.repository.SqliteRepository;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
......@@ -13,6 +17,10 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* SQLite Category SQL queries.
* @author BEAUVAIS ANTOINE
*/
@Repository
public class SqliteCategoryRepository extends SqliteRepository implements ICategoriesRepository {
......@@ -21,6 +29,21 @@ public class SqliteCategoryRepository extends SqliteRepository implements ICateg
*/
private static final String SQL_GETCATEGORIES =
"SELECT id, name FROM categories";
/**
* Query used to retrieve one category stored in the database.
*/
private static final String SQL_GETCATEGORY =
"SELECT id, name FROM categories WHERE id = ?";
private static final String SQL_GETLASTCATEGORY =
"SELECT MAX(id), name FROM categories";
/**
* Query used to create a new category.
*/
private static final String SQL_CREATECATEGORY =
"INSERT INTO categories (name) VALUES (?)";
public SqliteCategoryRepository(Connection conn) {
super(conn);
......@@ -46,4 +69,105 @@ public class SqliteCategoryRepository extends SqliteRepository implements ICateg
return res;
}
public Category getCategory(int id) throws DatabaseResourceNotFoundException
{
PreparedStatement ps;
try {
ps = this.conn.prepareStatement(SQL_GETCATEGORY);
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to connect to database.", ex);
return null;
}
try {
ps.setInt(1, id);
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to set query parameter.", ex);
return null;
}
ResultSet rs;
try {
rs = ps.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to fetch stock for item " +
id, ex);
return null;
}
Category c;
try {
if (rs.next())
c = new Category(rs.getInt("id"), rs.getString("name"));
else
throw new DatabaseResourceNotFoundException("Stock not found.");
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to parse results.", ex);
return null;
}
return c;
}
/**
* Retrieves the latest category.
* @return
*/
@Override
public Category getLatestCategory()
{
ResultSet rs = this.query(SQL_GETCATEGORIES);
if (rs == null)
return null;
Category c = null;
try {
while (rs.next()) {
c = new Category(rs.getInt("id"), rs.getString("name"));
}
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to fetch results.", ex);
return null;
}
return c;
}
@Override
public Category createCategory(Category c)
throws DatabaseConnectionException, DatabaseUpdateException
{
PreparedStatement ps;
try {
ps = this.conn.prepareStatement(SQL_CREATECATEGORY);
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to connect to database.", ex);
throw new DatabaseConnectionException("Failed to connect to DB.");
}
try {
ps.setString(1, c.getName());
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to set query parameter.", ex);
throw new DatabaseUpdateException("Failed to set query params.");
}
try {
ps.execute();
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName())
.log(Level.SEVERE, "Failed to execute query.", ex);
throw new DatabaseUpdateException("Failed to execute query.");
}
return c;
}
}
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