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
@@ -0,0 +1,13 @@
package net.tokishu.note;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class NotesApplicationTests {
@Test
void contextLoads() {
}
}
@@ -0,0 +1,48 @@
package net.tokishu.note.service;
import net.tokishu.note.model.Note;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class NoteServiceTest {
private NoteService noteService;
@BeforeEach
void setUp(){
noteService = new NoteService();
}
@Test
void addAndGetAllNotes(){
Note note = new Note(1L, "QQ", "Test content");
noteService.add(note);
List<Note> notes = noteService.getAll();
assertEquals(1, notes.size());
assertEquals("QQ", notes.get(0).getName());
}
@Test
void updateNote() {
Note note = new Note(1L, "Old QQ", "Old test content");
noteService.add(note);
Note updatedNote = new Note(1L, "New day, new qq :3", "New test content");
noteService.update(1L, updatedNote);
Note result = noteService.getAll().get(0);
assertEquals("New day, new qq :3", result.getName());
}
@Test
void deleteNote() {
noteService.add(new Note(1L, "Bye bye :(", "content"));
noteService.delete(1L);
assertTrue(noteService.getAll().isEmpty());
}
}