/
NikolayVasiljev
/
Project
Обзор
Документация
Войти
/
NikolayVasiljev
/
Project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
feature
src/main/java/org/example/notesystem/controller/NoteController.java
100 строк
3 KB
VasiljevNik
My sixth commit
07 авг 2025, 13:30
07 авг 2025, 13:30
7369756
Код
Авторство
О чём код?
package org.example.notesystem.controller; import org.example.notesystem.model.Note; import org.example.notesystem.service.NoteService; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.security.access.annotation.Secured; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.Optional; @Controller @RequestMapping("/notes") public class NoteController { private final NoteService service; public NoteController(NoteService service) { this.service = service; } @GetMapping public String listNotes(Model model) { model.addAttribute("notes", service.getAll()); return "notes"; // шаблон notes.html } @GetMapping("/new") public String newNoteForm(Model model) { model.addAttribute("note", new Note()); return "new_note"; // шаблон new_note.html } @PostMapping public String saveNote(@ModelAttribute Note note, @RequestParam("file") MultipartFile file) { try { if (!file.isEmpty()) { note.setAttachedFile(file.getBytes()); note.setFileName(file.getOriginalFilename()); } Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String currentUsername = auth.getName(); note.setAuthorUsername(currentUsername); service.save(note); } catch (IOException e) { e.printStackTrace(); return "error"; } return "redirect:/notes"; } @GetMapping("/file/{id}") public ResponseEntity<byte[]> downloadFile(@PathVariable Long id) { Optional<Note> noteOpt = service.noteFindById(id); if (noteOpt.isPresent() && noteOpt.get().getAttachedFile() != null) { Note note = noteOpt.get(); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + note.getFileName() + "\"") .body(note.getAttachedFile()); } else { return ResponseEntity.notFound().build(); } } @Secured("ROLE_ADMIN") @PostMapping("/delete/{id}") public String deleteNote(@PathVariable Long id) { service.delete(id); return "redirect:/notes"; } @Secured("ROLE_ADMIN") @GetMapping("/edit/{id}") public String editNoteForm(@PathVariable Long id, Model model) { Optional<Note> noteOpt = service.noteFindById(id); if (noteOpt.isPresent()) { model.addAttribute("note", noteOpt.get()); return "edit_note"; } else { // Обработка отсутствия заметки return "redirect:/notes"; } } @Secured("ROLE_ADMIN") @PostMapping("/edit/{id}") public String saveEditedNote(@PathVariable Long id, @ModelAttribute Note note) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String currentUsername = auth.getName(); note.setAuthorUsername(currentUsername); note.setId(id); service.save(note); return "redirect:/notes"; } }