preparing item service for use in document service
This commit is contained in:
8
items/build.gradle.kts
Normal file
8
items/build.gradle.kts
Normal file
@@ -0,0 +1,8 @@
|
||||
description = "Umbrella : Items"
|
||||
|
||||
dependencies{
|
||||
implementation(project(":core"))
|
||||
implementation("de.srsoftware:configuration.api:1.0.2")
|
||||
implementation("de.srsoftware:tools.jdbc:1.3.2")
|
||||
implementation("de.srsoftware:tools.util:2.0.3")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
public class Constants {
|
||||
private Constants(){}
|
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.items.database";
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
public class Item {
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.api.UserService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.items.Constants.CONFIG_DATABASE;
|
||||
|
||||
public class ItemApi extends BaseHandler {
|
||||
|
||||
private final ItemDb itemDb;
|
||||
private final UserService users;
|
||||
|
||||
public ItemApi(Configuration config, UserService userService) throws UmbrellaException {
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
itemDb = new SqliteDb(connect(dbFile));
|
||||
users = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listItems(ex,user);
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean listItems(HttpExchange ex, Optional<UmbrellaUser> user) throws IOException {
|
||||
var items = itemDb.list();
|
||||
return notImplemented(ex,"listItems",this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface ItemDb {
|
||||
Collection<Item> list();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class SqliteDb implements ItemDb{
|
||||
private final Connection db;
|
||||
|
||||
public SqliteDb(Connection connection) {
|
||||
db = connection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user