add domain logic
This commit is contained in:
parent
10cf45bf3e
commit
edbe53fff5
@ -5,7 +5,7 @@ import org.example.data.model.UserDTO
|
|||||||
interface UserRepository {
|
interface UserRepository {
|
||||||
fun addUser()
|
fun addUser()
|
||||||
fun removeUserById(userId: Int): Boolean
|
fun removeUserById(userId: Int): Boolean
|
||||||
fun updateUserById(userId: Int, newUser: UserDTO): UserDTO
|
fun updateUserById(userId: Int, updatedUser: UserDTO): UserDTO
|
||||||
fun getAllUsers(): List<UserDTO>
|
fun getAllUsers(): List<UserDTO>
|
||||||
fun findUserById(userId: Int): UserDTO
|
fun findUserById(userId: Int): UserDTO
|
||||||
}
|
}
|
@ -13,13 +13,14 @@ class UserRepositoryImpl: UserRepository {
|
|||||||
return userSource.removeIf { it.userId == userId }
|
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 }
|
val oldUser = userSource.first { it.userId == userId }
|
||||||
oldUser.phone = newUser.phone
|
oldUser.phone = updatedUser.phone
|
||||||
oldUser.address = newUser.address
|
oldUser.address = updatedUser.address
|
||||||
oldUser.firstName = newUser.firstName
|
oldUser.firstName = updatedUser.firstName
|
||||||
oldUser.lastName = newUser.lastName
|
oldUser.lastName = updatedUser.lastName
|
||||||
oldUser.password = newUser.password
|
oldUser.password = updatedUser.password
|
||||||
|
oldUser.email = updatedUser.email
|
||||||
return oldUser
|
return oldUser
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,16 +6,19 @@ val userList = listOf(
|
|||||||
UserDTO(
|
UserDTO(
|
||||||
userId = 1,
|
userId = 1,
|
||||||
firstName = "Andrey",
|
firstName = "Andrey",
|
||||||
password = "123"
|
password = "123",
|
||||||
|
email = "test@mail.ru"
|
||||||
),
|
),
|
||||||
UserDTO(
|
UserDTO(
|
||||||
userId = 2,
|
userId = 2,
|
||||||
firstName = "Alexey",
|
firstName = "Alexey",
|
||||||
password = "321"
|
password = "321",
|
||||||
|
email = "test2@mail.ru"
|
||||||
),
|
),
|
||||||
UserDTO(
|
UserDTO(
|
||||||
userId = 3,
|
userId = 3,
|
||||||
firstName = "Oleg",
|
firstName = "Oleg",
|
||||||
password = "423"
|
password = "423",
|
||||||
|
email = "test3@mail.ru",
|
||||||
),
|
),
|
||||||
)
|
)
|
@ -5,6 +5,7 @@ data class UserDTO(
|
|||||||
var firstName: String,
|
var firstName: String,
|
||||||
var lastName: String? = null,
|
var lastName: String? = null,
|
||||||
var password: String,
|
var password: String,
|
||||||
|
var email: String,
|
||||||
var phone: String? = null,
|
var phone: String? = null,
|
||||||
var address: 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