add domain logic
This commit is contained in:
parent
10cf45bf3e
commit
edbe53fff5
@ -5,7 +5,7 @@ import org.example.data.model.UserDTO
|
||||
interface UserRepository {
|
||||
fun addUser()
|
||||
fun removeUserById(userId: Int): Boolean
|
||||
fun updateUserById(userId: Int, newUser: UserDTO): UserDTO
|
||||
fun updateUserById(userId: Int, updatedUser: UserDTO): UserDTO
|
||||
fun getAllUsers(): List<UserDTO>
|
||||
fun findUserById(userId: Int): UserDTO
|
||||
}
|
@ -13,13 +13,14 @@ class UserRepositoryImpl: UserRepository {
|
||||
return userSource.removeIf { it.userId == userId }
|
||||
}
|
||||
|
||||
override fun updateUserById(userId: Int, newUser: UserDTO): UserDTO {
|
||||
override fun updateUserById(userId: Int, updatedUser: 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
|
||||
oldUser.phone = updatedUser.phone
|
||||
oldUser.address = updatedUser.address
|
||||
oldUser.firstName = updatedUser.firstName
|
||||
oldUser.lastName = updatedUser.lastName
|
||||
oldUser.password = updatedUser.password
|
||||
oldUser.email = updatedUser.email
|
||||
return oldUser
|
||||
}
|
||||
|
||||
|
@ -6,16 +6,19 @@ val userList = listOf(
|
||||
UserDTO(
|
||||
userId = 1,
|
||||
firstName = "Andrey",
|
||||
password = "123"
|
||||
password = "123",
|
||||
email = "test@mail.ru"
|
||||
),
|
||||
UserDTO(
|
||||
userId = 2,
|
||||
firstName = "Alexey",
|
||||
password = "321"
|
||||
password = "321",
|
||||
email = "test2@mail.ru"
|
||||
),
|
||||
UserDTO(
|
||||
userId = 3,
|
||||
firstName = "Oleg",
|
||||
password = "423"
|
||||
password = "423",
|
||||
email = "test3@mail.ru",
|
||||
),
|
||||
)
|
@ -5,6 +5,7 @@ data class UserDTO(
|
||||
var firstName: String,
|
||||
var lastName: String? = null,
|
||||
var password: String,
|
||||
var email: String,
|
||||
var phone: String? = null,
|
||||
var address: String? = null,
|
||||
)
|
||||
|
11
src/main/kotlin/domain/UserUseCase.kt
Normal file
11
src/main/kotlin/domain/UserUseCase.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package org.example.domain
|
||||
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
interface UserUseCase {
|
||||
fun authorize(authorizeRequest: AuthorizeRequest): UserResponse
|
||||
fun registration()
|
||||
fun changePassword()
|
||||
fun changeProfile()
|
||||
}
|
33
src/main/kotlin/domain/UserUseCaseImpl.kt
Normal file
33
src/main/kotlin/domain/UserUseCaseImpl.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package org.example.domain
|
||||
|
||||
import org.example.data.UserRepository
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
class UserUseCaseImpl(private val userRepository: UserRepository): UserUseCase {
|
||||
override fun authorize(authorizeRequest: AuthorizeRequest): UserResponse {
|
||||
val findUser = userRepository.getAllUsers()
|
||||
.firstOrNull {
|
||||
it.email == authorizeRequest.email
|
||||
}
|
||||
checkNotNull(findUser){
|
||||
"Пользователь с такой почтой не найден"
|
||||
}
|
||||
require(findUser.password == authorizeRequest.password){
|
||||
"Пароли не совпадают"
|
||||
}
|
||||
return UserDtoTOUserResponse(findUser)
|
||||
}
|
||||
|
||||
override fun registration() {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun changePassword() {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun changeProfile() {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
13
src/main/kotlin/domain/mapper.kt
Normal file
13
src/main/kotlin/domain/mapper.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package org.example.domain
|
||||
|
||||
import org.example.data.model.UserDTO
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
fun UserDtoTOUserResponse(userDTO: UserDTO) = UserResponse(
|
||||
lastName = userDTO.lastName,
|
||||
firstName = userDTO.firstName,
|
||||
email = userDTO.email,
|
||||
phone = userDTO.phone,
|
||||
userId = userDTO.userId,
|
||||
address = userDTO.address,
|
||||
)
|
6
src/main/kotlin/domain/request/AuthorizeRequest.kt
Normal file
6
src/main/kotlin/domain/request/AuthorizeRequest.kt
Normal file
@ -0,0 +1,6 @@
|
||||
package org.example.domain.request
|
||||
|
||||
data class AuthorizeRequest(
|
||||
val email: String,
|
||||
val password: String
|
||||
)
|
10
src/main/kotlin/domain/response/UserResponse.kt
Normal file
10
src/main/kotlin/domain/response/UserResponse.kt
Normal file
@ -0,0 +1,10 @@
|
||||
package org.example.domain.response
|
||||
|
||||
data class UserResponse(
|
||||
val userId: Int,
|
||||
var firstName: String,
|
||||
var lastName: String? = null,
|
||||
var email: String,
|
||||
var phone: String? = null,
|
||||
var address: String? = null,
|
||||
)
|
Loading…
Reference in New Issue
Block a user