-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
14 changed files
with
213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/interfaces/api/user/edit/email/EditUserEmailController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package interfaces.api.user.edit.email | ||
|
||
import dev.tchiba.auth.usecase.user.edit.email.{ChangeEmailOutput, ChangeEmailUseCase} | ||
import interfaces.api.SdmtApiController | ||
import interfaces.api.user.json.UserResponse | ||
import interfaces.security.UserAction | ||
import play.api.mvc.{Action, ControllerComponents} | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.ExecutionContext | ||
|
||
class EditUserEmailController @Inject() ( | ||
cc: ControllerComponents, | ||
userAction: UserAction, | ||
changeEmailUseCase: ChangeEmailUseCase | ||
)(implicit ec: ExecutionContext) | ||
extends SdmtApiController(cc) { | ||
|
||
def action(): Action[EditUserEmailRequest] = userAction(EditUserEmailRequest.validateJson) { implicit request => | ||
val input = request.body.get(request.user.id) | ||
changeEmailUseCase.handle(input) match { | ||
case ChangeEmailOutput.Success(user) => | ||
val response = UserResponse(user) | ||
Ok(response.json) | ||
case ChangeEmailOutput.NotFoundUser(userId) => | ||
notFound( | ||
code = "sdmt.user.edit.email.notFoundUser", | ||
message = "not found target user.", | ||
params = "userId" -> userId.value | ||
) | ||
case ChangeEmailOutput.EmailIsNotUnique(email) => | ||
badRequest( | ||
code = "sdmt.user.edit.email.isNotUnique", | ||
message = "input email address is not unique.", | ||
params = "email" -> email.value | ||
) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/interfaces/api/user/edit/email/EditUserEmailRequest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package interfaces.api.user.edit.email | ||
|
||
import cats.data.ValidatedNel | ||
import dev.tchiba.auth.core.user.UserId | ||
import dev.tchiba.auth.usecase.user.edit.email.ChangeEmailInput | ||
import dev.tchiba.sub.email.EmailAddress | ||
import interfaces.json.request.play.{PlayJsonRequest, PlayJsonRequestCompanion} | ||
import play.api.libs.json.{Json, OFormat} | ||
|
||
case class EditUserEmailRequest(email: String) extends PlayJsonRequest { | ||
|
||
override type VM = UserId => ChangeEmailInput | ||
|
||
override def validateParameters: ValidatedNel[(String, String), VM] = | ||
EmailAddress.validate(email).toValidated("email").map(e => ChangeEmailInput(_, e)) | ||
} | ||
|
||
object EditUserEmailRequest extends PlayJsonRequestCompanion[EditUserEmailRequest] { | ||
implicit val jsonFormat: OFormat[EditUserEmailRequest] = Json.format[EditUserEmailRequest] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...scala/dev/tchiba/auth/application/interactors/user/edit/email/ChangeEmailInteractor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.tchiba.auth.application.interactors.user.edit.email | ||
|
||
import dev.tchiba.auth.core.user.UserRepository | ||
import dev.tchiba.auth.core.user.specs.email.unique.UserEmailUniqueChecker | ||
import dev.tchiba.auth.usecase.user.edit.email.{ChangeEmailInput, ChangeEmailOutput, ChangeEmailUseCase} | ||
|
||
import javax.inject.Inject | ||
|
||
class ChangeEmailInteractor @Inject() ( | ||
userEmailUniqueChecker: UserEmailUniqueChecker, | ||
userRepository: UserRepository | ||
) extends ChangeEmailUseCase { | ||
|
||
import ChangeEmailOutput._ | ||
override def handle(input: ChangeEmailInput): ChangeEmailOutput = { | ||
val result = for { | ||
user <- userRepository.findById(input.userId).toRight(NotFoundUser(input.userId)) | ||
emailIsUniqueMessage <- userEmailUniqueChecker.check(input.email).left.map { _ => EmailIsNotUnique(input.email) } | ||
} yield { | ||
val emailUpdatedUser = user.changeEmail(emailIsUniqueMessage) | ||
userRepository.update(emailUpdatedUser) | ||
Success(emailUpdatedUser) | ||
} | ||
|
||
result.unwrap | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../src/main/scala/dev/tchiba/auth/core/user/specs/email/unique/UserEmailUniqueChecker.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.tchiba.auth.core.user.specs.email.unique | ||
|
||
import dev.tchiba.arch.ddd.DomainService | ||
import dev.tchiba.sub.email.EmailAddress | ||
|
||
trait UserEmailUniqueChecker extends DomainService { | ||
import UserEmailUniqueChecker._ | ||
|
||
/** | ||
* メールアドレスがユニークなものであるか調べる | ||
* | ||
* @param email | ||
* 調べるメールアドレス | ||
* @return | ||
* 引数のメールアドレスがまだDBに同じものが存在せず、ユニークである時、[[Right]]で[[EmailIsUnique]]メッセージを返す | ||
*/ | ||
def check(email: EmailAddress): Either[Because, EmailIsUnique] = | ||
Either.cond(isNotExist(email), new EmailIsUnique(email), EmailAlreadyExist) | ||
|
||
/** | ||
* 引数のメールアドレスを持つユーザーがまだDB上に存在しない | ||
* @param email | ||
* メールアドレス | ||
* @return | ||
* 存在する時: `false`, 存在しない時: `true` | ||
*/ | ||
protected def isNotExist(email: EmailAddress): Boolean | ||
} | ||
|
||
object UserEmailUniqueChecker { | ||
|
||
final class EmailIsUnique private[unique] (val email: EmailAddress) | ||
|
||
sealed trait Because | ||
|
||
case object EmailAlreadyExist extends Because | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...scala/dev/tchiba/auth/infra/core/user/specs/email/unique/JdbcUserEmailUniqueChecker.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.tchiba.auth.infra.core.user.specs.email.unique | ||
|
||
import dev.tchiba.auth.core.user.specs.email.unique.UserEmailUniqueChecker | ||
import dev.tchiba.sdmt.infra.scalikejdbc.Users | ||
import dev.tchiba.sub.email.EmailAddress | ||
import scalikejdbc._ | ||
class JdbcUserEmailUniqueChecker extends UserEmailUniqueChecker { | ||
|
||
private val u = Users.u | ||
override protected def isNotExist(email: EmailAddress): Boolean = DB readOnly { implicit session => | ||
withSQL { | ||
select(sqls.count) | ||
.from(Users.as(u)) | ||
.where | ||
.eq(u.emailAddress, email.value) | ||
}.map(rs => rs.int(1)).single().apply().get == 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
auth-usecase/src/main/scala/dev/tchiba/auth/usecase/user/edit/email/ChangeEmailInput.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.tchiba.auth.usecase.user.edit.email | ||
|
||
import dev.tchiba.arch.usecase.Input | ||
import dev.tchiba.auth.core.user.UserId | ||
import dev.tchiba.sub.email.EmailAddress | ||
|
||
/** | ||
* メールアドレス変更ユースケースの入力 | ||
* | ||
* @param userId | ||
* 変更対象のユーザーID | ||
* @param email | ||
* 置き換えるメールアドレス | ||
*/ | ||
final case class ChangeEmailInput(userId: UserId, email: EmailAddress) extends Input[ChangeEmailOutput] |
16 changes: 16 additions & 0 deletions
16
auth-usecase/src/main/scala/dev/tchiba/auth/usecase/user/edit/email/ChangeEmailOutput.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.tchiba.auth.usecase.user.edit.email | ||
|
||
import dev.tchiba.arch.usecase.Output | ||
import dev.tchiba.auth.core.user.{User, UserId} | ||
import dev.tchiba.sub.email.EmailAddress | ||
|
||
sealed abstract class ChangeEmailOutput extends Output | ||
|
||
object ChangeEmailOutput { | ||
|
||
final case class Success(user: User) extends ChangeEmailOutput | ||
|
||
final case class NotFoundUser(userId: UserId) extends ChangeEmailOutput | ||
|
||
final case class EmailIsNotUnique(email: EmailAddress) extends ChangeEmailOutput | ||
} |
8 changes: 8 additions & 0 deletions
8
auth-usecase/src/main/scala/dev/tchiba/auth/usecase/user/edit/email/ChangeEmailUseCase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dev.tchiba.auth.usecase.user.edit.email | ||
|
||
import dev.tchiba.arch.usecase.UseCase | ||
|
||
/** | ||
* ユーザーのメールアドレス変更ユースケース | ||
*/ | ||
abstract class ChangeEmailUseCase extends UseCase[ChangeEmailInput, ChangeEmailOutput] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters