working in list management
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.data.MailingList;
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@@ -10,15 +11,14 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
|
||||
public class Rest extends HttpServlet {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
|
||||
private static final String LIST_LIST = "list/list";
|
||||
private static final String USER_LIST = "user/list";
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
@@ -31,13 +31,18 @@ public class Rest extends HttpServlet {
|
||||
JSONObject json = new JSONObject();
|
||||
if (o instanceof User user){
|
||||
var path = req.getPathInfo();
|
||||
json.put(USER,safeMapUser(user));
|
||||
json.put(USER,user.safeMap());
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
switch (path) {
|
||||
case USER_LIST:
|
||||
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(Rest::safeMapUser).toList());
|
||||
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(User::safeMap).toList());
|
||||
break;
|
||||
case LIST_LIST:
|
||||
json.put("lists", MailingList.listsOf(user).stream().map(MailingList::safeMap).toList());
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
|
||||
}
|
||||
} else {
|
||||
json.put(ERROR,"Not logged in!");
|
||||
@@ -49,8 +54,4 @@ public class Rest extends HttpServlet {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String,String> safeMapUser(User user){
|
||||
return Map.of(NAME,user.name(),EMAIL,user.email(),PASSWORD,user.hashedPassword() == null ? "no" : "yes");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user