Migrated to DB use
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
package net.tokishu.note.controller;
|
||||
|
||||
import net.tokishu.note.dto.NoteRequest;
|
||||
import net.tokishu.note.model.Note;
|
||||
import net.tokishu.note.service.NoteService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
public class NotesController {
|
||||
@@ -14,27 +20,30 @@ public class NotesController {
|
||||
private NoteService noteService;
|
||||
|
||||
@GetMapping("/notes")
|
||||
public List<Note> getAll(){
|
||||
return noteService.getAll();
|
||||
public ResponseEntity<?> getAll(){
|
||||
return ResponseEntity.ok(noteService.getAll());
|
||||
}
|
||||
|
||||
@GetMapping("/note/{id}")
|
||||
public Note find(@PathVariable Long id){
|
||||
return noteService.find(id);
|
||||
@GetMapping("/note/{uuid}")
|
||||
public Note find(@PathVariable UUID uuid){
|
||||
return noteService.find(uuid);
|
||||
}
|
||||
|
||||
@PostMapping("/note")
|
||||
public Note add(@RequestBody Note note){
|
||||
return noteService.add(note);
|
||||
public ResponseEntity<?> add(@RequestBody NoteRequest note){
|
||||
noteService.add(note);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("message", "Note added"));
|
||||
}
|
||||
|
||||
@PutMapping("/note/{id}")
|
||||
public Note update(@PathVariable Long id, @RequestBody Note note){
|
||||
return noteService.update(id, note);
|
||||
@PutMapping("/note/{uuid}")
|
||||
public ResponseEntity<?> update(@PathVariable("uuid") UUID uuid, @RequestBody NoteRequest note){
|
||||
noteService.update(uuid, note);
|
||||
return ResponseEntity.ok(Map.of("message", "Note updated"));
|
||||
}
|
||||
|
||||
@DeleteMapping("/note/{id}")
|
||||
public void delete(@PathVariable Long id){
|
||||
noteService.delete(id);
|
||||
@DeleteMapping("/note/{uuid}")
|
||||
public ResponseEntity<?> delete(@PathVariable UUID uuid){
|
||||
noteService.delete(uuid);
|
||||
return ResponseEntity.ok(Map.of(" message", "Note deleted"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.tokishu.note.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class NoteRequest {
|
||||
private String name;
|
||||
private String text;
|
||||
}
|
||||
@@ -1,46 +1,18 @@
|
||||
package net.tokishu.note.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "Notes")
|
||||
public class Note {
|
||||
private Long id;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private UUID uuid;
|
||||
private String name;
|
||||
private String text;
|
||||
|
||||
public Note(Long id, String name, String text) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Note{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", text='" + text + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.tokishu.note.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface NoteRepository extends JpaRepository<net.tokishu.note.model.Note, UUID> {
|
||||
}
|
||||
@@ -1,52 +1,52 @@
|
||||
package net.tokishu.note.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.tokishu.note.dto.NoteRequest;
|
||||
import net.tokishu.note.model.Note;
|
||||
import net.tokishu.note.repo.NoteRepository;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class NoteService {
|
||||
private List<Note> notes = new ArrayList<>();
|
||||
private AtomicLong idGenerator = new AtomicLong(1);
|
||||
|
||||
public final NoteRepository noteRepository;
|
||||
|
||||
public List<Note> getAll(){
|
||||
return notes;
|
||||
return noteRepository.findAll();
|
||||
}
|
||||
|
||||
public Note find(Long id) {
|
||||
for (Note note : notes) {
|
||||
if (note.getId().equals(id)) {
|
||||
return note;
|
||||
}
|
||||
}
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Note not found");
|
||||
public Note find(UUID uuid) {
|
||||
return noteRepository.findById(uuid)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Note not found"));
|
||||
}
|
||||
|
||||
|
||||
public Note add(Note note){
|
||||
note.setId(idGenerator.getAndIncrement());
|
||||
notes.add(note);
|
||||
return note;
|
||||
public Note add(NoteRequest data){
|
||||
Note note = new Note();
|
||||
note.setName(data.getName());
|
||||
note.setText(data.getText());
|
||||
|
||||
return noteRepository.save(note);
|
||||
}
|
||||
|
||||
public Note update(Long id, Note updatedNote){
|
||||
for (Note note : notes){
|
||||
if (note.getId().equals(id)) {
|
||||
note.setName(updatedNote.getName());
|
||||
note.setText(updatedNote.getText());
|
||||
return note;
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException("Note not found");
|
||||
public Note update(UUID uuid, NoteRequest data){
|
||||
Note existing = noteRepository.findById(uuid)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Note not found"));
|
||||
existing.setName(data.getName());
|
||||
existing.setText(data.getText());
|
||||
|
||||
return noteRepository.save(existing);
|
||||
}
|
||||
|
||||
public void delete(Long id){
|
||||
notes.removeIf(note -> note.getId().equals(id));
|
||||
public void delete(UUID uuid){
|
||||
Note existing = noteRepository.findById(uuid)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Note not found"));
|
||||
noteRepository.deleteById(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,11 @@
|
||||
spring.application.name=notes
|
||||
spring.mvc.servlet.path=/api
|
||||
spring.mvc.servlet.path=/api
|
||||
|
||||
# DB
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/notes
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=password
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
|
||||
# Disable trace
|
||||
server.error.include-stacktrace=never
|
||||
Reference in New Issue
Block a user