add repository logic
This commit is contained in:
parent
392e04b511
commit
10cf45bf3e
@ -1,5 +1,11 @@
|
|||||||
package org.example.data
|
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<UserDTO>
|
||||||
|
fun findUserById(userId: Int): UserDTO
|
||||||
}
|
}
|
@ -1,4 +1,33 @@
|
|||||||
package org.example.data
|
package org.example.data
|
||||||
|
|
||||||
class UserRepositoryImpl {
|
import org.example.data.model.UserDTO
|
||||||
|
|
||||||
|
class UserRepositoryImpl: UserRepository {
|
||||||
|
private val userSource: MutableList<UserDTO> = 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<UserDTO> = userList
|
||||||
|
|
||||||
|
override fun findUserById(userId: Int): UserDTO {
|
||||||
|
val findUser = userList.firstOrNull { it.userId == userId }
|
||||||
|
checkNotNull(findUser)
|
||||||
|
return findUser
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ package org.example.data.model
|
|||||||
|
|
||||||
data class UserDTO(
|
data class UserDTO(
|
||||||
val userId: Int,
|
val userId: Int,
|
||||||
val firstName: String,
|
var firstName: String,
|
||||||
var lastName: String? = null,
|
var lastName: String? = null,
|
||||||
var password: String,
|
var password: String,
|
||||||
var phone: String? = null,
|
var phone: String? = null,
|
||||||
|
Loading…
Reference in New Issue
Block a user