CRUD with mock repository
This commit is contained in:
parent
88f38ea80e
commit
ed13d6b914
|
|
@ -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<KotlinCompile> {
|
||||
|
|
|
|||
|
|
@ -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<Note> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.kassaev.notes.model
|
||||
|
||||
data class Note(
|
||||
val id: Int,
|
||||
val text: String,
|
||||
val dateCreated: String
|
||||
)
|
||||
|
|
@ -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<Note>{
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Note>{
|
||||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue