working on project creation
This commit is contained in:
@@ -7,4 +7,6 @@ import java.util.Collection;
|
||||
|
||||
public interface ProjectDb {
|
||||
Collection<Project> list(long companyId, boolean includeClosed) throws UmbrellaException;
|
||||
|
||||
Project save(Project prj);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ 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.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.project.Constants.CONFIG_DATABASE;
|
||||
import static java.lang.Boolean.TRUE;
|
||||
import static java.util.Comparator.comparing;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -21,6 +21,8 @@ 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 org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@@ -54,6 +56,7 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listItems(ex,user.get());
|
||||
case null -> postProject(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
@@ -61,10 +64,6 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Project> listProjects(long companyId, boolean includeClosed) throws UmbrellaException {
|
||||
return projectDb.list(companyId, includeClosed).stream().sorted(comparing(Project::name)).toList();
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -77,4 +76,26 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
.map(HashMap::new);
|
||||
return sendContent(ex,items);
|
||||
}
|
||||
|
||||
public Collection<Project> listProjects(long companyId, boolean includeClosed) throws UmbrellaException {
|
||||
return projectDb.list(companyId, includeClosed).stream().sorted(comparing(Project::name)).toList();
|
||||
}
|
||||
|
||||
private boolean postProject(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(NAME) && json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
|
||||
var description = json.has(DESCRIPTION) && json.get(DESCRIPTION) instanceof String d ? d : null;
|
||||
Long companyId = null;
|
||||
if (json.has(COMPANY_ID) && json.get(COMPANY_ID) instanceof Number number){
|
||||
if (!companies.membership(number.longValue(), user.id())) throw forbidden("You are not a member of company {0}!",number);
|
||||
companyId = number.longValue();
|
||||
}
|
||||
var showClosed = false;
|
||||
if (json.has(SETTINGS) && json.get(SETTINGS) instanceof JSONObject settingsJson){
|
||||
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE;
|
||||
}
|
||||
var prj = new Project(0,name,description,Project.Status.Open,companyId,showClosed);
|
||||
projectDb.save(prj);
|
||||
return notFound(ex);
|
||||
}
|
||||
}
|
||||
@@ -138,4 +138,12 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to load items from database");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project save(Project prj) {
|
||||
try {
|
||||
insertInto(TABLE_PROJECTS);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user