package com.softserve.academy.food.controllers.admin;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.softserve.academy.food.controllers.datamodel.ModelFindquery;
import com.softserve.academy.food.model.mUserCredentials;
import com.softserve.academy.food.model.mUserInfo;
import com.softserve.academy.food.services.iUserService;
import com.softserve.academy.food.services.admin.iUserManagerService;
import com.softserve.academy.food.services.supplement.Convector;
@Controller
@RequestMapping( value="/managerusers" )
public class UserManager
{
@Autowired
private iUserManagerService aService;
@Autowired
private iUserService uService;
private ModelAndView uModel;
private ArrayList<mUserCredentials> findResult = new ArrayList<mUserCredentials>();
private String id="0";
@RequestMapping( method = RequestMethod.GET)
public ModelAndView info()
{
uModel = new ModelAndView("admin");
return uModel;
}
@RequestMapping( params={"action=userlist"}, method = RequestMethod.GET)
public ModelAndView userList()
{
uModel = new ModelAndView("admin");
uModel.addObject( "listUser", aService.getUsers() );
return uModel;
}
@RequestMapping( params={"action=adduser"}, method = RequestMethod.GET)
public ModelAndView addUser()
{
uModel = new ModelAndView("admin");
uModel.addObject( "user", new mUserCredentials() );
return uModel;
}
@RequestMapping( params={"action=deluser"}, method = RequestMethod.GET)
public ModelAndView viewDelUser()
{
uModel = new ModelAndView("admin");
uModel.addObject( "user", aService.getUser( Convector.StringToInt(id) ) );
return uModel;
}
@RequestMapping( params={"action=useredit"}, method = RequestMethod.GET)
public ModelAndView showUser()
{
uModel = new ModelAndView("admin");
uModel.addObject( "user", aService.getUserInfo( Convector.StringToInt(id) ) );
return uModel;
}
@RequestMapping( params={"action=finduser"}, method = RequestMethod.GET)
public ModelAndView findUser()
{
uModel = new ModelAndView("admin");
uModel.addObject( "findquery", new ModelFindquery() );
uModel.addObject( "listUser", findResult );
return uModel;
}
@RequestMapping( params={"action=useredit"}, method = RequestMethod.POST)
public String userEdit( @ModelAttribute("User") mUserInfo user )
{
uService.updateUser( user );
return "redirect:/managerusers.htm?action=useredit";
}
@RequestMapping( params={"action=finduser"}, method = RequestMethod.POST)
public String findUser( @ModelAttribute("ModelFindquery") ModelFindquery query )
{
if ( query.getType().equals("ID") )
{
findResult.clear();
findResult.add( aService.getUser( Convector.StringToInt(query.getQuery()) ) );
}
else
{
findResult = aService.findUsers( query.getQuery() );
}
return "redirect:/managerusers.htm?action=finduser";
}
@RequestMapping( params={"action=deluser"}, method = RequestMethod.POST)
public String showDel( HttpServletRequest request )
{
if ( request.getParameter("acept")!=null && request.getParameter("acept").equals("YES") )
{
aService.delUser(id);
return "redirect:/managerusers.htm?action=userlist";
}
else
{
id = request.getParameter("id");
}
return "redirect:/managerusers.htm?action=deluser";
}
@RequestMapping( params={"action=userlist"}, method = RequestMethod.POST)
public String delUser( HttpServletRequest request )
{
id = request.getParameter("id");
return "redirect:/managerusers.htm?action=useredit";
}
@RequestMapping( params={"action=adduser"}, method = RequestMethod.POST)
public ModelAndView addUser( @ModelAttribute("User") mUserCredentials user )
{
uModel = new ModelAndView("admin");
uModel.addObject("user", new mUserCredentials());
if ( uService.checkUser(user.getLogin()) )
{
uModel.addObject("checkLogin", "Логин занят");
return uModel;
}
uService.addUser( user );
uModel.addObject("checkLogin", "Пользователь создан");
return uModel;
}
}