Esempio n. 1
0
  /**
   * saves a playlist to the database for the current user. outputs a single integer which is the
   * playlist ID if all goes well, otherwise you'll get a description of the problem.
   *
   * @throws IOException
   */
  protected void savePlaylist() throws IOException, SQLException, BadRequestException {

    final Request req = getRequest();
    final User user = getUser();
    final Locale locale = getLocale();
    final String name = req.getUrlParam(2).trim();
    final String[] args = req.getPlayParams(2);

    String result = locale.getString("www.json.error.unknown");

    // make sure data is ok first
    if (name.equals("")) result = locale.getString("www.json.error.noName");
    else if (args.length == 0) result = locale.getString("www.json.error.noArguments");
    else if (user == null) result = locale.getString("www.json.error.notLoggedIn");
    else {

      final Database db = getDatabase();
      final List<Track> vTracks = Track.getTracksFromPlayArgs(db, args);
      final Track[] tracks = new Track[vTracks.size()];

      for (int i = 0; i < vTracks.size(); i++) tracks[i] = vTracks.get(i);

      result = Integer.toString(cm.savePlaylist(name, tracks, user));
    }

    final TString tpl = new TString();
    tpl.setResult(result);
    getResponse().showJson(tpl.makeRenderer());
  }
Esempio n. 2
0
  /**
   * tries to delete a users playlist. needs to check things like did they create it, etc... if all
   * goes ok then sends back the ID so that the javascript handler can do whatever...
   *
   * @throws BadRequestException
   * @throws SQLException
   * @throws IOException
   */
  protected void deletePlaylist() throws BadRequestException, SQLException, IOException {

    final Request req = getRequest();
    final User user = getUser();
    final Locale locale = getLocale();

    if (user == null)
      throw new BadRequestException(locale.getString("www.json.error.notLoggedIn"), 403);

    final Database db = getDatabase();
    final int id = Integer.parseInt(req.getUrlParam(2));
    final String sql =
        " select 1 " + " from playlists p " + " where p.id = ? " + " and p.user_id = ? ";

    ResultSet rs = null;
    PreparedStatement st = null;

    try {

      // check user owns playlist before deleting it
      st = db.prepare(sql);
      st.setInt(1, id);
      st.setInt(2, user.getId());
      rs = st.executeQuery();
      if (!rs.next()) throw new BadRequestException("You don't own that playlist", 403);

      cm.removePlaylist(id);

      // done, send success response
      final TString tpl = new TString();
      tpl.setResult(Integer.toString(id));
      getResponse().showJson(tpl.makeRenderer());

    } finally {
      Utils.close(rs);
      Utils.close(st);
    }
  }