Skip to content
Snippets Groups Projects
Commit 08763e51 authored by GAYDAMAKHA MIKHAIL's avatar GAYDAMAKHA MIKHAIL
Browse files

:recycle: Use dependency injection for categories controller

parent cc25d276
Branches
2 merge requests!25Develop,!19:recycle: Use dependency injection for categories controller
......@@ -5,6 +5,10 @@ plugins {
id 'war'
}
tasks {
compileJava.inputs.files(processResources)
}
group = 'fr.unistra.sil'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
......@@ -24,6 +28,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.thymeleaf:thymeleaf-spring5'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
javadoc {
......
......@@ -5,45 +5,47 @@
package fr.unistra.sil.erp.back;
import fr.unistra.sil.erp.back.interceptor.api.ApiAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Main configuration file for the application.
*
* @author BEAUVAIS ANTOINE
*/
@Configuration
@PropertySources(
@PropertySource("/apikey.properties")
)
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
/**
* Autowired environment for retrieving properties.
*/
@Autowired
private Environment env;
private final Environment env;
/**
* API version.
*/
public static final String API_VERSION = "v1";
/**
* API prefix.
*/
public static final String API_PREFIX = "/api/";
/**
* Prefix for API calls.
*/
public static final String API_FULL_PREFIX = API_PREFIX + API_VERSION;
/**
* API Mapping for retrieving all items.
*/
......@@ -72,29 +74,47 @@ public class WebMvcConfig implements WebMvcConfigurer {
* Web mapping for the login page.
*/
public static final String MAPPING_LOGIN = "/login";
/**
* Web mapping for the items list.
*/
public static final String WEB_MAPPING_ITEMS = "/products";
public WebMvcConfig(Environment env) {
this.env = env;
}
/**
* Adds interceptors to the application.
*
* Interceptors process HTTP requests before they reach their
* <p>
* Interceptors process HTTP requests before they reach their
* attributed methods.
*
* Currently, interceptors are only used for authentication in this
* <p>
* Currently, interceptors are only used for authentication in this
* application.
*
*
* @param registry the Interceptor Registry to use for additions.
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ApiAuthenticationInterceptor(
this.env.getProperty("api.key")))
.addPathPatterns(API_PREFIX + "**");
registry.addInterceptor(new ApiAuthenticationInterceptor(this.env.getRequiredProperty("api.key"))).addPathPatterns(API_PREFIX + "**");
}
/**
* SQL Connection object.
*/
private Connection conn;
@Bean
public Connection connection() {
if (this.conn == null) {
try {
conn = DriverManager.getConnection(env.getRequiredProperty("spring.datasource.url"));
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to connect to SQLite file.", ex);
}
}
return conn;
}
}
......@@ -5,50 +5,46 @@
package fr.unistra.sil.erp.back.controller.api;
import static fr.unistra.sil.erp.back.WebMvcConfig.MAPPING_GETCATEGORIES;
import fr.unistra.sil.erp.back.DatabaseSystem;
import fr.unistra.sil.erp.back.controller.IRetrieveCategoriesController;
import fr.unistra.sil.erp.back.model.Category;
import fr.unistra.sil.erp.back.db.DatabaseConnectionException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import fr.unistra.sil.erp.back.repository.ICategoriesRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import fr.unistra.sil.erp.back.db.IDatabase;
/**
* REST controller for the category list.
*
* @author BEAUVAIS ANTOINE
*/
@RestController
public class ApiRetrieveCategoriesController
implements IRetrieveCategoriesController {
public class ApiRetrieveCategoriesController implements IRetrieveCategoriesController {
private final ICategoriesRepository repository;
public ApiRetrieveCategoriesController(ICategoriesRepository repository) {
this.repository = repository;
}
/**
* Returns the list of categories in JSON format.
*
* @return the HTTP response.
* @throws ApiServerErrorException Database failure.
*/
@GetMapping(MAPPING_GETCATEGORIES)
@Override
public ResponseEntity<Object> getCategories() throws ApiServerErrorException
{
IDatabase 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)
public ResponseEntity<Object> getCategories() throws ApiServerErrorException {
List<Category> res = repository.getCategories();
if (res == null)
throw new ApiServerErrorException("Database failure.");
return new ResponseEntity<>(res, HttpStatus.OK);
}
}
package fr.unistra.sil.erp.back.repository;
import fr.unistra.sil.erp.back.model.Category;
import java.util.List;
public interface ICategoriesRepository {
/**
* Returns the list of all categories.
*
* @return the list of categories, or null if an error occurred.
*/
List<Category> getCategories();
}
package fr.unistra.sil.erp.back.repository;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@Repository
public class SqliteRepository {
/**
* SQL Connection object.
*/
private final Connection conn;
/**
* Class constructor.
*/
public SqliteRepository(Connection conn) {
this.conn = conn;
}
/**
* Executes a standard query, without parameters.
*
* @param query the string's query.
* @return the result set for this query.
*/
protected ResultSet query(String query) {
Statement stmt;
ResultSet rs;
try {
stmt = this.conn.createStatement();
rs = stmt.executeQuery(query);
} catch (SQLException ex) {
Logger.getLogger(this.getClass().getName()).log(
Level.SEVERE, "Failed to run query: " + query, ex);
return null;
}
return rs;
}
}
package fr.unistra.sil.erp.back.repository.category;
import fr.unistra.sil.erp.back.model.Category;
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.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@Repository
public class SqliteCategoryRepository extends SqliteRepository implements ICategoriesRepository {
/**
* Query used to retrieve all categories stored in the database.
*/
private static final String SQL_GETCATEGORIES =
"SELECT id, name FROM categories";
public SqliteCategoryRepository(Connection conn) {
super(conn);
}
@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(this.getClass().getName()).log(
Level.SEVERE, "Failed to fetch results.", ex);
return null;
}
return res;
}
}
/**
* Database interfaces and implementations.
*/
package fr.unistra.sil.erp.back.repository;
\ No newline at end of file
{
"properties": [
{
"name": "api.key",
"type": "java.lang.String",
"description": "Api key to access the server. Should be added to the HTTP-header ''."
}
]
}
\ No newline at end of file
spring.mvc.static-path-pattern=/static/**
api.key=someKey
spring.datasource.url=jdbc:sqlite:dev.db
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