Advertisement
mrchebik

Untitled

Aug 27th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package ru.mrchebik.web;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.*;
  7. import ru.mrchebik.data.NoteRepository;
  8. import ru.mrchebik.model.Note;
  9. import ru.mrchebik.session.UserSession;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import static org.springframework.web.bind.annotation.RequestMethod.GET;
  15. import static org.springframework.web.bind.annotation.RequestMethod.POST;
  16.  
  17. /**
  18.  * Created by mrchebik on 08.08.16.
  19.  */
  20. @Controller
  21. @RequestMapping("/notes")
  22. public class NotesController {
  23.     private NoteRepository noteRepository;
  24.  
  25.     public NotesController() {
  26.  
  27.     }
  28.  
  29.     @Autowired
  30.     public NotesController(NoteRepository noteRepository) {
  31.         this.noteRepository = noteRepository;
  32.     }
  33.  
  34.     @RequestMapping(value = "/{username}", method = GET)
  35.     public String notes(@PathVariable String username,
  36.                         Model model) {
  37.         model.addAttribute("username", username);
  38.         return "Notes";
  39.     }
  40.  
  41.     @RequestMapping(value = "/{username}/add", method = GET)
  42.     public String Get(@PathVariable String username,
  43.                       Model model) {
  44.         model.addAttribute("username", username);
  45.         return "AddNote";
  46.     }
  47.  
  48.     @RequestMapping(value = "{username}/add", method = POST)
  49.     public void add(@PathVariable("username") String username,
  50.                     @RequestBody Note note) {
  51.         noteRepository.add(new Note(UserSession.getUser(), note.getTitle(), note.getText()));
  52.     }
  53.  
  54.     @RequestMapping(value = "/{username}/view", method = GET)
  55.     public String view(@PathVariable String username,
  56.                        @RequestParam(value = "hide", defaultValue = "1") int page,
  57.                        @RequestParam(value = "hideId", defaultValue = "0") long id,
  58.                        Model model) {
  59.  
  60.         if (id != 0) {
  61.             noteRepository.remove(id);
  62.         }
  63.  
  64.         List<Note> notes = new ArrayList<>(noteRepository.findNotes(UserSession.getUser().getUSER_ID()));
  65.         UserSession.setPages(notes, UserSession.getCount());
  66.  
  67.         if (notes.size() > UserSession.getCount()) {
  68.             if (page * UserSession.getCount() > notes.size()) {
  69.                 notes = notes.subList((page - 1) * UserSession.getCount(), notes.size());
  70.             } else {
  71.                 notes = notes.subList((page - 1) * UserSession.getCount(), page * UserSession.getCount());
  72.             }
  73.         }
  74.  
  75.         model.addAttribute("username", username);
  76.         model.addAttribute("notes", notes);
  77.         model.addAttribute("page", page);
  78.         model.addAttribute("pages", UserSession.getPages());
  79.  
  80.         return "ViewNotes";
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement