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:
2025-07-13 23:45:18 +02:00
parent ea888c2be4
commit bac04ac047
39 changed files with 642 additions and 61 deletions

5
project/build.gradle.kts Normal file
View File

@@ -0,0 +1,5 @@
description = "Umbrella : Projects"
dependencies{
implementation(project(":core"))
}

View File

@@ -0,0 +1,10 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.project;
public class Constants {
private Constants(){}
public static final String CONFIG_DATABASE = "umbrella.modules.project.database";
public static final String TABLE_PROJECTS = "projects";
}

View File

@@ -0,0 +1,10 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.project;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Project;
import java.util.Collection;
public interface ProjectDb {
Collection<Project> list(long companyId, boolean includeClosed) throws UmbrellaException;
}

View File

@@ -0,0 +1,79 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.project;
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.project.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.ProjectService;
import de.srsoftware.umbrella.core.api.UserService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Project;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Optional;
public class ProjectModule extends BaseHandler implements ProjectService {
private final ProjectDb projectDb;
private final CompanyService companies;
private final UserService users;
public ProjectModule(Configuration config, CompanyService companyService) throws UmbrellaException {
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
projectDb = new SqliteDb(connect(dbFile));
companies = companyService;
users = companies.userService();
}
@Override
public CompanyService companyService() {
return companies;
}
@Override
public boolean doPost(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.get());
default -> super.doGet(path,ex);
};
} catch (UmbrellaException e){
return send(ex,e);
}
}
public Collection<Project> listProjects(long companyId, boolean includeClosed) throws UmbrellaException {
return projectDb.list(companyId, includeClosed);
}
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 = listProjects(companyId,false)
.stream()
.map(Project::toMap)
.map(HashMap::new);
return sendContent(ex,items);
}
}

View File

@@ -0,0 +1,41 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.project;
import static de.srsoftware.tools.jdbc.Condition.equal;
import static de.srsoftware.tools.jdbc.Condition.lessThan;
import static de.srsoftware.tools.jdbc.Query.select;
import static de.srsoftware.umbrella.core.Constants.COMPANY_ID;
import static de.srsoftware.umbrella.core.Constants.STATUS;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static de.srsoftware.umbrella.project.Constants.TABLE_PROJECTS;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Project;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
public class SqliteDb implements ProjectDb {
private final Connection db;
public SqliteDb(Connection connection) {
db = connection;
}
@Override
public Collection<Project> list(long companyId, boolean includeClosed) throws UmbrellaException {
try {
var items = new HashSet<Project>();
var query = select("*").from(TABLE_PROJECTS).where(COMPANY_ID, equal(companyId));
if (!includeClosed) query = query.where(STATUS,lessThan(Project.Status.Complete.code()));
var rs = query.exec(db);
while (rs.next()) items.add(Project.of(rs));
rs.close();
return items;
} catch (SQLException e) {
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to load items from database");
}
}
}