|
| 1 | +package com.zengularity.querymonad.examples.todoapp.controller |
| 2 | + |
| 3 | +import java.nio.charset.Charset |
| 4 | +import java.util.{Base64, UUID} |
| 5 | + |
| 6 | +import scala.concurrent.{ExecutionContext, Future} |
| 7 | + |
| 8 | +import cats.instances.either._ |
| 9 | +import play.api.mvc._ |
| 10 | +import play.api.libs.json.Json |
| 11 | + |
| 12 | +import com.zengularity.querymonad.examples.todoapp.controller.model.AddUserPayload |
| 13 | +import com.zengularity.querymonad.examples.todoapp.model.{Credential, User} |
| 14 | +import com.zengularity.querymonad.examples.todoapp.store.{ |
| 15 | + CredentialStore, |
| 16 | + UserStore |
| 17 | +} |
| 18 | +import com.zengularity.querymonad.module.sql.{SqlQueryRunner, SqlQueryT} |
| 19 | + |
| 20 | +class UserController( |
| 21 | + runner: SqlQueryRunner, |
| 22 | + store: UserStore, |
| 23 | + credentialStore: CredentialStore, |
| 24 | + cc: ControllerComponents |
| 25 | +)(implicit val ec: ExecutionContext) |
| 26 | + extends AbstractController(cc) |
| 27 | + with Authentication { |
| 28 | + |
| 29 | + type ErrorOrResult[A] = Either[String, A] |
| 30 | + |
| 31 | + def createUser: Action[AddUserPayload] = |
| 32 | + Action(parse.json[AddUserPayload]).async { implicit request => |
| 33 | + val payload = request.body |
| 34 | + val query = for { |
| 35 | + _ <- SqlQueryT.fromQuery[ErrorOrResult, Unit]( |
| 36 | + store.getByLogin(payload.login).map { |
| 37 | + case Some(_) => Left("User already exists") |
| 38 | + case None => Right(()) |
| 39 | + } |
| 40 | + ) |
| 41 | + |
| 42 | + user = AddUserPayload.toModel(payload)(UUID.randomUUID()) |
| 43 | + credential = AddUserPayload.toCredential(payload) |
| 44 | + |
| 45 | + _ <- SqlQueryT.liftQuery[ErrorOrResult, Unit]( |
| 46 | + credentialStore.saveCredential(credential) |
| 47 | + ) |
| 48 | + _ <- SqlQueryT.liftQuery[ErrorOrResult, Unit](store.createUser(user)) |
| 49 | + } yield () |
| 50 | + |
| 51 | + runner(query).map { |
| 52 | + case Right(_) => NoContent |
| 53 | + case Left(description) => BadRequest(description) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + def getUser(userId: UUID): Action[AnyContent] = ConnectedAction.async { |
| 58 | + request => |
| 59 | + if (request.userInfo.id == userId) |
| 60 | + runner(store.getUser(userId)).map { |
| 61 | + case Some(user) => Ok(Json.toJson(user)) |
| 62 | + case None => NotFound("The user doesn't exist") |
| 63 | + } else |
| 64 | + Future.successful(NotFound("Cannot operate this action")) |
| 65 | + } |
| 66 | + |
| 67 | + def deleteUser(userId: UUID): Action[AnyContent] = ConnectedAction.async { |
| 68 | + request => |
| 69 | + val userInfo = request.userInfo |
| 70 | + if (userInfo.id == userId) { |
| 71 | + val query = for { |
| 72 | + _ <- credentialStore.deleteCredentials(userInfo.login) |
| 73 | + _ <- store.deleteUser(userId) |
| 74 | + } yield () |
| 75 | + runner(query).map(_ => NoContent.withNewSession) |
| 76 | + } else |
| 77 | + Future.successful(BadRequest("Cannot operate this action")) |
| 78 | + } |
| 79 | + |
| 80 | + def login: Action[AnyContent] = Action.async { implicit request => |
| 81 | + val authHeaderOpt = request.headers |
| 82 | + .get("Authorization") |
| 83 | + .map(_.substring("Basic".length()).trim()) |
| 84 | + |
| 85 | + val query = for { |
| 86 | + credential <- SqlQueryT.liftF[ErrorOrResult, Credential]( |
| 87 | + authHeaderOpt |
| 88 | + .map { encoded => |
| 89 | + val decoded = Base64.getDecoder().decode(encoded) |
| 90 | + val authStr = new String(decoded, Charset.forName("UTF-8")) |
| 91 | + authStr.split(':').toList |
| 92 | + } |
| 93 | + .collect { |
| 94 | + case login :: password :: _ => Credential(login, password) |
| 95 | + } |
| 96 | + .toRight("Missing credentials") |
| 97 | + ) |
| 98 | + |
| 99 | + exists <- SqlQueryT.liftQuery[ErrorOrResult, Boolean]( |
| 100 | + credentialStore.check(credential) |
| 101 | + ) |
| 102 | + |
| 103 | + user <- { |
| 104 | + if (exists) |
| 105 | + SqlQueryT.fromQuery[ErrorOrResult, User]( |
| 106 | + store |
| 107 | + .getByLogin(credential.login) |
| 108 | + .map(_.toRight("The user doesn't exist")) |
| 109 | + ) |
| 110 | + else |
| 111 | + SqlQueryT.liftF[ErrorOrResult, User](Left("Wrong credentials")) |
| 112 | + } |
| 113 | + } yield user |
| 114 | + |
| 115 | + runner(query).map { |
| 116 | + case Right(user) => |
| 117 | + NoContent.withSession("id" -> user.id.toString, "login" -> user.login) |
| 118 | + case Left(description) => BadRequest(description).withNewSession |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + def logout: Action[AnyContent] = ConnectedAction { |
| 123 | + NoContent.withNewSession |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments