From 2af7d6512096c84b028011503246ce86deec540f Mon Sep 17 00:00:00 2001 From: ulrich Date: Wed, 21 Apr 2021 12:59:07 +0000 Subject: [PATCH] Vorlagencache aktiviert --- src/de/uhilger/mediaz/api/ListHandler.java | 41 ++++++++++++++++++++++++++--------------- 1 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/de/uhilger/mediaz/api/ListHandler.java b/src/de/uhilger/mediaz/api/ListHandler.java index 84a9056..be42315 100644 --- a/src/de/uhilger/mediaz/api/ListHandler.java +++ b/src/de/uhilger/mediaz/api/ListHandler.java @@ -35,6 +35,7 @@ * PUT /mz/api/alist/[pl-name] den Titel im Body anfuegen an die Liste [pl-name] * PUT /mz/api/alist/[pl-name]/[nr] an der Position nr der Liste [pl-name] den Titel im Body einfuegen * DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen + * DELETE /mz/api/alist/[pl-name]/alle alle Titel aus der Liste [pl-name] entfernen * * @author Ulrich Hilger * @version 1, 8.4.2021 @@ -42,31 +43,27 @@ public class ListHandler extends AbstractHandler { private static final Logger logger = Logger.getLogger(ListHandler.class.getName()); + + public static final String ALLE_TITEL = "alle"; @Override protected String get(HttpExchange e) { String path = e.getRequestURI().toString(); - String[] elems = path.split(App.getRs(Server.RB_SLASH)); + String[] elems = path.split(Server.SLASH); String plname = elems[elems.length - 1]; - logger.finer("GET plname: " + plname); FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname); - logger.finer("PL json: " + json); - return json; + return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE); } @Override protected String put(HttpExchange e) throws IOException { String path = e.getRequestURI().toString(); - String[] elems = path.split(App.getRs(Server.RB_SLASH)); + String[] elems = path.split(Server.SLASH); String response = "ListHandler.put: ungueltiger URL"; - logger.finer("elems.length: " + elems.length); - for(String elem : elems) { - logger.finer("elem: " + elem); - } switch(elems.length) { - case 5: + case 5: // ohne nr am Ende response = addTitel(e, elems[4]); break; @@ -78,16 +75,12 @@ } private String addTitel(HttpExchange e, String plname) throws IOException { - //String plname = elems[elems.length - 1]; - logger.finer("plname: " + plname); FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname); String response = "Titel konnte nicht hinzugefuegt werden."; if(entity instanceof Abspielliste) { Abspielliste aliste = (Abspielliste) entity; - logger.finer("aliste: " + aliste.getName()); String titelJson = bodyLesen(e); - logger.finer("titelJson: " + titelJson); Gson gson = new Gson(); Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType()); if(o instanceof Titel) { @@ -105,9 +98,27 @@ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } + // DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen @Override protected boolean delete(HttpExchange e) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + String path = e.getRequestURI().toString(); + String[] elems = path.split(Server.SLASH); + String listName = elems[elems.length - 2]; + FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); + Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName); + if(entity instanceof Abspielliste) { + Abspielliste liste = (Abspielliste) entity; + String titelStr = elems[elems.length-1]; + if(titelStr.equalsIgnoreCase(ALLE_TITEL)) { + liste.getTitel().clear(); + } else { + liste.getTitel().remove(Integer.parseInt(elems[elems.length-1])); + } + fs.write(liste, true); + return true; + } else { + return false; + } } } -- Gitblit v1.9.3