Refactor database API to use for-comprehensions.
This commit is contained in:
parent
86b247c481
commit
0109b2d87a
1 changed files with 56 additions and 94 deletions
|
|
@ -35,113 +35,75 @@ class UACAPI(
|
||||||
|
|
||||||
def setup: DBIO[Unit] =
|
def setup: DBIO[Unit] =
|
||||||
DBIO.seq(
|
DBIO.seq(
|
||||||
(queries.UACBase.schema ++ queries.tokenScopeBase.schema).createIfNotExists
|
(
|
||||||
|
queries.UACBase.schema
|
||||||
|
++ queries.tokenScopeBase.schema
|
||||||
|
).createIfNotExists
|
||||||
)
|
)
|
||||||
|
|
||||||
def create(cred: UserAuthenticationCredential)(implicit ec: ExecutionContext): DBIO[Option[Int]] =
|
/**
|
||||||
queries.UACBase.returning(queries.UACBase.map(_.id)).+=(
|
* Construct a query to add a new UserAuthenticationCredential to the
|
||||||
queries.schema.RawUAC(
|
* database.
|
||||||
0,
|
*
|
||||||
cred.userId,
|
* @param cred Credential to insert.
|
||||||
cred.accessToken,
|
* @return A query object. Pass it to `exec.execute()` to run it.
|
||||||
cred.refreshToken,
|
*/
|
||||||
cred.expires
|
def create(
|
||||||
)
|
cred: UserAuthenticationCredential
|
||||||
).flatMap(uacId =>
|
)(
|
||||||
queries.tokenScopeBase.++=(
|
implicit ec: ExecutionContext
|
||||||
|
): DBIO[Option[Int]] =
|
||||||
|
for
|
||||||
|
// Insert base UAC
|
||||||
|
base <- queries.UACBase.returning(queries.UACBase.map(_.id)) +=
|
||||||
|
queries.schema.RawUAC(
|
||||||
|
0,
|
||||||
|
cred.userId,
|
||||||
|
cred.accessToken,
|
||||||
|
cred.refreshToken,
|
||||||
|
cred.expires
|
||||||
|
)
|
||||||
|
|
||||||
|
// Insert scopes for UAC
|
||||||
|
scopes <- queries.tokenScopeBase ++=
|
||||||
cred.scopes.map(scope =>
|
cred.scopes.map(scope =>
|
||||||
queries.schema.UACTokenScope(
|
queries.schema.UACTokenScope(
|
||||||
uacId,
|
base,
|
||||||
scope.uid
|
scope.uid
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
yield scopes
|
||||||
)
|
|
||||||
|
|
||||||
def retrieve(user: TwitchUID)(implicit ec: ExecutionContext): DBIO[UserAuthenticationCredential] =
|
def retrieve(
|
||||||
// Construct query for base UAC data
|
user: TwitchUID
|
||||||
queries.UACBase.filter(
|
)(
|
||||||
_.userId === user
|
implicit ec: ExecutionContext
|
||||||
).take(1)
|
): DBIO[UserAuthenticationCredential] =
|
||||||
.result.headOption // Take only the first UAC that matches (there should never be more than one)
|
for
|
||||||
.flatMap(res => // With the result of that query:
|
// Query the base UAC
|
||||||
res match
|
uac <- queries.UACBase.filter(
|
||||||
case None => slick.dbio.FailureAction(NotFoundException()) // Don't attempt to retrieve scopes if there's no matching UAC
|
_.userId === user
|
||||||
|
).take(1).result.headOption
|
||||||
|
|
||||||
|
// Query the scopes (if the base UAC returns)
|
||||||
|
scopes <- uac match
|
||||||
|
case None => slick.dbio.FailureAction(NotFoundException())
|
||||||
case Some(uac) =>
|
case Some(uac) =>
|
||||||
// Construct a query to retrieve relevant scopes
|
queries.tokenScopeBase.filter(scope =>
|
||||||
queries.tokenScopeBase.filter( scope =>
|
|
||||||
scope.tokenId === uac.id
|
scope.tokenId === uac.id
|
||||||
).result.map(res =>
|
).result
|
||||||
val convertedScopes = res.map(scope =>
|
|
||||||
// Convert UIDs to TokenScope enums
|
|
||||||
TokenScope.fromUid(scope.scopeId)
|
|
||||||
).filter(mappedScope =>
|
|
||||||
// Filter any UIDs that didn't match TokenScopes.
|
|
||||||
mappedScope match
|
|
||||||
case None => false
|
|
||||||
case _ => true
|
|
||||||
).map(convertedScope =>
|
|
||||||
// Strip the Options.
|
|
||||||
convertedScope.get
|
|
||||||
)
|
|
||||||
|
|
||||||
UserAuthenticationCredential(
|
yield UserAuthenticationCredential(
|
||||||
uac.userId,
|
uac.get.userId,
|
||||||
uac.accessToken,
|
uac.get.accessToken,
|
||||||
uac.refreshToken,
|
uac.get.refreshToken,
|
||||||
uac.expires,
|
uac.get.expires,
|
||||||
convertedScopes.toList
|
// n.b. - `flatten` removes scope IDs that don't map properly.
|
||||||
)
|
scopes.map(scope =>
|
||||||
)
|
TokenScope.fromUid(scope.scopeId)
|
||||||
|
).flatten.toList
|
||||||
)
|
)
|
||||||
|
|
||||||
// val action = database.run(query).flatMap( res =>
|
|
||||||
// res match
|
|
||||||
// case None => Future{None}
|
|
||||||
// case Some(uac) =>
|
|
||||||
// // Construct query for
|
|
||||||
// val scopesQuery = queries.tokenScopeBase.filter( scope =>
|
|
||||||
// scope.tokenId === uac.id
|
|
||||||
// ).result.map(res =>
|
|
||||||
// res.map(scope =>
|
|
||||||
// // Convert UIDs to TokenScope enums
|
|
||||||
// TokenScope.fromUid(scope.scopeId)
|
|
||||||
// ).filter(mappedScope =>
|
|
||||||
// // Filter any UIDs that didn't match TokenScopes.
|
|
||||||
// mappedScope match
|
|
||||||
// case None => false
|
|
||||||
// case _ => true
|
|
||||||
// ).map(convertedScope =>
|
|
||||||
// // Strip the Options.
|
|
||||||
// convertedScope.get
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
|
|
||||||
// database.run(scopesQuery).map(res =>
|
|
||||||
// UserAuthenticationCredential(
|
|
||||||
// uac.userId,
|
|
||||||
// uac.accessToken,
|
|
||||||
// uac.refreshToken,
|
|
||||||
// uac.expires,
|
|
||||||
// res.toList
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
|
|
||||||
// .map((res) =>
|
|
||||||
// val queryResult: Option[queries.schema.RawUAC] =
|
|
||||||
// if res.length > 0 then Some(res[0]) else None
|
|
||||||
|
|
||||||
// if queryResult == None then
|
|
||||||
// throw Exception
|
|
||||||
|
|
||||||
// val scopesQuery = queries.tokenScopeBase.filter(
|
|
||||||
// _.tokenId === queryResult.accessToken
|
|
||||||
// ).take(1).result
|
|
||||||
// )
|
|
||||||
|
|
||||||
// action
|
|
||||||
|
|
||||||
def delete(user: TwitchUID): Future[Unit] = ???
|
def delete(user: TwitchUID): Future[Unit] = ???
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue