Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.mrchebik.web;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
- import ru.mrchebik.data.NoteRepository;
- import ru.mrchebik.model.Note;
- import ru.mrchebik.session.UserSession;
- import java.util.ArrayList;
- import java.util.List;
- import static org.springframework.web.bind.annotation.RequestMethod.GET;
- import static org.springframework.web.bind.annotation.RequestMethod.POST;
- /**
- * Created by mrchebik on 08.08.16.
- */
- @Controller
- @RequestMapping("/notes")
- public class NotesController {
- private NoteRepository noteRepository;
- public NotesController() {
- }
- @Autowired
- public NotesController(NoteRepository noteRepository) {
- this.noteRepository = noteRepository;
- }
- @RequestMapping(value = "/{username}", method = GET)
- public String notes(@PathVariable String username,
- Model model) {
- model.addAttribute("username", username);
- return "Notes";
- }
- @RequestMapping(value = "/{username}/add", method = GET)
- public String Get(@PathVariable String username,
- Model model) {
- model.addAttribute("username", username);
- return "AddNote";
- }
- @RequestMapping(value = "{username}/add", method = POST)
- public void add(@PathVariable("username") String username,
- @RequestBody Note note) {
- noteRepository.add(new Note(UserSession.getUser(), note.getTitle(), note.getText()));
- }
- @RequestMapping(value = "/{username}/view", method = GET)
- public String view(@PathVariable String username,
- @RequestParam(value = "hide", defaultValue = "1") int page,
- @RequestParam(value = "hideId", defaultValue = "0") long id,
- Model model) {
- if (id != 0) {
- noteRepository.remove(id);
- }
- List<Note> notes = new ArrayList<>(noteRepository.findNotes(UserSession.getUser().getUSER_ID()));
- UserSession.setPages(notes, UserSession.getCount());
- if (notes.size() > UserSession.getCount()) {
- if (page * UserSession.getCount() > notes.size()) {
- notes = notes.subList((page - 1) * UserSession.getCount(), notes.size());
- } else {
- notes = notes.subList((page - 1) * UserSession.getCount(), page * UserSession.getCount());
- }
- }
- model.addAttribute("username", username);
- model.addAttribute("notes", notes);
- model.addAttribute("page", page);
- model.addAttribute("pages", UserSession.getPages());
- return "ViewNotes";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement