Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

#14 자바코드 코틀린 전환 #15 코드리뷰 피드백 반영 #16

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kr.flab.wiki.core.common.exception

open class ValidationException(msg : String) : Exception(msg)
13 changes: 13 additions & 0 deletions app-core/src/main/kotlin/kr/flab/wiki/core/login/LoginUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kr.flab.wiki.core.login

import java.util.regex.Pattern

object LoginUtils {
// https://atin.tistory.com/425
private const val REGEX_EMAIL = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@(?:\\w+\\.)+\\w+$"
private val emailPattern: Pattern = Pattern.compile(REGEX_EMAIL)

fun isValidEmail(email: String?): Boolean {
return emailPattern.matcher(email).matches()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.flab.wiki.core.login.business

import kr.flab.wiki.core.login.persistence.User

interface UserLoginService {
fun login(user : User) : User?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.flab.wiki.core.login.business

import kr.flab.wiki.core.login.LoginUtils
import kr.flab.wiki.core.login.exception.UserValidationException
import kr.flab.wiki.core.login.persistence.User
import kr.flab.wiki.core.login.repository.SessionRepository
import kr.flab.wiki.core.login.repository.UserLoginRepository

class UserLoginServiceImpl(
private val userLoginRepository: UserLoginRepository,
private val sessionRepository: SessionRepository
) : UserLoginService{
override fun login(user: User) : User? {

val email : String = user.email
val password : String = user.password

val user : User? = userLoginRepository.findUserWithIdAndPassword(email, password)

//매치되는 회원이 없을 경우
user ?: throw UserValidationException("There's No Matched Member!")

//세션 저장
if (sessionRepository.setAttribute("userName", email) == null) {
throw UserValidationException("Session creation failed!")
}

return user

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kr.flab.wiki.core.login.exception

import kr.flab.wiki.core.common.exception.ValidationException

class UserValidationException(msg : String) : ValidationException(msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.flab.wiki.core.login.persistence

interface User {
//이름
val email : String
//비밀번호
val password : String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.flab.wiki.core.login.persistence

import kr.flab.wiki.core.login.LoginUtils
import kr.flab.wiki.core.login.exception.UserValidationException

class UserEntity : User {
override var email : String = ""
set(value) {
if(value.isEmpty()){
throw UserValidationException("Email is Empty!")
}
if (!LoginUtils.isValidEmail(value)) {
throw UserValidationException("Not Email Type!")
}
field = value
}
override var password : String = ""
set(value) {
if (value.isEmpty()) {
throw UserValidationException("Password is Empty!")
}
field = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.flab.wiki.core.login.repository

interface SessionRepository {
/**
* @설명 : 세션에 값을 저장하고, username 을 반환한다.
* @return : username : String
*/
fun setAttribute(key : String, username : String) : String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.flab.wiki.core.login.repository

class SessionRepositoryImpl : SessionRepository {
override fun setAttribute(key: String, username: String): String? {
return username
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.flab.wiki.core.login.repository

import kr.flab.wiki.core.login.persistence.User

interface UserLoginRepository {
fun isUserExists(email : String, password : String) : Boolean
fun findUserWithIdAndPassword(email : String, password : String) : User?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.flab.wiki.core.login.repository

import kr.flab.wiki.core.login.persistence.User
import kr.flab.wiki.core.login.persistence.UserEntity

class UserLoginRepositoryImpl : UserLoginRepository {
private val database : HashMap<String, String> = HashMap()

override fun isUserExists(email: String, password: String): Boolean {
val originPassword : String = database[email] ?: return false
return originPassword == password
}

override fun findUserWithIdAndPassword(email: String, password: String): User? {
if(isUserExists(email, password)){
return UserEntity().apply {
this.email = email
this.password = password
}
}
return null
}
}
Loading