From c5c07f023e1e43a82ed471e990fcd85d13455c28 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Fri, 09 Apr 2021 14:48:31 +0000
Subject: [PATCH] JavaScript aufgeraeumt

---
 src/de/uhilger/mediaz/api/ListHandler.java |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/de/uhilger/mediaz/api/ListHandler.java b/src/de/uhilger/mediaz/api/ListHandler.java
index d851478..b947f5e 100644
--- a/src/de/uhilger/mediaz/api/ListHandler.java
+++ b/src/de/uhilger/mediaz/api/ListHandler.java
@@ -17,8 +17,16 @@
  */
 package de.uhilger.mediaz.api;
 
+import com.google.gson.Gson;
 import com.sun.net.httpserver.HttpExchange;
+import de.uhilger.mediaz.App;
+import de.uhilger.mediaz.Server;
+import de.uhilger.mediaz.entity.Abspielliste;
+import de.uhilger.mediaz.entity.Entity;
+import de.uhilger.mediaz.entity.Titel;
+import de.uhilger.mediaz.store.FileStorage;
 import java.io.IOException;
+import java.util.logging.Logger;
 
 /**
  * Der ListHandler bearbeitet HTTP-Anfragen zu Abspiellisten
@@ -32,15 +40,54 @@
  * @version 1, 8.4.2021
  */
 public class ListHandler extends AbstractHandler {
+  
+  private static final Logger logger = Logger.getLogger(ListHandler.class.getName());
+
 
   @Override
   protected String get(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(App.getRs(Server.RB_SLASH));
+    String plname = elems[elems.length - 1];
+    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+    String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
+    return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
   }
 
   @Override
   protected String put(HttpExchange e) throws IOException {
-    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    String path = e.getRequestURI().toString();
+    String[] elems = path.split(App.getRs(Server.RB_SLASH));
+    String response = "ListHandler.put: ungueltiger URL";
+    switch(elems.length) {
+      case 5: // ohne nr am Ende
+        response = addTitel(e, elems[4]);
+        break;
+        
+      case 6:
+        response = "Einfuegen noch nicht fertig.";
+        break;
+    }
+    return response;
+  }
+  
+  private String addTitel(HttpExchange e, String plname) throws IOException {
+    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;
+      String titelJson = bodyLesen(e);
+      Gson gson = new Gson();
+      Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
+      if(o instanceof Titel) {
+        Titel titel = (Titel) o;
+        aliste.addTitel(titel);
+        fs.write(aliste, true);
+        response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt.";
+      }
+    }
+    return response;
   }
 
   @Override

--
Gitblit v1.9.3