From ed13d6b914718c4f9add417ba25655d13d316dd5 Mon Sep 17 00:00:00 2001 From: Andrey Kassaev Date: Mon, 11 Dec 2023 23:16:45 +0400 Subject: [PATCH] CRUD with mock repository --- build.gradle.kts | 3 ++ .../notes/controller/NoteController.kt | 42 +++++++++++++++++++ .../kotlin/com/kassaev/notes/model/Note.kt | 7 ++++ .../notes/repository/MockRepository.kt | 33 +++++++++++++++ .../com/kassaev/notes/service/NoteService.kt | 30 +++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 src/main/kotlin/com/kassaev/notes/controller/NoteController.kt create mode 100644 src/main/kotlin/com/kassaev/notes/model/Note.kt create mode 100644 src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt create mode 100644 src/main/kotlin/com/kassaev/notes/service/NoteService.kt diff --git a/build.gradle.kts b/build.gradle.kts index b303d6c..bcac1f0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,6 +23,9 @@ dependencies { implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") testImplementation("org.springframework.boot:spring-boot-starter-test") + + compileOnly("org.projectlombok:lombok:1.18.30") + annotationProcessor("org.projectlombok:lombok:1.18.30") } tasks.withType { diff --git a/src/main/kotlin/com/kassaev/notes/controller/NoteController.kt b/src/main/kotlin/com/kassaev/notes/controller/NoteController.kt new file mode 100644 index 0000000..cb6f343 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/controller/NoteController.kt @@ -0,0 +1,42 @@ +package com.kassaev.notes.controller + +import com.kassaev.notes.model.Note +import com.kassaev.notes.service.NoteService +import lombok.AllArgsConstructor +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/api/v1.0.0/note") +@AllArgsConstructor +class NoteController(val service: NoteService) { + +// @Autowired +// lateinit var service: NoteService + + @GetMapping("/all") + fun getAllNotes(): List { + return service.getAllNotes() + } + + @GetMapping("/{id}") + fun getNoteById(@PathVariable id: Int): Note? { + return service.getNoteById(id) + } + + @PutMapping("/update") + fun updateNote(@RequestBody note: Note): Note { + return service.updateNote(note) + } + + @DeleteMapping("/remove/{id}") + fun deleteNote(@PathVariable id: Int){ + return service.deleteNote(id) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/model/Note.kt b/src/main/kotlin/com/kassaev/notes/model/Note.kt new file mode 100644 index 0000000..e5e40f1 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/model/Note.kt @@ -0,0 +1,7 @@ +package com.kassaev.notes.model + +data class Note( + val id: Int, + val text: String, + val dateCreated: String +) \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt b/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt new file mode 100644 index 0000000..58b56ec --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/repository/MockRepository.kt @@ -0,0 +1,33 @@ +package com.kassaev.notes.repository + +import com.kassaev.notes.model.Note +import org.springframework.stereotype.Repository +import org.springframework.stereotype.Service +import java.util.* + +@Repository +class MockRepository { + + private val notesList = mutableListOf( + Note(id = 0, text = "first", dateCreated = "21:15"), + Note(id = 1, text = "second", dateCreated = "22:15"), + Note(id = 2, text = "third", dateCreated = "23:15"), + Note(id = 3, text = "fourth", dateCreated = "00:15"), + ) + fun getAllNotes(): List{ + return notesList + } + + fun getNoteById(id: Int): Note? { + return notesList.find{ it.id == id} + } + + fun updateNote(note: Note): Note { + notesList[note.id] = note + return note + } + + fun deleteNote(id: Int){ + notesList.removeIf { it.id == id } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/kassaev/notes/service/NoteService.kt b/src/main/kotlin/com/kassaev/notes/service/NoteService.kt new file mode 100644 index 0000000..bcd5ba2 --- /dev/null +++ b/src/main/kotlin/com/kassaev/notes/service/NoteService.kt @@ -0,0 +1,30 @@ +package com.kassaev.notes.service + +import com.kassaev.notes.model.Note +import com.kassaev.notes.repository.MockRepository +import lombok.AllArgsConstructor +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Service + +@Service +class NoteService(val repository: MockRepository) { + +// @Autowired +// lateinit var repository: MockRepository + + fun getAllNotes(): List{ + return repository.getAllNotes() + } + + fun getNoteById(id: Int): Note? { + return repository.getNoteById(id) + } + + fun updateNote(note: Note): Note { + return repository.updateNote(note) + } + + fun deleteNote(id: Int){ + repository.deleteNote(id) + } +} \ No newline at end of file