|
|
|
@ -26,13 +26,13 @@ import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
|
|
public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
|
|
|
|
|
|
|
|
private final ProjectDb projectDb; |
|
|
|
private final ProjectDb projects; |
|
|
|
private final CompanyService companies; |
|
|
|
private final CompanyService companies; |
|
|
|
private final UserService users; |
|
|
|
private final UserService users; |
|
|
|
|
|
|
|
|
|
|
|
public ProjectModule(Configuration config, CompanyService companyService) throws UmbrellaException { |
|
|
|
public ProjectModule(Configuration config, CompanyService companyService) throws UmbrellaException { |
|
|
|
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
|
|
|
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
|
|
|
projectDb = new SqliteDb(connect(dbFile)); |
|
|
|
projects = new SqliteDb(connect(dbFile)); |
|
|
|
companies = companyService; |
|
|
|
companies = companyService; |
|
|
|
users = companies.userService(); |
|
|
|
users = companies.userService(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -42,6 +42,24 @@ public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
return companies; |
|
|
|
return companies; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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 -> listUserProjects(ex,user.get()); |
|
|
|
|
|
|
|
case null -> postProject(ex,user.get()); |
|
|
|
|
|
|
|
default -> super.doGet(path,ex); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} catch (UmbrellaException e){ |
|
|
|
|
|
|
|
return send(ex,e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean doPost(Path path, HttpExchange ex) throws IOException { |
|
|
|
public boolean doPost(Path path, HttpExchange ex) throws IOException { |
|
|
|
addCors(ex); |
|
|
|
addCors(ex); |
|
|
|
@ -51,7 +69,7 @@ public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
if (user.isEmpty()) return unauthorized(ex); |
|
|
|
var head = path.pop(); |
|
|
|
var head = path.pop(); |
|
|
|
return switch (head) { |
|
|
|
return switch (head) { |
|
|
|
case LIST -> listProjects(ex,user.get()); |
|
|
|
case LIST -> listCompanyProjects(ex,user.get()); |
|
|
|
case null -> postProject(ex,user.get()); |
|
|
|
case null -> postProject(ex,user.get()); |
|
|
|
default -> super.doGet(path,ex); |
|
|
|
default -> super.doGet(path,ex); |
|
|
|
}; |
|
|
|
}; |
|
|
|
@ -60,24 +78,35 @@ public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean listProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
public Collection<Project> listCompanyProjects(long companyId, boolean includeClosed) throws UmbrellaException { |
|
|
|
|
|
|
|
return projects.ofCompany(companyId, includeClosed, users).values().stream().sorted(comparing(Project::name)).toList(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean listCompanyProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!(json.has(COMPANY_ID) && json.get(COMPANY_ID) instanceof Number cid)) throw missingFieldException(COMPANY_ID); |
|
|
|
if (!(json.has(COMPANY_ID) && json.get(COMPANY_ID) instanceof Number cid)) throw missingFieldException(COMPANY_ID); |
|
|
|
var companyId = cid.longValue(); |
|
|
|
var companyId = cid.longValue(); |
|
|
|
var company = companies.get(companyId); |
|
|
|
var company = companies.get(companyId); |
|
|
|
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
var projects = listProjectsOfCompany(companyId,false) |
|
|
|
var projects = listCompanyProjects(companyId,false) |
|
|
|
.stream() |
|
|
|
.stream() |
|
|
|
.map(Project::toMap) |
|
|
|
.map(Project::toMap) |
|
|
|
.map(HashMap::new); |
|
|
|
.map(HashMap::new); |
|
|
|
return sendContent(ex,projects); |
|
|
|
return sendContent(ex,projects); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Collection<Project> listProjectsOfCompany(long companyId, boolean includeClosed) throws UmbrellaException { |
|
|
|
@Override |
|
|
|
|
|
|
|
public Map<Long, Project> listUserProjects(long userId, boolean includeClosed) throws UmbrellaException { |
|
|
|
|
|
|
|
return projects.ofUser(userId, includeClosed, users); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return projectDb.list(companyId, includeClosed, users).values().stream().sorted(comparing(Project::name)).toList(); |
|
|
|
private boolean listUserProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
|
|
|
|
var projects = new HashMap<Long,Map<String,Object>>(); |
|
|
|
|
|
|
|
for (var entry : listUserProjects(user.id(),false).entrySet()) projects.put(entry.getKey(),entry.getValue().toMap()); |
|
|
|
|
|
|
|
return sendContent(ex,projects); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean postProject(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
private boolean postProject(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException { |
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
if (!(json.has(NAME) && json.get(NAME) instanceof String name)) throw missingFieldException(NAME); |
|
|
|
if (!(json.has(NAME) && json.get(NAME) instanceof String name)) throw missingFieldException(NAME); |
|
|
|
@ -92,7 +121,7 @@ public class ProjectModule extends BaseHandler implements ProjectService { |
|
|
|
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE; |
|
|
|
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE; |
|
|
|
} |
|
|
|
} |
|
|
|
var prj = new Project(0,name,description,Project.Status.Open,companyId,showClosed, List.of(new Member(user, OWNER))); |
|
|
|
var prj = new Project(0,name,description,Project.Status.Open,companyId,showClosed, List.of(new Member(user, OWNER))); |
|
|
|
prj = projectDb.save(prj); |
|
|
|
prj = projects.save(prj); |
|
|
|
return sendContent(ex,prj); |
|
|
|
return sendContent(ex,prj); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |