First Commit

This commit is contained in:
Tokishu
2025-05-15 01:15:42 +02:00
commit 18f5330e7b
13 changed files with 720 additions and 0 deletions
@@ -0,0 +1,13 @@
package net.tokishu.note;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NotesApplication {
public static void main(String[] args) {
SpringApplication.run(NotesApplication.class, args);
}
}
@@ -0,0 +1,35 @@
package net.tokishu.note.controller;
import net.tokishu.note.model.Note;
import net.tokishu.note.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class NotesController {
@Autowired
private NoteService noteService;
@GetMapping("/notes")
public List<Note> getAll(){
return noteService.getAll();
}
@PostMapping("/note")
public Note add(@RequestBody Note note){
return noteService.add(note);
}
@PutMapping("/note/{id}")
public Note update(@PathVariable Long id, @RequestBody Note note){
return noteService.update(id, note);
}
@DeleteMapping("/note/{id}")
public void delete(@PathVariable Long id){
noteService.delete(id);
}
}
@@ -0,0 +1,46 @@
package net.tokishu.note.model;
public class Note {
private Long id;
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,40 @@
package net.tokishu.note.service;
import net.tokishu.note.model.Note;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicLong;
@Service
public class NoteService {
private List<Note> notes = new ArrayList<>();
private AtomicLong idGenerator = new AtomicLong(1);
public List<Note> getAll(){
return notes;
}
public Note add(Note note){
note.setId(idGenerator.getAndIncrement());
notes.add(note);
return 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 void delete(Long id){
notes.removeIf(note -> note.getId().equals(id));
}
}
@@ -0,0 +1,2 @@
spring.application.name=notes
spring.mvc.servlet.path=/api