/*
 * Copyright (c) 2013 Lucas Holt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

package com.justjournal.ctl.api;

import com.justjournal.WebLogin;
import com.justjournal.db.FriendTo;
import com.justjournal.db.FriendsDao;
import com.justjournal.db.UserDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.Map;

/**
 * Manage Friends
 * @author Lucas Holt
 */
@Controller
@RequestMapping("/api/friend")
final public class FriendController {

    // TODO: refactor to return user objects?
    /**
     *
     * @param id username
     * @param session http session
     * @param response http response
     * @return List of usernames as strings
     */
    @RequestMapping("/api/friend/{id}")
    @ResponseBody
    public Collection<String> getById(@PathVariable String id, HttpSession session, HttpServletResponse response) {
        try {
            return UserDao.friends(id);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }
    }


    @RequestMapping(method = RequestMethod.DELETE)
    public
    @ResponseBody
    Map<String, String> delete(@RequestBody FriendTo friend, HttpSession session, HttpServletResponse response) throws Exception {


        if (!WebLogin.isAuthenticated(session)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return java.util.Collections.singletonMap("error", "The login timed out or is invalid.");
        }

        friend.setOwnerId(WebLogin.currentLoginId(session));

        if (FriendsDao.delete(friend)) {
            return java.util.Collections.singletonMap("status", "success");
        }

        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return java.util.Collections.singletonMap("error", "Error deleting friend");
    }
}

@@ -1438,24 +1438,17 @@
 rnal.db.
-FriendTo
+*
 ;%0Aimport
@@ -1452,72 +1452,31 @@
 ort 
-com.justjournal.db.FriendsDao;%0Aimport com.justjournal.db.UserDao
+org.apache.log4j.Logger
 ;%0Aim
@@ -1734,16 +1734,19 @@
 Friends%0A
+ *%0A
  * @auth
@@ -1843,16 +1843,96 @@
 roller %7B
+%0A    private static final Logger log = Logger.getLogger(FriendController.class);
 %0A%0A    //
@@ -1975,16 +1975,17 @@
 ts?%0A
+%0A
     /**%0A
     
@@ -1984,15 +1984,8 @@
 /**%0A
-     *%0A
     
@@ -1997,16 +1997,22 @@
 aram id 
+      
 username
@@ -2034,16 +2034,17 @@
 session 
+ 
 http ses
@@ -2488,16 +2488,16 @@
 n null;%0A
-
         
@@ -2505,16 +2505,1291 @@
 %0A    %7D%0A%0A
+    @RequestMapping(method = RequestMethod.POST, produces = %22application/json%22)%0A    @ResponseBody%0A    public Map%3CString, String%3E post(@RequestParam String friend, HttpSession session, HttpServletResponse response) %7B%0A        if (!WebLogin.isAuthenticated(session)) %7B%0A            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);%0A            return java.util.Collections.singletonMap(%22error%22, %22The login timed out or is invalid.%22);%0A        %7D%0A%0A        try %7B%0A            UserTo friendUser = UserDao.view(friend);%0A%0A            if (friendUser == null)%0A                return java.util.Collections.singletonMap(%22error%22, %22Could not find friend's username%22);%0A%0A            String sqlStatement = %22Insert INTO friends (id, friendid) values('%22 + WebLogin.currentLoginId(session) + %22','%22 + friendUser.getId() + %22');%22;%0A            int rowsAffected = SQLHelper.executeNonQuery(sqlStatement);%0A            if (rowsAffected == 1)%0A                return java.util.Collections.singletonMap(%22status%22, %22success%22);%0A%0A            return java.util.Collections.singletonMap(%22error%22, %22Unable to add friend%22);%0A        %7D catch (Exception e) %7B%0A            log.error(e.getMessage());%0A            return java.util.Collections.singletonMap(%22error%22, %22Could not find friend's username%22);%0A        %7D%0A%0A    %7D%0A%0A
 %0A    @Re

add friends