public String handlePost()

in Elastiflix/java-favorite-elastic-manual/src/main/java/com/movieapi/ApiServlet.java [70:94]


    public String handlePost(@RequestParam String user_id, @RequestBody String requestBody) throws InterruptedException, Exception {
        handleDelay();
        logger.info("Adding or removing favorites");

        JSONObject json = new JSONObject(requestBody);
        String movieID = Integer.toString(json.getInt("id"));
        
        logger.info("Adding or removing favorites for user " +  user_id + ", movieID " + movieID);
        Jedis jedis = r.getResource();

        try  {
            Long redisResponse = jedis.srem(user_id, movieID);
            if (redisResponse == 0) {
                jedis.sadd(user_id, movieID);
            }
        } catch (Exception e) {
            logger.error("Error adding or removing favorites for user " + user_id + ", movieID " + movieID);
        } finally {
            jedis.close();
        }

        String favorites = getUserFavorites(user_id);
        handleCanary();
        return favorites;
    }