implemented function to return estimated times of company.
Therefore task and project module had to be created and partially implemented
This commit is contained in:
@@ -2,7 +2,4 @@ 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")
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
public class Constants {
|
||||
private Constants(){}
|
||||
|
||||
public static final String CODE = "code";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.items.database";
|
||||
public static final String TABLE_ITEMS = "items";
|
||||
public static final String TAX = "tax";
|
||||
public static final String UNIT = "unit";
|
||||
public static final String UNIT_PRICE = "unit_price";
|
||||
}
|
||||
|
||||
@@ -1,4 +1,38 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
public class Item {
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Util.markdown;
|
||||
import static de.srsoftware.umbrella.items.Constants.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
public record Item(long id, long companyId, String code, String name, String description, String unit, long unitPrice, long tax) implements Mappable {
|
||||
public static Item of(ResultSet rs) throws SQLException {
|
||||
var id = rs.getLong(ID);
|
||||
var companyId = rs.getLong(COMPANY_ID);
|
||||
var code = rs.getString(CODE);
|
||||
var name = rs.getString(NAME);
|
||||
var desc = rs.getString(DESCRIPTION);
|
||||
var unit = rs.getString(UNIT);
|
||||
var unitPrice = rs.getLong(UNIT_PRICE);
|
||||
var tax = rs.getInt(TAX);
|
||||
|
||||
return new Item(id,companyId,code,name,desc,unit,unitPrice,tax);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(
|
||||
ID,id,
|
||||
COMPANY_ID,companyId,
|
||||
CODE,code,NAME,name,
|
||||
DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)),
|
||||
UNIT,unit,
|
||||
UNIT_PRICE,unitPrice,
|
||||
TAX,tax);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,42 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.forbidden;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.items.Constants.CONFIG_DATABASE;
|
||||
|
||||
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.CompanyService;
|
||||
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.HashMap;
|
||||
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 CompanyService companies;
|
||||
private final UserService users;
|
||||
|
||||
public ItemApi(Configuration config, UserService userService) throws UmbrellaException {
|
||||
public ItemApi(Configuration config, CompanyService companyService) throws UmbrellaException {
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
itemDb = new SqliteDb(connect(dbFile));
|
||||
users = userService;
|
||||
companies = companyService;
|
||||
users = companies.userService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
@@ -38,7 +44,7 @@ public class ItemApi extends BaseHandler {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listItems(ex,user);
|
||||
case LIST -> listItems(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
@@ -46,8 +52,17 @@ public class ItemApi extends BaseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean listItems(HttpExchange ex, Optional<UmbrellaUser> user) throws IOException {
|
||||
var items = itemDb.list();
|
||||
return notImplemented(ex,"listItems",this);
|
||||
private boolean listItems(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(COMPANY_ID) && json.get(COMPANY_ID) instanceof Number cid)) throw missingFieldException(COMPANY_ID);
|
||||
var companyId = cid.longValue();
|
||||
var company = companies.get(companyId);
|
||||
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
var items = itemDb.list(companyId)
|
||||
.stream()
|
||||
.map(Item::toMap)
|
||||
.map(HashMap::new)
|
||||
.peek(map -> map.put(FIELD_CURRENCY,company.currency()));
|
||||
return sendContent(ex,items);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface ItemDb {
|
||||
Collection<Item> list();
|
||||
Collection<Item> list(long companyId) throws UmbrellaException;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,36 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.items;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.umbrella.core.Constants.COMPANY_ID;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
|
||||
import static de.srsoftware.umbrella.items.Constants.TABLE_ITEMS;
|
||||
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class SqliteDb implements ItemDb{
|
||||
|
||||
private final Connection db;
|
||||
|
||||
public SqliteDb(Connection connection) {
|
||||
db = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Item> list(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
var items = new HashSet<Item>();
|
||||
var rs = select("*").from(TABLE_ITEMS).where(COMPANY_ID, equal(companyId)).exec(db);
|
||||
while (rs.next()) items.add(Item.of(rs));
|
||||
rs.close();
|
||||
return items;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to load items from database");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user