package com.mason.server.controller.json;

import java.security.Principal;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mason.server.dao.jdo.UserDAO;
import com.mason.server.exception.ResourceNotFoundException;
import com.mason.server.model.DbUser;

@Controller
@RequestMapping("/json/")
public class MainControllerJson {

	private static UserDAO userDAO = new UserDAO();

	@RequestMapping(value = "/access/loggedIn", method = RequestMethod.GET)
	public @ResponseBody int getLoggedinUser(Principal principal) {
		if (principal == null ) {
			return 0;
		}
		return 1;
	}

	@RequestMapping(value = "/allUsers", method = RequestMethod.GET)
	public @ResponseBody List<String> getAllUsers(Principal principal) {
		return userDAO.getAllUsernames();
	}


	public static DbUser getUserByUsername(String username) {
		DbUser user = userDAO.getByUsername(username);
		if (user == null) {
			throw new ResourceNotFoundException("User: " + username);
		}
		return user;
	}

}
