package com.example.notes;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@RestController
@RequestMapping("/api/notes")
public class NoteController {
private final NoteRepository repo;
public NoteController(NoteRepository repo) {
this.repo = repo;
}
@GetMapping
public List<Note> list() {
return repo.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Note create(@RequestBody NoteCreate body) {
return repo.save(new Note(body.text()));
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
if (!repo.existsById(id)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
repo.deleteById(id);
}
public record NoteCreate(String text) {}
}
package com.example.notes;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@RestController
@RequestMapping("/api/notes")
public class NoteController {
private final NoteRepository repo;
public NoteController(NoteRepository repo) {
this.repo = repo;
}
@GetMapping
public List<Note> list() {
return repo.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Note create(@RequestBody NoteCreate body) {
return repo.save(new Note(body.text()));
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
if (!repo.existsById(id)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
repo.deleteById(id);
}
public record NoteCreate(String text) {}
}