Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.02 KB | None | 0 0
  1. package com.controllers;
  2.  
  3. import com.DAO.newDAO.CharacterRepository;
  4. import com.DAO.newDAO.SpellsRepository;
  5. import com.fasterxml.jackson.core.JsonProcessingException;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.models.Kostyly;
  8. import com.models.entity.CharactersEntity;
  9. import com.models.entity.SpellsEntity;
  10. import com.models.entity.UsersEntity;
  11. import com.services.UserService;
  12. //import com.sun.org.apache.xpath.internal.operations.String;
  13. import javax.validation.constraints.NotNull;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.ui.Model;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.ModelAttribute;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20.  
  21. import javax.servlet.http.Cookie;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.IOException;
  24. import java.io.UnsupportedEncodingException;
  25. import java.net.URLEncoder;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28.  
  29. @Controller
  30. public class RestController {
  31.  
  32.     private UsersEntity user;
  33.     private Kostyly kostyly;
  34.  
  35.     private final UserService userService;
  36.  
  37.     @Autowired
  38.     public CharacterRepository characterRepository;
  39.  
  40.     @Autowired
  41.     public SpellsRepository spellsRepository;
  42.  
  43.     @Autowired
  44.     public RestController(UserService userService) {
  45.         this.userService = userService;
  46.     }
  47.  
  48.     @GetMapping("/")
  49.     public String redirectToGreeting() {
  50.         return "redirect:/greeting";
  51.     }
  52.  
  53.     @GetMapping("/greeting")
  54.     public String greetingForm(Model model) {
  55.         model.addAttribute("reg", new UsersEntity());
  56.         return "index";
  57.     }
  58.  
  59.     @GetMapping("/main_page")
  60.     public String mainPage() {
  61.         return "user_main_page";
  62.     }
  63.  
  64.     @PostMapping("/greeting")
  65.     public String greetingSubmit(@ModelAttribute UsersEntity userR) {
  66.         System.out.println(userR);
  67.  
  68.         UsersEntity userByLoginAndPassword =
  69.                 userService.getUserByLoginAndPassword(
  70.                         userR.getLogin(),
  71.                         userR.getPassword()
  72.                 );
  73.  
  74.         System.out.println(userByLoginAndPassword);
  75.         if (userByLoginAndPassword == null) {
  76.             return "redirect:/greeting";
  77.         }
  78.         user = userByLoginAndPassword;
  79.         return "redirect:/character_menu";
  80.     }
  81.  
  82.     @GetMapping("/reg")
  83.     public String registrationForm(Model model) {
  84.         model.addAttribute("userR", new UsersEntity());
  85.         return "registration";
  86.     }
  87.  
  88.     @PostMapping("/reg")
  89.     public String registrationSubmit(@ModelAttribute UsersEntity userR) {
  90.         if (userService.checkLogin(userR.getLogin())) {
  91.             userService.saveUser(userR);
  92.             return "redirect:/greeting";
  93.         } else {
  94.             return "redirect:/reg";
  95.         }
  96.     }
  97.  
  98.     @GetMapping("/character_menu")
  99.     public String characterMenuForm(HttpServletResponse httpServletResponse, Model model) throws IOException {
  100.         if (user != null) {
  101.             model.addAttribute("characterId", new Kostyly());
  102.             ObjectMapper objectMapper = new ObjectMapper();
  103.             String jsonStr = objectMapper.writeValueAsString(user.getCharacters());
  104.             httpServletResponse.addCookie(new Cookie("characters", URLEncoder.encode(jsonStr, "UTF-8")));
  105.             return "character_menu";
  106.         } else
  107.             return "redirect:/greeting";
  108.     }
  109.  
  110.     @PostMapping("/character_menu")
  111.     public String characterMenuSubmit(@ModelAttribute Kostyly characterId) {
  112.         System.out.println(characterId.getCharacterId());
  113.         kostyly = characterId;
  114.         return "redirect:/home";
  115.     }
  116.  
  117.     @GetMapping("/home")
  118.     public String homeForm(HttpServletResponse httpServletResponse, Model model) throws IOException {
  119.         model.addAttribute("characterId", new Kostyly());
  120.         if (kostyly != null) {
  121.             CharactersEntity charactersEntity = null;
  122.             for (int i = 0; i < user.getCharacters().size(); i++) {
  123.                 if (Integer.parseInt(kostyly.getCharacterId())
  124.                         == user.getCharacters().get(i).getId()) {
  125.                     charactersEntity = user.getCharacters().get(i);
  126.                 }
  127.             }
  128.  
  129.             ObjectMapper objectMapper = new ObjectMapper();
  130.  
  131.             String jsonStr = objectMapper.writeValueAsString(charactersEntity);
  132.  
  133.  
  134.                 String jsonStr2 = objectMapper.writeValueAsString(charactersEntity.getSpells());
  135.  
  136.             String jsonStr3 = objectMapper.writeValueAsString(spellsRepository.findAll());
  137.  
  138.             httpServletResponse.addCookie(new Cookie("character", URLEncoder.encode(jsonStr, "UTF-8")));
  139.             if (charactersEntity.getSpells() != null) {
  140.                 httpServletResponse.addCookie(new Cookie("spells", URLEncoder.encode(jsonStr2, "UTF-8")));
  141.             }else {
  142.                 httpServletResponse.addCookie(new Cookie("spells", URLEncoder.encode("{}", "UTF-8")));
  143.             }
  144.             httpServletResponse.addCookie(new Cookie("allSpells", URLEncoder.encode(jsonStr3, "UTF-8")));
  145.             return "home";
  146.         } else
  147.             return "redirect:/greeting";
  148.     }
  149.  
  150.     @PostMapping("/home")
  151.     public String homeSubmit(@ModelAttribute Kostyly characterId) {
  152.         if (characterId.getTypeE().equals("addToCharacter")) {
  153.             int sendCharacterId = Integer.parseInt(characterId.getCharacterId());
  154.             System.out.println("sendCharacterId = " + sendCharacterId);
  155.             int sendSpellsId = Integer.parseInt(characterId.getSpellId());
  156.             System.out.println("sendSpellsId = " + sendSpellsId);
  157.             List<SpellsEntity> spellsEntities =
  158.                     spellsRepository.saveSpellInUser(sendCharacterId, sendSpellsId);
  159.  
  160.         } else if (characterId.getTypeE().equals("RemoveFromCharacter")) {
  161.             spellsRepository.deleteSpellInUser(Integer.parseInt(characterId.getCharacterId()),
  162.                     Integer.parseInt(characterId.getSpellId()));
  163.         }
  164.  
  165.         String login = user.getLogin();
  166.         String password = user.getPassword();
  167.  
  168.         UsersEntity userByLoginAndPassword =
  169.                 userService.getUserByLoginAndPassword(login, password);
  170.         user = userByLoginAndPassword;
  171.         return "redirect:/home";
  172.     }
  173.  
  174.     @GetMapping("/create_character")
  175.     public String characterForm(Model model) {
  176.         model.addAttribute("character", new CharactersEntity());
  177.         return "create_character";
  178.     }
  179.  
  180.     @PostMapping("/create_character")
  181.     public String characterSubmit(@ModelAttribute CharactersEntity character) {
  182.         character.setUser(user);
  183.         characterRepository.save(character);
  184.  
  185.         String login = user.getLogin();
  186.         String password = user.getPassword();
  187.  
  188.         UsersEntity userByLoginAndPassword =
  189.                 userService.getUserByLoginAndPassword(login, password);
  190.         user = userByLoginAndPassword;
  191.         return "redirect:/home";
  192.     }
  193.  
  194.  
  195.     @GetMapping("/admin_menu")
  196.     public String adminForm(HttpServletResponse httpServletResponse, Model model) throws JsonProcessingException, UnsupportedEncodingException {
  197.         ObjectMapper objectMapper = new ObjectMapper();
  198.         model.addAttribute("characterId", new Kostyly());
  199.         String jsonStr1 = objectMapper.writeValueAsString(spellsRepository.findAll());
  200.         String jsonStr2 = objectMapper.writeValueAsString(userService.getAllUsers());
  201.         String jsonStr3 = objectMapper.writeValueAsString(characterRepository.findAll());
  202.  
  203.         httpServletResponse.addCookie(new Cookie("allUsers", URLEncoder.encode(jsonStr2, "UTF-8")));
  204.         httpServletResponse.addCookie(new Cookie("allSpells", URLEncoder.encode(jsonStr1, "UTF-8")));
  205.         httpServletResponse.addCookie(new Cookie("character", URLEncoder.encode(jsonStr3, "UTF-8")));
  206.  
  207.         return "admin_menu";
  208.     }
  209.  
  210.     @PostMapping("/admin_menu")
  211.     public String adminSubmit(@ModelAttribute Kostyly kostyly) {
  212.         if (kostyly.getTypeE().equals("spell")) {
  213.             List<SpellsEntity> byId = spellsRepository.findById(Integer.parseInt(kostyly.getCharacterId()));
  214.             spellsRepository.delete(byId.get(0));
  215.         } else if (kostyly.getTypeE().equals("user")) {
  216.             userService.removeUser(Integer.parseInt(kostyly.getCharacterId()));
  217.         } else if (kostyly.getTypeE().equals("character")) {
  218.             List<CharactersEntity> byId = characterRepository.findById(Integer.parseInt(kostyly.getCharacterId()));
  219.             this.characterRepository.delete(byId.get(0));
  220.         }
  221.         return "redirect:/admin_menu";
  222.     }
  223.  
  224.     @GetMapping("/create_spell")
  225.     public String SpellForm(Model model) {
  226.         model.addAttribute("spell", new SpellsEntity());
  227.         return "create_spell";
  228.     }
  229.  
  230.     @PostMapping("/create_spell")
  231.     public String spellSubmit(@ModelAttribute SpellsEntity spell) {
  232.         spellsRepository.save(spell);
  233.         return "redirect:/admin_menu";
  234.  
  235.     }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement