From edbe53fff5083ca660080f17381755145b8f5e09 Mon Sep 17 00:00:00 2001 From: KP9lKk Date: Thu, 23 Jan 2025 10:42:30 +0300 Subject: [PATCH] add domain logic --- src/main/kotlin/data/UserRepository.kt | 2 +- src/main/kotlin/data/UserRepositoryImpl.kt | 13 ++++---- src/main/kotlin/data/localDataSource.kt | 9 +++-- src/main/kotlin/data/model/UserDTO.kt | 1 + src/main/kotlin/domain/UserUseCase.kt | 11 +++++++ src/main/kotlin/domain/UserUseCaseImpl.kt | 33 +++++++++++++++++++ src/main/kotlin/domain/mapper.kt | 13 ++++++++ .../kotlin/domain/request/AuthorizeRequest.kt | 6 ++++ .../kotlin/domain/response/UserResponse.kt | 10 ++++++ 9 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/domain/UserUseCase.kt create mode 100644 src/main/kotlin/domain/UserUseCaseImpl.kt create mode 100644 src/main/kotlin/domain/mapper.kt create mode 100644 src/main/kotlin/domain/request/AuthorizeRequest.kt create mode 100644 src/main/kotlin/domain/response/UserResponse.kt diff --git a/src/main/kotlin/data/UserRepository.kt b/src/main/kotlin/data/UserRepository.kt index 6814e59..9878def 100644 --- a/src/main/kotlin/data/UserRepository.kt +++ b/src/main/kotlin/data/UserRepository.kt @@ -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 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 bd3122a..e7a3c42 100644 --- a/src/main/kotlin/data/UserRepositoryImpl.kt +++ b/src/main/kotlin/data/UserRepositoryImpl.kt @@ -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 } diff --git a/src/main/kotlin/data/localDataSource.kt b/src/main/kotlin/data/localDataSource.kt index 74d3ca0..d2f8a7b 100644 --- a/src/main/kotlin/data/localDataSource.kt +++ b/src/main/kotlin/data/localDataSource.kt @@ -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", ), ) \ 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 678a1ac..518121b 100644 --- a/src/main/kotlin/data/model/UserDTO.kt +++ b/src/main/kotlin/data/model/UserDTO.kt @@ -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, ) diff --git a/src/main/kotlin/domain/UserUseCase.kt b/src/main/kotlin/domain/UserUseCase.kt new file mode 100644 index 0000000..c4a9c75 --- /dev/null +++ b/src/main/kotlin/domain/UserUseCase.kt @@ -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() +} \ No newline at end of file diff --git a/src/main/kotlin/domain/UserUseCaseImpl.kt b/src/main/kotlin/domain/UserUseCaseImpl.kt new file mode 100644 index 0000000..18c9bcb --- /dev/null +++ b/src/main/kotlin/domain/UserUseCaseImpl.kt @@ -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") + } +} \ No newline at end of file diff --git a/src/main/kotlin/domain/mapper.kt b/src/main/kotlin/domain/mapper.kt new file mode 100644 index 0000000..cf1dd3b --- /dev/null +++ b/src/main/kotlin/domain/mapper.kt @@ -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, +) \ No newline at end of file diff --git a/src/main/kotlin/domain/request/AuthorizeRequest.kt b/src/main/kotlin/domain/request/AuthorizeRequest.kt new file mode 100644 index 0000000..11ba605 --- /dev/null +++ b/src/main/kotlin/domain/request/AuthorizeRequest.kt @@ -0,0 +1,6 @@ +package org.example.domain.request + +data class AuthorizeRequest( + val email: String, + val password: String +) diff --git a/src/main/kotlin/domain/response/UserResponse.kt b/src/main/kotlin/domain/response/UserResponse.kt new file mode 100644 index 0000000..1fdd8f2 --- /dev/null +++ b/src/main/kotlin/domain/response/UserResponse.kt @@ -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, +)