working on editing of list properties

This commit is contained in:
2022-04-16 11:45:19 +02:00
parent 3ef331db1a
commit 7b41ece15e
7 changed files with 61 additions and 16 deletions

View File

@@ -22,6 +22,8 @@ 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 LIST_DISABLE = "list/disable";
private static final String LIST_ENABLE = "list/enable";
private static final String LIST_HIDE = "list/hide";
private static final String LIST_SHOW = "list/show";
private static final String USER_LIST = "user/list";
@@ -79,14 +81,25 @@ public class Rest extends HttpServlet {
JSONObject json = new JSONObject();
var path = req.getPathInfo();
path = path == null ? INDEX : path.substring(1);
if (o instanceof User user){
json.put(USER,user.safeMap());
switch (path) {
var listEmail = req.getParameter(LIST);
if (listEmail == null || listEmail.isBlank()) {
json.putAll(Map.of(ERROR,"no list email provided!"));
} else switch (path) {
case LIST_DISABLE:
json.putAll(enableList(listEmail,user,false));
break;
case LIST_ENABLE:
json.putAll(enableList(listEmail,user,true));
break;
case LIST_HIDE:
json.putAll(hideList(req,user,true));
json.putAll(hideList(listEmail,user,true));
break;
case LIST_SHOW:
json.putAll(hideList(req,user,false));
json.putAll(hideList(listEmail,user,false));
break;
default:
json.put(ERROR,t("No handler for path '{}'!",path));
@@ -104,9 +117,22 @@ public class Rest extends HttpServlet {
}
}
private Map<String, String> hideList(HttpServletRequest req, User user, boolean hide) {
var listEmail = req.getParameter(LIST);
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
private Map enableList(String listEmail, User user, boolean enable) {
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
try {
MailingList.enable(listEmail,enable);
return Map.of("success",t("Mailing list '{}' was {}!",listEmail,enable ? "enabled" : "disabled"));
} catch (SQLException e) {
LOG.error("Failed to enable/disable mailing list: ",e);
return Map.of("error",t("Failed to update list '{}'",listEmail));
}
} else {
return Map.of("error",t("You are not allowed to edit '{}'",listEmail));
}
}
private Map<String, String> hideList(String listEmail, User user, boolean hide) {
if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){
try {
MailingList.hide(listEmail,hide);