added functions to data and domain
This commit is contained in:
parent
7afaa089c6
commit
b03a3becda
@ -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<UserDTO>
|
|
||||||
fun findUserById(userId: Int): UserDTO
|
|
||||||
}
|
|
@ -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<UserDTO> = 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<UserDTO> = userList
|
|
||||||
|
|
||||||
override fun findUserById(userId: Int): UserDTO {
|
|
||||||
val findUser = userList.firstOrNull { it.userId == userId }
|
|
||||||
checkNotNull(findUser)
|
|
||||||
return findUser
|
|
||||||
}
|
|
||||||
}
|
|
@ -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",
|
|
||||||
),
|
|
||||||
)
|
|
@ -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,
|
|
||||||
)
|
|
@ -1,8 +1,7 @@
|
|||||||
package org.example
|
package org.example
|
||||||
|
|
||||||
import org.example.data.UserRepository
|
|
||||||
import org.example.data.UserRepositoryImpl
|
import org.example.data.UserRepositoryImpl
|
||||||
import org.example.domain.UserUseCaseImpl
|
import org.example.domain.UseCase.UserUseCaseImpl
|
||||||
import org.example.ui.UserUI
|
import org.example.ui.UserUI
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
@ -15,7 +14,8 @@ fun main() {
|
|||||||
println("Выберерите команду:")
|
println("Выберерите команду:")
|
||||||
println("1. Авторизация")
|
println("1. Авторизация")
|
||||||
println("2. Регистрация")
|
println("2. Регистрация")
|
||||||
println("3. Выход")
|
println("3. Вывод всех товаров")
|
||||||
|
println("7. Выход")
|
||||||
print("Команда: ")
|
print("Команда: ")
|
||||||
|
|
||||||
// Выбор команды
|
// Выбор команды
|
||||||
@ -35,6 +35,5 @@ fun main() {
|
|||||||
else{
|
else{
|
||||||
println("Неверная команда!!!")
|
println("Неверная команда!!!")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
src/main/kotlin/data/ShoesRepository.kt
Normal file
9
src/main/kotlin/data/ShoesRepository.kt
Normal file
@ -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<ShoesDTO>
|
||||||
|
fun removeShoesById(shoesId: Int): Boolean
|
||||||
|
}
|
28
src/main/kotlin/data/ShoesRepositoryImpl.kt
Normal file
28
src/main/kotlin/data/ShoesRepositoryImpl.kt
Normal file
@ -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<ShoesDTO> = 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<ShoesDTO> = shoesList
|
||||||
|
|
||||||
|
override fun removeShoesById(shoesId: Int): Boolean {
|
||||||
|
return shoesSource.removeIf { it.shoesId == shoesId }
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,19 @@
|
|||||||
package org.example.data
|
package org.example.data
|
||||||
|
|
||||||
|
import org.example.data.model.ShoesDTO
|
||||||
import org.example.data.model.UserDTO
|
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(
|
val userList = listOf(
|
||||||
UserDTO(
|
UserDTO(
|
||||||
userId = 1,
|
userId = 1,
|
||||||
|
10
src/main/kotlin/data/model/ShoesDTO.kt
Normal file
10
src/main/kotlin/data/model/ShoesDTO.kt
Normal file
@ -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
|
||||||
|
)
|
10
src/main/kotlin/domain/Request/AddShoesRequest.kt
Normal file
10
src/main/kotlin/domain/Request/AddShoesRequest.kt
Normal file
@ -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
|
||||||
|
)
|
6
src/main/kotlin/domain/Request/changePasswordRequest.kt
Normal file
6
src/main/kotlin/domain/Request/changePasswordRequest.kt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package org.example.domain.request
|
||||||
|
|
||||||
|
data class ChangePasswordRequest (
|
||||||
|
val userId: Int,
|
||||||
|
val newPassword: String
|
||||||
|
)
|
10
src/main/kotlin/domain/Response/ShoesResponse.kt
Normal file
10
src/main/kotlin/domain/Response/ShoesResponse.kt
Normal file
@ -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
|
||||||
|
)
|
10
src/main/kotlin/domain/UseCase/ShoesUseCase.kt
Normal file
10
src/main/kotlin/domain/UseCase/ShoesUseCase.kt
Normal file
@ -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()
|
||||||
|
}
|
20
src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt
Normal file
20
src/main/kotlin/domain/UseCase/ShoesUseCaseImpl.kt
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.example.domain
|
package org.example.domain.UseCase
|
||||||
|
|
||||||
import org.example.domain.request.AuthorizeRequest
|
import org.example.domain.request.AuthorizeRequest
|
||||||
import org.example.domain.request.RegistrationRequest
|
import org.example.domain.request.RegistrationRequest
|
@ -1,6 +1,7 @@
|
|||||||
package org.example.domain
|
package org.example.domain.UseCase
|
||||||
|
|
||||||
import org.example.data.UserRepository
|
import org.example.data.UserRepository
|
||||||
|
import org.example.domain.UserDtoToUserResponse
|
||||||
import org.example.domain.request.AuthorizeRequest
|
import org.example.domain.request.AuthorizeRequest
|
||||||
import org.example.domain.request.RegistrationRequest
|
import org.example.domain.request.RegistrationRequest
|
||||||
import org.example.domain.response.UserResponse
|
import org.example.domain.response.UserResponse
|
||||||
@ -32,8 +33,6 @@ class UserUseCaseImpl(private val userRepository: UserRepository): UserUseCase {
|
|||||||
return UserDtoToUserResponse(newUser)
|
return UserDtoToUserResponse(newUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
override fun changePassword() {
|
override fun changePassword() {
|
||||||
TODO("Not yet implemented")
|
TODO("Not yet implemented")
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package org.example.domain
|
package org.example.domain
|
||||||
|
|
||||||
|
import org.example.data.model.ShoesDTO
|
||||||
import org.example.data.model.UserDTO
|
import org.example.data.model.UserDTO
|
||||||
|
import org.example.domain.response.ShoesResponse
|
||||||
import org.example.domain.response.UserResponse
|
import org.example.domain.response.UserResponse
|
||||||
|
|
||||||
fun UserDtoToUserResponse(userDTO: UserDTO) = UserResponse(
|
fun UserDtoToUserResponse(userDTO: UserDTO) = UserResponse(
|
||||||
@ -10,4 +12,12 @@ fun UserDtoToUserResponse(userDTO: UserDTO) = UserResponse(
|
|||||||
phone = userDTO.phone,
|
phone = userDTO.phone,
|
||||||
userId = userDTO.userId,
|
userId = userDTO.userId,
|
||||||
address = userDTO.address,
|
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
|
||||||
)
|
)
|
72
src/main/kotlin/ui/MainMenuUI.kt
Normal file
72
src/main/kotlin/ui/MainMenuUI.kt
Normal file
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package org.example.ui
|
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.AuthorizeRequest
|
||||||
import org.example.domain.request.RegistrationRequest
|
import org.example.domain.request.RegistrationRequest
|
||||||
import org.example.domain.response.UserResponse
|
import org.example.domain.response.UserResponse
|
||||||
|
Loading…
Reference in New Issue
Block a user