Пример #1
0
  public RequestResponse requestSong(String spotifyId, String token, int locationId, String code)
      throws SpotifyApiException, UpdateQRCodeException {

    if (!data.hasSong(spotifyId)) { // Fetch song info from spotify if neccessary
      SpotifyLookupContainer song = requestSpotifySong(spotifyId);
      logger.debug(
          song.getTrack().getName()
              + " - Fetched from spotify, length: "
              + song.getTrack().getLength());
      long length = (long) song.getTrack().getLength() * 1000;
      data.saveSong(
          spotifyId, song.getTrack().getName(), song.getTrack().concatArtistNames(), length);
    }

    int qrValidationCode = qrService.verify(code, locationId);
    if (!data.getSession(locationId).isOpen()) { // session is not open
      return new RequestResponse(
          "Stemming er ikke mulig. Sesjonen er ikke aktiv", false, qrValidationCode);
    } else if (qrValidationCode == QR_IS_NOT_VALID) { // Code is not valid
      return new RequestResponse("QR-koden er ikke gyldig", false, qrValidationCode);
    } else if (qrValidationCode == QR_HAS_UNLIMITED_USES) { // Unlimited votes so check token
      int userId = verifyToken(token); // throws exception if code is not valid or is null

      if (data.userCanRequestSong(spotifyId, locationId, userId)) {
        data.requestSongOneActiveVote(spotifyId, locationId, userId);
      } else {
        logger.debug(
            "User "
                + userId
                + " could not vote for song "
                + spotifyId
                + " at location "
                + locationId);
        return new RequestResponse(
            "Denne brukeren har allerede stemt opp denne sangen", false, qrValidationCode);
      }
    } else if (qrValidationCode == QR_HAS_LIMITED_USES) { // Limited votes, so token not neccessary
      int userId = authService.verify(token);
      if (userId == -1) { // Token null or not valid
        userId = -1337; // The fake spotify user
      }
      boolean success =
          data.requestSongManyActiveVotes(
              spotifyId,
              locationId,
              userId); // No need to update qrcodes and such.//Jo? Have to decrement the uses.
      if (success) {
        if (!qrService.codeIsUsed(code)) {
          logger.debug("Could not update QRcode");
        }
      } else {
        return new RequestResponse("Kunne ikke oppdatere databasen", false, qrValidationCode);
      }
    }

    return new RequestResponse("Stemmen ble registrert", true, qrValidationCode);
  }
Пример #2
0
  public List<Track> searchForTrack(String query, String orderBy, int page, int locationId)
      throws SpotifyApiException {
    SpotifyTrackSearchContainer tracks = searchSpotify(query, page);
    List<String> spotifyIds = new ArrayList<String>();

    for (SpotifyTrack track : tracks.getTracks()) { // Save tracks to db
      long length = (long) track.getLength() * 1000;
      data.saveSong(track.getHref(), track.getName(), track.concatArtistNames(), length);
      // logger.debug("Found - "+track.getName()+" id: "+track.getHref());
      spotifyIds.add(track.getHref());
    }
    if (spotifyIds.size() > 0) {
      return data.getSongs(spotifyIds, locationId, orderBy);
    } else { // to prevent sql-error when there are no ids.
      return new ArrayList<Track>();
    }
  }