From b03a3becda4ec02db4771c56d7779a5657b88e12 Mon Sep 17 00:00:00 2001 From: NikitaOnianov Date: Mon, 3 Feb 2025 19:26:52 +0300 Subject: [PATCH] added functions to data and domain --- src/main/data/UserRepository.kt | 12 ---- src/main/data/UserRepositoryImpl.kt | 43 ----------- src/main/data/localDataSource.kt | 24 ------- src/main/data/model/UserDTO.kt | 11 --- src/main/kotlin/Main.kt | 9 ++- src/main/kotlin/data/ShoesRepository.kt | 9 +++ src/main/kotlin/data/ShoesRepositoryImpl.kt | 28 ++++++++ src/main/kotlin/data/localDataSource.kt | 12 ++++ src/main/kotlin/data/model/ShoesDTO.kt | 10 +++ .../kotlin/domain/Request/AddShoesRequest.kt | 10 +++ .../domain/Request/changePasswordRequest.kt | 6 ++ .../kotlin/domain/Response/ShoesResponse.kt | 10 +++ .../kotlin/domain/UseCase/ShoesUseCase.kt | 10 +++ .../kotlin/domain/UseCase/ShoesUseCaseImpl.kt | 20 ++++++ .../domain/{ => UseCase}/UserUseCase.kt | 2 +- .../domain/{ => UseCase}/UserUseCaseImpl.kt | 5 +- src/main/kotlin/domain/mapper.kt | 10 +++ src/main/kotlin/ui/MainMenuUI.kt | 72 +++++++++++++++++++ src/main/kotlin/ui/UserUI.kt | 2 +- 19 files changed, 205 insertions(+), 100 deletions(-) delete mode 100644 src/main/data/UserRepository.kt delete mode 100644 src/main/data/UserRepositoryImpl.kt delete mode 100644 src/main/data/localDataSource.kt delete mode 100644 src/main/data/model/UserDTO.kt create mode 100644 src/main/kotlin/data/ShoesRepository.kt create mode 100644 src/main/kotlin/data/ShoesRepositoryImpl.kt create mode 100644 src/main/kotlin/data/model/ShoesDTO.kt create mode 100644 src/main/kotlin/domain/Request/AddShoesRequest.kt create mode 100644 src/main/kotlin/domain/Request/changePasswordRequest.kt create mode 100644 src/main/kotlin/domain/Response/ShoesResponse.kt create mode 100644 src/main/kotlin/domain/UseCase/ShoesUseCase.kt create mode 100644 src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt rename src/main/kotlin/domain/{ => UseCase}/UserUseCase.kt (91%) rename src/main/kotlin/domain/{ => UseCase}/UserUseCaseImpl.kt (94%) create mode 100644 src/main/kotlin/ui/MainMenuUI.kt diff --git a/src/main/data/UserRepository.kt b/src/main/data/UserRepository.kt deleted file mode 100644 index dc250ff..0000000 --- a/src/main/data/UserRepository.kt +++ /dev/null @@ -1,12 +0,0 @@ -package org.example.data - -import org.example.data.model.UserDTO -import org.example.domain.request.RegistrationRequest - -interface UserRepository { - fun addUser(registrationRequest: RegistrationRequest): UserDTO - fun removeUserById(userId: Int): Boolean - 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/data/UserRepositoryImpl.kt b/src/main/data/UserRepositoryImpl.kt deleted file mode 100644 index ae9fc56..0000000 --- a/src/main/data/UserRepositoryImpl.kt +++ /dev/null @@ -1,43 +0,0 @@ -package org.example.data - -import org.example.data.model.UserDTO -import org.example.domain.request.RegistrationRequest - -class UserRepositoryImpl: UserRepository { - private val userSource: MutableList = userList.toMutableList() - - - override fun addUser(registrationRequest: RegistrationRequest): UserDTO { - val newUser = UserDTO( - userId = userSource.size + 1, - email = registrationRequest.email, - firstName = registrationRequest.firstName, - password = registrationRequest.password - ) - userSource.add(newUser) - return newUser - } - - override fun removeUserById(userId: Int): Boolean { - return userSource.removeIf { it.userId == userId } - } - - override fun updateUserById(userId: Int, updatedUser: UserDTO): UserDTO { - val oldUser = userSource.first { it.userId == userId } - 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 - } - - 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/data/localDataSource.kt b/src/main/data/localDataSource.kt deleted file mode 100644 index d2f8a7b..0000000 --- a/src/main/data/localDataSource.kt +++ /dev/null @@ -1,24 +0,0 @@ -package org.example.data - -import org.example.data.model.UserDTO - -val userList = listOf( - UserDTO( - userId = 1, - firstName = "Andrey", - password = "123", - email = "test@mail.ru" - ), - UserDTO( - userId = 2, - firstName = "Alexey", - password = "321", - email = "test2@mail.ru" - ), - UserDTO( - userId = 3, - firstName = "Oleg", - password = "423", - email = "test3@mail.ru", - ), -) \ No newline at end of file diff --git a/src/main/data/model/UserDTO.kt b/src/main/data/model/UserDTO.kt deleted file mode 100644 index 518121b..0000000 --- a/src/main/data/model/UserDTO.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.example.data.model - -data class UserDTO( - val userId: Int, - 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/Main.kt b/src/main/kotlin/Main.kt index a0bb9b1..6543153 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,8 +1,7 @@ package org.example -import org.example.data.UserRepository import org.example.data.UserRepositoryImpl -import org.example.domain.UserUseCaseImpl +import org.example.domain.UseCase.UserUseCaseImpl import org.example.ui.UserUI fun main() { @@ -15,7 +14,8 @@ fun main() { println("Выберерите команду:") println("1. Авторизация") println("2. Регистрация") - println("3. Выход") + println("3. Вывод всех товаров") + println("7. Выход") print("Команда: ") // Выбор команды @@ -35,6 +35,5 @@ fun main() { else{ println("Неверная команда!!!") } - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/data/ShoesRepository.kt b/src/main/kotlin/data/ShoesRepository.kt new file mode 100644 index 0000000..2069f6e --- /dev/null +++ b/src/main/kotlin/data/ShoesRepository.kt @@ -0,0 +1,9 @@ +package org.example.data +import org.example.data.model.ShoesDTO +import org.example.domain.request.AddShoesRequest + +interface ShoesRepository { + fun addShoes(shoes: AddShoesRequest ): ShoesDTO + fun getAllShoes(): List + fun removeShoesById(shoesId: Int): Boolean +} \ No newline at end of file diff --git a/src/main/kotlin/data/ShoesRepositoryImpl.kt b/src/main/kotlin/data/ShoesRepositoryImpl.kt new file mode 100644 index 0000000..52fa9a0 --- /dev/null +++ b/src/main/kotlin/data/ShoesRepositoryImpl.kt @@ -0,0 +1,28 @@ +package org.example.data + +import org.example.data.model.ShoesDTO +import org.example.domain.request.AddShoesRequest + +class ShoesRepositoryImpl: ShoesRepository { + private val shoesSource: MutableList = shoesList.toMutableList() + + + override fun addShoes(shoes: AddShoesRequest ): ShoesDTO { + val newShoes = ShoesDTO( + shoesId = shoesSource.size + 1, + shoesName = shoes.Name, + shoesDescription = shoes.Description, + shoesUrl = shoes.Url, + category = shoes.category, + price = shoes.price + ) + shoesSource.add(newShoes) + return newShoes + } + + override fun getAllShoes(): List = shoesList + + override fun removeShoesById(shoesId: Int): Boolean { + return shoesSource.removeIf { it.shoesId == shoesId } + } +} diff --git a/src/main/kotlin/data/localDataSource.kt b/src/main/kotlin/data/localDataSource.kt index d2f8a7b..b937ad4 100644 --- a/src/main/kotlin/data/localDataSource.kt +++ b/src/main/kotlin/data/localDataSource.kt @@ -1,7 +1,19 @@ package org.example.data +import org.example.data.model.ShoesDTO import org.example.data.model.UserDTO +val shoesList = listOf( + ShoesDTO( + shoesId = 1, + shoesName = "1", + shoesDescription = "123", + shoesUrl = "3", + category = "123", + price = 1.1 + ) +) + val userList = listOf( UserDTO( userId = 1, diff --git a/src/main/kotlin/data/model/ShoesDTO.kt b/src/main/kotlin/data/model/ShoesDTO.kt new file mode 100644 index 0000000..e9aa205 --- /dev/null +++ b/src/main/kotlin/data/model/ShoesDTO.kt @@ -0,0 +1,10 @@ +package org.example.data.model + +data class ShoesDTO ( + val shoesId: Int, + val shoesName: String, + val shoesDescription: String, + val shoesUrl: String, + val category: String, + val price: Double +) \ No newline at end of file diff --git a/src/main/kotlin/domain/Request/AddShoesRequest.kt b/src/main/kotlin/domain/Request/AddShoesRequest.kt new file mode 100644 index 0000000..ecabaf7 --- /dev/null +++ b/src/main/kotlin/domain/Request/AddShoesRequest.kt @@ -0,0 +1,10 @@ +package org.example.domain.request + +data class AddShoesRequest( + val shoesId: Int, + val Name: String, + val Description: String, + val Url: String, + val category: String, + val price: Double +) diff --git a/src/main/kotlin/domain/Request/changePasswordRequest.kt b/src/main/kotlin/domain/Request/changePasswordRequest.kt new file mode 100644 index 0000000..3957895 --- /dev/null +++ b/src/main/kotlin/domain/Request/changePasswordRequest.kt @@ -0,0 +1,6 @@ +package org.example.domain.request + +data class ChangePasswordRequest ( + val userId: Int, + val newPassword: String +) \ No newline at end of file diff --git a/src/main/kotlin/domain/Response/ShoesResponse.kt b/src/main/kotlin/domain/Response/ShoesResponse.kt new file mode 100644 index 0000000..4015ccd --- /dev/null +++ b/src/main/kotlin/domain/Response/ShoesResponse.kt @@ -0,0 +1,10 @@ +package org.example.domain.response + +data class ShoesResponse ( + val shoesId: Int, + val shoesName: String, + val shoesDescription: String, + val shoesUrl: String, + val category: String, + val price: Double +) \ No newline at end of file diff --git a/src/main/kotlin/domain/UseCase/ShoesUseCase.kt b/src/main/kotlin/domain/UseCase/ShoesUseCase.kt new file mode 100644 index 0000000..0101729 --- /dev/null +++ b/src/main/kotlin/domain/UseCase/ShoesUseCase.kt @@ -0,0 +1,10 @@ +package org.example.domain.UseCase + +import org.example.domain.response.ShoesResponse +import org.example.domain.request.AddShoesRequest + +interface ShoesUseCase { + fun addShoes(addShoesRequest: AddShoesRequest): ShoesResponse + fun getAll() + fun getAllById() +} \ No newline at end of file diff --git a/src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt b/src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt new file mode 100644 index 0000000..5256c18 --- /dev/null +++ b/src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt @@ -0,0 +1,20 @@ +package org.example.domain.UseCase + +import org.example.data.ShoesRepository +import org.example.domain.ShoesDTOToShoesResponse +import org.example.domain.request.AddShoesRequest +import org.example.domain.response.ShoesResponse + +class ShoesUseCaseImpl(private val ShoesRepository: ShoesRepository): ShoesUseCase { + override fun addShoesRequest(addShoesRequest: AddShoesRequest): ShoesResponse { + //уникальность имени + val isUnique = ShoesRepository.getAllShoes().firstOrNull{ + it.shoesName == addShoesRequest.Name + } == null + require(isUnique){ + "такой товар существует" + } + val newShoes = ShoesRepository.addShoes(addShoesRequest) + return ShoesDTOToShoesResponse(newShoes) + } +} \ No newline at end of file diff --git a/src/main/kotlin/domain/UserUseCase.kt b/src/main/kotlin/domain/UseCase/UserUseCase.kt similarity index 91% rename from src/main/kotlin/domain/UserUseCase.kt rename to src/main/kotlin/domain/UseCase/UserUseCase.kt index 504fb54..6aa9ef7 100644 --- a/src/main/kotlin/domain/UserUseCase.kt +++ b/src/main/kotlin/domain/UseCase/UserUseCase.kt @@ -1,4 +1,4 @@ -package org.example.domain +package org.example.domain.UseCase import org.example.domain.request.AuthorizeRequest import org.example.domain.request.RegistrationRequest diff --git a/src/main/kotlin/domain/UserUseCaseImpl.kt b/src/main/kotlin/domain/UseCase/UserUseCaseImpl.kt similarity index 94% rename from src/main/kotlin/domain/UserUseCaseImpl.kt rename to src/main/kotlin/domain/UseCase/UserUseCaseImpl.kt index cbc9e08..9d2f89c 100644 --- a/src/main/kotlin/domain/UserUseCaseImpl.kt +++ b/src/main/kotlin/domain/UseCase/UserUseCaseImpl.kt @@ -1,6 +1,7 @@ -package org.example.domain +package org.example.domain.UseCase import org.example.data.UserRepository +import org.example.domain.UserDtoToUserResponse import org.example.domain.request.AuthorizeRequest import org.example.domain.request.RegistrationRequest import org.example.domain.response.UserResponse @@ -32,8 +33,6 @@ class UserUseCaseImpl(private val userRepository: UserRepository): UserUseCase { return UserDtoToUserResponse(newUser) } - - override fun changePassword() { TODO("Not yet implemented") } diff --git a/src/main/kotlin/domain/mapper.kt b/src/main/kotlin/domain/mapper.kt index 6885c6e..88abe3a 100644 --- a/src/main/kotlin/domain/mapper.kt +++ b/src/main/kotlin/domain/mapper.kt @@ -1,6 +1,8 @@ package org.example.domain +import org.example.data.model.ShoesDTO import org.example.data.model.UserDTO +import org.example.domain.response.ShoesResponse import org.example.domain.response.UserResponse fun UserDtoToUserResponse(userDTO: UserDTO) = UserResponse( @@ -10,4 +12,12 @@ fun UserDtoToUserResponse(userDTO: UserDTO) = UserResponse( phone = userDTO.phone, userId = userDTO.userId, address = userDTO.address, +) +fun ShoesDTOToShoesResponse(shoesDTO: ShoesDTO) = ShoesResponse( + shoesId = shoesDTO.shoesId, + shoesName = shoesDTO.shoesName, + shoesDescription = shoesDTO.shoesDescription, + shoesUrl = shoesDTO.shoesUrl, + category = shoesDTO.category, + price = shoesDTO.price ) \ No newline at end of file diff --git a/src/main/kotlin/ui/MainMenuUI.kt b/src/main/kotlin/ui/MainMenuUI.kt new file mode 100644 index 0000000..e4cb0a9 --- /dev/null +++ b/src/main/kotlin/ui/MainMenuUI.kt @@ -0,0 +1,72 @@ +package org.example.ui + +class MainMenuUI( + private val userUI: UserUI +){ + val menuItems = listOf( + "1. Авторизоваться", + "2. Зарегестрироваться", + "3. Exit" + + ) + val menuAuthorizedItems = listOf( + "1. Change password", + "2. Edit profile", + "3. Exit" + + ) + private fun displayMenuItem(menuItem: () -> Unit){ + try { + menuItem() + } + catch (e: Exception){ + println(e.message) + userUI.userAuthorized?.let { + displayMenuForAuthorizeUser() + } + } + } + + fun displayStartMenu(){ + print(menuItems.joinToString("\n")) + val menuPosition = readlnOrNull()?.toIntOrNull() + if(menuPosition == null) displayStartMenu() + when(menuPosition){ + 1 -> { + displayMenuItem { + userUI.authorize() + displayMenuForAuthorizeUser() + } + } + 2 -> { + userUI.registration() + } + 3 ->{ + return + } + else -> { + displayStartMenu() + } + } + } + + private fun displayMenuForAuthorizeUser(){ + print(menuAuthorizedItems.joinToString("\n")) + val menuPosition = readlnOrNull()?.toIntOrNull() + if(menuPosition == null) displayMenuForAuthorizeUser() + when(menuPosition){ + 1 -> { + displayMenuItem { + userUI.changePassword() + } + } + 2 -> { + userUI.edit() + } + 3 -> { + return + } + else -> displayMenuForAuthorizeUser() + } + } +} diff --git a/src/main/kotlin/ui/UserUI.kt b/src/main/kotlin/ui/UserUI.kt index 485a73c..5208985 100644 --- a/src/main/kotlin/ui/UserUI.kt +++ b/src/main/kotlin/ui/UserUI.kt @@ -1,6 +1,6 @@ package org.example.ui -import org.example.domain.UserUseCase +import org.example.domain.UseCase.UserUseCase import org.example.domain.request.AuthorizeRequest import org.example.domain.request.RegistrationRequest import org.example.domain.response.UserResponse