Persoenliche Mediazentrale
undisclosed
2023-01-09 1bf4f6d3cdcf1445c2a40959f286a73485eb31c8
src/de/uhilger/tango/api/ListHandler.java
@@ -19,13 +19,13 @@
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import de.uhilger.tango.App;
import de.uhilger.tango.Server;
import de.uhilger.tango.entity.Abspielliste;
import de.uhilger.tango.entity.Entity;
import de.uhilger.tango.entity.Titel;
import de.uhilger.tango.store.FileStorage;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
/**
@@ -34,13 +34,12 @@
 * GET /mz/api/alist/[pl-name]          die Titel-Objekte der Liste [pl-name] liefern
 * 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
 * Neu: PUT /mz/api/alist/[pl-name]/[nr]/up  den Titel an der Position nr der Liste [pl-name] eins nach oben
 * Neu: PUT /mz/api/alist/[pl-name]/[nr]/dn  den Titel an der Position nr der Liste [pl-name] eins nach unten
 * PUT /mz/api/alist/[pl-name]/[nrVon]/[nrNach]   den Titel von seiner aktuellen Position an eine
 *                                                 andere Position der Liste [pl-name] verschieben
 * 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  
 * 
 * TODO (2.1.2023):
 * - Titel eins nach oben/unten
 * - Liste ab Titel spielen
 * - Ganzes Album der Liste hinzufuegen
 *
@@ -80,8 +79,75 @@
        break;
        
      case 6:
        response = "Einfuegen noch nicht fertig.";
        response = insertTitel(e, elems[4], Integer.parseInt(elems[5]));
        break;
      case 7:
        response = moveTitel(e, elems[4], Integer.parseInt(elems[5]), Integer.parseInt(elems[6]));
        break;
    }
    return response;
  }
  /**
   * Den Titel im Body von seiner aktuellen Position an die angegebene
   * Position setzen. Der Titel an der angegebenen Position rueckt nach
   * unten.
   *
   * Annahme: Die Abspielliste enthaelt keine Titel mehrfach.
   *
   * @param e
   * @param plname
   * @param zielPos
   * @return
   * @throws IOException
   */
  private String moveTitel(HttpExchange e, String plname, int pos, int zielPos) throws IOException {
    FileStorage fs = new FileStorage(conf);
    Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
    String response = "Titel konnte nicht verschoben werden.";
    if(entity instanceof Abspielliste) {
      if(pos < zielPos) {
        --zielPos;
      }
      Abspielliste aliste = (Abspielliste) entity;
      List<Titel> liste = aliste.getTitel();
      Titel t = liste.get(pos);
      liste.remove(pos);
      liste.add(zielPos, t);
      fs.write(aliste, true);
      response = "Titel " + t.getName() + " der Liste " + aliste.getName() +
              " an Position " + zielPos + " verschoben.";
    }
    return response;
  }
  /**
   * Den Titel im Body an Position anPos einfuegen. Der Titel an anPos
   * rueckt nach unten.
   *
   * @param e
   * @param plname
   * @param anPos
   * @return
   * @throws IOException
   */
  private String insertTitel(HttpExchange e, String plname, int anPos) throws IOException {
    FileStorage fs = new FileStorage(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.putTitel(titel, anPos);
        fs.write(aliste, true);
        response = "Titel " + titel.getName() + " der Liste " + aliste.getName() +
                " an Position " + anPos + " hinzugefuegt.";
      }
    }
    return response;
  }
@@ -103,11 +169,6 @@
      }
    }
    return response;
  }
  @Override
  protected String post(HttpExchange e) {
    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