add menu commands
This commit is contained in:
parent
36ec674811
commit
7afaa089c6
12
src/main/data/UserRepository.kt
Normal file
12
src/main/data/UserRepository.kt
Normal file
@ -0,0 +1,12 @@
|
||||
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
|
||||
}
|
43
src/main/data/UserRepositoryImpl.kt
Normal file
43
src/main/data/UserRepositoryImpl.kt
Normal file
@ -0,0 +1,43 @@
|
||||
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
|
||||
}
|
||||
}
|
24
src/main/data/localDataSource.kt
Normal file
24
src/main/data/localDataSource.kt
Normal file
@ -0,0 +1,24 @@
|
||||
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",
|
||||
),
|
||||
)
|
11
src/main/data/model/UserDTO.kt
Normal file
11
src/main/data/model/UserDTO.kt
Normal file
@ -0,0 +1,11 @@
|
||||
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,14 +1,40 @@
|
||||
package org.example
|
||||
|
||||
import org.example.data.faetures.authorize.AuthorizeRepositoryImpl
|
||||
import org.example.domain.Service.AuthorizeserviceImpl
|
||||
import org.example.ui.model.AuthorizeUI
|
||||
import org.example.data.UserRepository
|
||||
import org.example.data.UserRepositoryImpl
|
||||
import org.example.domain.UserUseCaseImpl
|
||||
import org.example.ui.UserUI
|
||||
|
||||
fun main() {
|
||||
val authorizeRepository = AuthorizeRepositoryImpl()
|
||||
val authorizeServiceImpl = AuthorizeserviceImpl(authorizeRepository)
|
||||
val authorizeUI = AuthorizeUI(authorizeServiceImpl)
|
||||
val userRepository = UserRepositoryImpl()
|
||||
val userUseCase = UserUseCaseImpl(userRepository)
|
||||
val userUI = UserUI(userUseCase)
|
||||
|
||||
authorizeUI.registration()
|
||||
authorizeUI.authorize()
|
||||
while (true){
|
||||
// Меню команд
|
||||
println("Выберерите команду:")
|
||||
println("1. Авторизация")
|
||||
println("2. Регистрация")
|
||||
println("3. Выход")
|
||||
print("Команда: ")
|
||||
|
||||
// Выбор команды
|
||||
val comand = readLine()?.toInt()
|
||||
|
||||
if (comand == 1){
|
||||
println("Авторизавия")
|
||||
userUI.authorize()
|
||||
}
|
||||
else if (comand == 2){
|
||||
println("Регистрация")
|
||||
userUI.registration()
|
||||
}
|
||||
else if (comand == 3){
|
||||
break
|
||||
}
|
||||
else{
|
||||
println("Неверная команда!!!")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
12
src/main/kotlin/data/UserRepository.kt
Normal file
12
src/main/kotlin/data/UserRepository.kt
Normal file
@ -0,0 +1,12 @@
|
||||
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
|
||||
}
|
43
src/main/kotlin/data/UserRepositoryImpl.kt
Normal file
43
src/main/kotlin/data/UserRepositoryImpl.kt
Normal file
@ -0,0 +1,43 @@
|
||||
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,11 +0,0 @@
|
||||
package org.example.data.faetures.authorize
|
||||
|
||||
import org.example.domain.Request.AuthorizeRequest
|
||||
import org.example.domain.Request.RegistrationRequest
|
||||
import org.example.domain.Response.UserResponse
|
||||
|
||||
|
||||
interface AuthorizeRepository {
|
||||
fun auth(authorizeRequest: AuthorizeRequest): UserResponse
|
||||
fun resitration(request: RegistrationRequest): UserResponse
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package org.example.data.faetures.authorize
|
||||
|
||||
import io.github.serpro69.kfaker.Faker
|
||||
import org.example.data.faetures.model.UserDTO
|
||||
import org.example.domain.Request.AuthorizeRequest
|
||||
import org.example.domain.Request.RegistrationRequest
|
||||
import org.example.domain.Response.UserResponse
|
||||
|
||||
class AuthorizeRepositoryImpl: AuthorizeRepository {
|
||||
val faker = Faker()
|
||||
val user = mutableListOf<UserDTO>(
|
||||
|
||||
)
|
||||
override fun auth(authorizeRequest: AuthorizeRequest): UserResponse {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun resitration(request: RegistrationRequest): UserResponse {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package org.example.data.faetures.model
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
data class UserDTO(
|
||||
val uuid: UUID,
|
||||
var firstname: String,
|
||||
var lastname: String?,
|
||||
var email: String,
|
||||
var number: String?,
|
||||
var address: String?,
|
||||
var photo: String?
|
||||
)
|
24
src/main/kotlin/data/localDataSource.kt
Normal file
24
src/main/kotlin/data/localDataSource.kt
Normal file
@ -0,0 +1,24 @@
|
||||
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",
|
||||
),
|
||||
)
|
11
src/main/kotlin/data/model/UserDTO.kt
Normal file
11
src/main/kotlin/data/model/UserDTO.kt
Normal file
@ -0,0 +1,11 @@
|
||||
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,4 +1,4 @@
|
||||
package org.example.domain.Request
|
||||
package org.example.domain.request
|
||||
|
||||
data class AuthorizeRequest(
|
||||
val email: String,
|
||||
|
@ -1,8 +1,7 @@
|
||||
package org.example.domain.Request
|
||||
package org.example.domain.request
|
||||
|
||||
data class RegistrationRequest (
|
||||
val firstname: String,
|
||||
val email: String,
|
||||
val password: String,
|
||||
|
||||
val firstName: String
|
||||
)
|
@ -1,10 +1,10 @@
|
||||
package org.example.domain.Response
|
||||
package org.example.domain.response
|
||||
|
||||
data class UserResponse(
|
||||
val firstname: String,
|
||||
val lastname: String?,
|
||||
val email: String,
|
||||
val number: String?,
|
||||
val address: String?,
|
||||
val photo: String?
|
||||
)
|
||||
val userId: Int,
|
||||
var firstName: String,
|
||||
var lastName: String? = null,
|
||||
var email: String,
|
||||
var phone: String? = null,
|
||||
var address: String? = null,
|
||||
)
|
||||
|
@ -1,11 +0,0 @@
|
||||
package org.example.domain.Service
|
||||
|
||||
import org.example.domain.Request.AuthorizeRequest
|
||||
import org.example.domain.Request.RegistrationRequest
|
||||
import org.example.ui.model.UserPresenter
|
||||
|
||||
|
||||
interface AuthorizeService {
|
||||
fun auth(authorizeRequest: AuthorizeRequest): UserPresenter
|
||||
fun registration(registrationRequest: RegistrationRequest): UserPresenter
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.example.domain.Service
|
||||
|
||||
import org.example.data.faetures.authorize.AuthorizeRepository
|
||||
import org.example.domain.Request.AuthorizeRequest
|
||||
import org.example.domain.Request.RegistrationRequest
|
||||
import org.example.ui.model.UserPresenter
|
||||
|
||||
class AuthorizeserviceImpl(private val authorizeRepository: AuthorizeRepository): AuthorizeService {
|
||||
override fun auth(authorizeRequest: AuthorizeRequest): UserPresenter {
|
||||
val user = authorizeRepository.auth(authorizeRequest)
|
||||
return UserPresenter()
|
||||
}
|
||||
|
||||
override fun registration(registrationRequest: RegistrationRequest): UserPresenter {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
12
src/main/kotlin/domain/UserUseCase.kt
Normal file
12
src/main/kotlin/domain/UserUseCase.kt
Normal file
@ -0,0 +1,12 @@
|
||||
package org.example.domain
|
||||
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.request.RegistrationRequest
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
interface UserUseCase {
|
||||
fun authorize(authorizeRequest: AuthorizeRequest): UserResponse
|
||||
fun registration(registrationRequest: RegistrationRequest): UserResponse
|
||||
fun changePassword()
|
||||
fun changeProfile()
|
||||
}
|
44
src/main/kotlin/domain/UserUseCaseImpl.kt
Normal file
44
src/main/kotlin/domain/UserUseCaseImpl.kt
Normal file
@ -0,0 +1,44 @@
|
||||
package org.example.domain
|
||||
|
||||
import org.example.data.UserRepository
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.request.RegistrationRequest
|
||||
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(registrationRequest: RegistrationRequest): UserResponse {
|
||||
//уникальность почты
|
||||
val isUnique = userRepository.getAllUsers().firstOrNull{
|
||||
it.email == registrationRequest.email
|
||||
} == null
|
||||
require(isUnique){
|
||||
"Такая почта уже существует"
|
||||
}
|
||||
val newUser = userRepository.addUser(registrationRequest)
|
||||
return UserDtoToUserResponse(newUser)
|
||||
}
|
||||
|
||||
|
||||
|
||||
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,
|
||||
)
|
71
src/main/kotlin/ui/UserUI.kt
Normal file
71
src/main/kotlin/ui/UserUI.kt
Normal file
@ -0,0 +1,71 @@
|
||||
package org.example.ui
|
||||
|
||||
import org.example.domain.UserUseCase
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.request.RegistrationRequest
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
class UserUI(private val userUseCase: UserUseCase) {
|
||||
fun authorize(){
|
||||
println("Введите почту")
|
||||
val email = readlnOrNull()
|
||||
checkNotNull(email){
|
||||
"Почта не должна отсутствовать"
|
||||
}
|
||||
println("Введите пароль")
|
||||
val password = readlnOrNull()
|
||||
checkNotNull(password){
|
||||
"Пароль не должен отсутствовать"
|
||||
}
|
||||
val authorizeRequest = AuthorizeRequest(
|
||||
email = email,
|
||||
password = password,)
|
||||
val user = userUseCase.authorize(authorizeRequest)
|
||||
println(userResponseToString(user))
|
||||
}
|
||||
|
||||
fun registration(){
|
||||
println("Введите имя")
|
||||
val firstName = readlnOrNull()
|
||||
checkNotNull(firstName){
|
||||
"Имя не должно отсутствовать"
|
||||
}
|
||||
println("Введите почту")
|
||||
val email = readlnOrNull()
|
||||
checkNotNull(email){
|
||||
"Почта не должна отсутствовать"
|
||||
}
|
||||
println("Введите пароль")
|
||||
val password = readlnOrNull()
|
||||
checkNotNull(password){
|
||||
"Пароль не должен отсутствовать"
|
||||
}
|
||||
val registrationRequest = RegistrationRequest(
|
||||
email = email,
|
||||
firstName = firstName,
|
||||
password = password
|
||||
)
|
||||
val newUser = userUseCase.registration(registrationRequest)
|
||||
userResponseToString(newUser)
|
||||
}
|
||||
|
||||
private fun userResponseToString(userResponse: UserResponse): String{
|
||||
val printOutput = StringBuilder()
|
||||
printOutput.append("Ваша почта ${userResponse.email}")
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваше имя ${userResponse.firstName}")
|
||||
if (!userResponse.lastName.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваше фамилия ${userResponse.lastName}")
|
||||
}
|
||||
if (!userResponse.phone.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваш телефон ${userResponse.phone}")
|
||||
}
|
||||
if (!userResponse.address.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваш адрес ${userResponse.phone}")
|
||||
}
|
||||
return printOutput.toString()
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package org.example.ui.model
|
||||
|
||||
import org.example.domain.Request.AuthorizeRequest
|
||||
import org.example.domain.Service.AuthorizeService
|
||||
|
||||
class AuthorizeUI(val authorizeService: AuthorizeService) {
|
||||
fun authorize(){
|
||||
println("Enter the email")
|
||||
val email = readlnOrNull()
|
||||
println("Enter the password")
|
||||
val password = readlnOrNull()
|
||||
checkNotNull(email)
|
||||
checkNotNull(password)
|
||||
authorizeService.auth(AuthorizeRequest(email = email, password = password))
|
||||
}
|
||||
fun registration(){
|
||||
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package org.example.ui.model
|
||||
|
||||
data class UserPresenter()
|
71
src/main/ui/UserUI.kt
Normal file
71
src/main/ui/UserUI.kt
Normal file
@ -0,0 +1,71 @@
|
||||
package org.example.ui
|
||||
|
||||
import org.example.domain.UserUseCase
|
||||
import org.example.domain.request.AuthorizeRequest
|
||||
import org.example.domain.request.RegistrationRequest
|
||||
import org.example.domain.response.UserResponse
|
||||
|
||||
class UserUI(private val userUseCase: UserUseCase) {
|
||||
fun authorize(){
|
||||
println("Введите почту")
|
||||
val email = readlnOrNull()
|
||||
checkNotNull(email){
|
||||
"Почта не должна отсутствовать"
|
||||
}
|
||||
println("Введите пароль")
|
||||
val password = readlnOrNull()
|
||||
checkNotNull(password){
|
||||
"Пароль не должен отсутствовать"
|
||||
}
|
||||
val authorizeRequest = AuthorizeRequest(
|
||||
email = email,
|
||||
password = password,)
|
||||
val user = userUseCase.authorize(authorizeRequest)
|
||||
println(userResponseToString(user))
|
||||
}
|
||||
|
||||
fun registration(){
|
||||
println("Введите имя")
|
||||
val firstName = readlnOrNull()
|
||||
checkNotNull(firstName){
|
||||
"Имя не должно отсутствовать"
|
||||
}
|
||||
println("Введите почту")
|
||||
val email = readlnOrNull()
|
||||
checkNotNull(email){
|
||||
"Почта не должна отсутствовать"
|
||||
}
|
||||
println("Введите пароль")
|
||||
val password = readlnOrNull()
|
||||
checkNotNull(password){
|
||||
"Пароль не должен отсутствовать"
|
||||
}
|
||||
val registrationRequest = RegistrationRequest(
|
||||
email = email,
|
||||
firstName = firstName,
|
||||
password = password
|
||||
)
|
||||
val newUser = userUseCase.registration(registrationRequest)
|
||||
userResponseToString(newUser)
|
||||
}
|
||||
|
||||
private fun userResponseToString(userResponse: UserResponse): String{
|
||||
val printOutput = StringBuilder()
|
||||
printOutput.append("Ваша почта ${userResponse.email}")
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваше имя ${userResponse.firstName}")
|
||||
if (!userResponse.lastName.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваше фамилия ${userResponse.lastName}")
|
||||
}
|
||||
if (!userResponse.phone.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваш телефон ${userResponse.phone}")
|
||||
}
|
||||
if (!userResponse.address.isNullOrBlank()){
|
||||
printOutput.appendLine()
|
||||
printOutput.append("Ваш адрес ${userResponse.phone}")
|
||||
}
|
||||
return printOutput.toString()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user