diff --git a/src/main/kotlin/data/UserRepository.kt b/src/main/kotlin/data/UserRepository.kt index 19ea027..6814e59 100644 --- a/src/main/kotlin/data/UserRepository.kt +++ b/src/main/kotlin/data/UserRepository.kt @@ -1,5 +1,11 @@ package org.example.data -interface UserRepository { +import org.example.data.model.UserDTO +interface UserRepository { + fun addUser() + fun removeUserById(userId: Int): Boolean + fun updateUserById(userId: Int, newUser: UserDTO): UserDTO + fun getAllUsers(): List + fun findUserById(userId: Int): UserDTO } \ No newline at end of file diff --git a/src/main/kotlin/data/UserRepositoryImpl.kt b/src/main/kotlin/data/UserRepositoryImpl.kt index 8cd1788..bd3122a 100644 --- a/src/main/kotlin/data/UserRepositoryImpl.kt +++ b/src/main/kotlin/data/UserRepositoryImpl.kt @@ -1,4 +1,33 @@ package org.example.data -class UserRepositoryImpl { +import org.example.data.model.UserDTO + +class UserRepositoryImpl: UserRepository { + private val userSource: MutableList = userList.toMutableList() + + override fun addUser() { + TODO("Not yet implemented") + } + + override fun removeUserById(userId: Int): Boolean { + return userSource.removeIf { it.userId == userId } + } + + override fun updateUserById(userId: Int, newUser: UserDTO): UserDTO { + val oldUser = userSource.first { it.userId == userId } + oldUser.phone = newUser.phone + oldUser.address = newUser.address + oldUser.firstName = newUser.firstName + oldUser.lastName = newUser.lastName + oldUser.password = newUser.password + return oldUser + } + + override fun getAllUsers(): List = userList + + override fun findUserById(userId: Int): UserDTO { + val findUser = userList.firstOrNull { it.userId == userId } + checkNotNull(findUser) + return findUser + } } \ No newline at end of file diff --git a/src/main/kotlin/data/model/UserDTO.kt b/src/main/kotlin/data/model/UserDTO.kt index bc257f0..678a1ac 100644 --- a/src/main/kotlin/data/model/UserDTO.kt +++ b/src/main/kotlin/data/model/UserDTO.kt @@ -2,7 +2,7 @@ package org.example.data.model data class UserDTO( val userId: Int, - val firstName: String, + var firstName: String, var lastName: String? = null, var password: String, var phone: String? = null,