more files

This commit is contained in:
adm 2024-11-14 17:39:41 +03:00
parent 4cbe9c453e
commit a4e6a081f1
12 changed files with 141 additions and 18 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -1,16 +1,22 @@
import domain.AuthUseCaseImpl
import data.models.User import data.models.User
import data.repository.NewUserRepositoryImpl
import data.repository.UserRepository import data.repository.UserRepository
import data.repository.UserRepositoryImpl import data.repository.UserRepositoryImpl
import domain.AuthUseCase
import domain.models.RegistrationRequest
fun main() { fun main() {
val repository = UserRepositoryImpl() val repository = UserRepositoryImpl()
val useCase = AuthUseCaseImpl(repository)
while (true) { while (true) {
println("1.Вывести пользователей\n2.Добавить пользователя")
println("1.Вывести пользователей\n2.Регистрация пользователя")
when (readln().toInt()) { when (readln().toInt()) {
1 -> printAllUsers(repository) 1 -> printAllUsers(repository)
2 -> addUser(repository) 2 -> addUser(useCase)
else -> break else -> break
} }
} }
@ -23,14 +29,13 @@ fun printAllUsers(userRepository: UserRepository) {
} }
} }
fun addUser(userRepository: UserRepository){ fun addUser(useCase: AuthUseCase){
println("Введите почту") println("Введите почту")
val email = readln() val email = readln()
println("Введите имя") println("Введите Имя Фамилию")
val firstName = readln() val firstName = readln()
println("Введите фамилию") println("Введите пароль")
val lastName = readln() val password = readln()
val id = userRepository.getAllUsers().count() + 1 val user = RegistrationRequest(email = email, name = firstName, password = password)
val user = User(email = email, firstName = firstName, lastName = lastName, id = id) useCase.registration(user)
userRepository.addUser(user)
} }

View File

@ -0,0 +1,6 @@
package data.models
data class AddUserRequest(val firstName:String,
val lastName: String,
val password:String,
val email:String)

View File

@ -3,4 +3,5 @@ package data.models
data class User(val id: Int, data class User(val id: Int,
val firstName: String, val firstName: String,
val lastName: String, val lastName: String,
val password: String,
val email:String) val email:String)

View File

@ -0,0 +1,22 @@
package data.repository
import data.models.AddUserRequest
import data.models.User
class NewUserRepositoryImpl:UserRepository {
override fun getAllUsers(): List<User> {
TODO("Not yet implemented")
}
override fun addUser(newUser: AddUserRequest): User {
TODO("Not yet implemented")
}
override fun removeUserById(id: Int): Boolean {
TODO("Not yet implemented")
}
override fun updateUser(id: Int, newUser: User): Boolean {
TODO("Not yet implemented")
}
}

View File

@ -1,10 +1,11 @@
package data.repository package data.repository
import data.models.AddUserRequest
import data.models.User import data.models.User
interface UserRepository { interface UserRepository {
fun getAllUsers(): List<User> fun getAllUsers(): List<User>
fun addUser(user: User): Boolean fun addUser(newUser: AddUserRequest): User
fun removeUserById(id: Int): Boolean fun removeUserById(id: Int): Boolean
fun updateUser(id:Int, newUser: User): Boolean fun updateUser(id:Int, newUser: User): Boolean
} }

View File

@ -1,21 +1,30 @@
package data.repository package data.repository
import data.models.AddUserRequest
import data.models.User import data.models.User
class UserRepositoryImpl : UserRepository { class UserRepositoryImpl : UserRepository {
private val _users = mutableListOf<User>( private val _users = mutableListOf<User>(
User(id = 1, lastName = "Ivanov", firstName = "Ivan", email = "ivan@mai.ru"), User(id = 1, lastName = "Ivanov", firstName = "Ivan", email = "ivan@mai.ru", password = "123"),
User(id = 2, lastName = "Petrov", firstName = "Petr", email = "petr@mai.ru"), User(id = 2, lastName = "Petrov", firstName = "Petr", email = "petr@mai.ru", password = "123"),
User(id = 3, lastName = "Alex", firstName = "Alexandrov", email = "Alex@mai.ru"), User(id = 3, lastName = "Alex", firstName = "Alexandrov", email = "Alex@mai.ru", password = "123"),
User(id = 4, lastName = "Kolya", firstName = "Nikolaev", email = "kolya@mai.ru"), User(id = 4, lastName = "Kolya", firstName = "Nikolaev", email = "kolya@mai.ru", password = "123"),
) )
override fun getAllUsers(): List<User> { override fun getAllUsers(): List<User> {
return _users return _users
} }
override fun addUser(user: User): Boolean { override fun addUser(newUser: AddUserRequest): User
val result = _users.add(user) {
return result val user = User(id = _users.count() + 1,
email = newUser.email,
lastName = newUser.lastName,
firstName = newUser.firstName,
password = newUser.password
)
_users.add(user)
return user
} }
override fun removeUserById(id: Int): Boolean { override fun removeUserById(id: Int): Boolean {
@ -23,6 +32,12 @@ class UserRepositoryImpl : UserRepository {
} }
override fun updateUser(id: Int, newUser: User): Boolean { override fun updateUser(id: Int, newUser: User): Boolean {
TODO("Not yet implemented") val userIndex = _users.indexOfFirst { it -> it.id == id }
_users[userIndex] = _users[userIndex].copy(
firstName = newUser.firstName,
lastName = newUser.lastName,
email = newUser.email,
)
return true
} }
} }

10
src/domain/AuthUseCase.kt Normal file
View File

@ -0,0 +1,10 @@
package domain
import domain.models.AuthorizeRequest
import domain.models.RegistrationRequest
import domain.models.User
interface AuthUseCase {
fun authorize(authorizeRequest: AuthorizeRequest): User
fun registration(registrationRequest: RegistrationRequest) : User
}

View File

@ -0,0 +1,34 @@
package domain
import data.models.AddUserRequest
import data.repository.UserRepository
import domain.models.AuthorizeRequest
import domain.models.RegistrationRequest
import domain.models.User
class AuthUseCaseImpl(private val repository: UserRepository): AuthUseCase {
override fun authorize(authorizeRequest: AuthorizeRequest): User {
TODO("Not yet implemented")
}
override fun registration(registrationRequest: RegistrationRequest): User {
val name = registrationRequest.name.split(' ')
val user = AddUserRequest(
email = registrationRequest.email,
password = registrationRequest.password,
firstName = name[0],
lastName = name[1]
)
val result = repository.addUser(user)
return User(
id = result.id,
lastName = result.lastName,
firstName = result.firstName,
email = result.email,
password = result.password
)
}
}

View File

@ -0,0 +1,3 @@
package domain.models
data class AuthorizeRequest(val email:String, val password:String)

View File

@ -0,0 +1,5 @@
package domain.models
data class RegistrationRequest(val email:String,
val name:String,
val password:String)

15
src/domain/models/User.kt Normal file
View File

@ -0,0 +1,15 @@
package domain.models
data class User(val id: Int,
val firstName: String,
val lastName: String,
val email:String,
var password:String
)
{
fun resetPassword(password: String, confirmedPassword:String):Boolean{
if(password != confirmedPassword) return false
this.password = password
return true
}
}