-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Philipp Otto <[email protected]> Co-authored-by: Florian M <[email protected]>
- Loading branch information
1 parent
de45b21
commit 7eeb7f9
Showing
16 changed files
with
265 additions
and
53 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
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
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
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,36 @@ | ||
package controllers | ||
|
||
import com.mohiva.play.silhouette.api.Silhouette | ||
import com.scalableminds.util.tools.FoxImplicits | ||
import models.shortlinks.{ShortLink, ShortLinkDAO} | ||
import oxalis.security.{RandomIDGenerator, WkEnv} | ||
import play.api.libs.json.Json | ||
import play.api.mvc.{Action, AnyContent, PlayBodyParsers} | ||
import utils.{ObjectId, WkConf} | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.ExecutionContext | ||
|
||
class ShortLinkController @Inject()(shortLinkDAO: ShortLinkDAO, sil: Silhouette[WkEnv], wkConf: WkConf)( | ||
implicit ec: ExecutionContext, | ||
val bodyParsers: PlayBodyParsers) | ||
extends Controller | ||
with FoxImplicits { | ||
|
||
def create: Action[String] = sil.SecuredAction.async(validateJson[String]) { implicit request => | ||
val longLink = request.body | ||
val _id = ObjectId.generate | ||
val key = RandomIDGenerator.generateBlocking(12) | ||
for { | ||
_ <- bool2Fox(longLink.startsWith(wkConf.Http.uri)) ?~> "Could not generate short link: URI does not match" | ||
_ <- shortLinkDAO.insertOne(ShortLink(_id, key, longLink)) ?~> "create.failed" | ||
inserted <- shortLinkDAO.findOne(_id) | ||
} yield Ok(Json.toJson(inserted)) | ||
} | ||
|
||
def getByKey(key: String): Action[AnyContent] = Action.async { implicit request => | ||
for { | ||
shortLink <- shortLinkDAO.findOneByKey(key) | ||
} yield Ok(Json.toJson(shortLink)) | ||
} | ||
} |
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,55 @@ | ||
package models.shortlinks | ||
|
||
import com.scalableminds.util.tools.Fox | ||
import com.scalableminds.webknossos.schema.Tables | ||
import com.scalableminds.webknossos.schema.Tables.{Shortlinks, ShortlinksRow} | ||
import play.api.libs.json.{Json, OFormat} | ||
import slick.jdbc.PostgresProfile.api._ | ||
import slick.lifted.Rep | ||
import utils.{ObjectId, SQLClient, SQLDAO} | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.ExecutionContext | ||
|
||
case class ShortLink(_id: ObjectId, key: String, longLink: String) | ||
|
||
object ShortLink { | ||
implicit val jsonFormat: OFormat[ShortLink] = Json.format[ShortLink] | ||
} | ||
|
||
class ShortLinkDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext) | ||
extends SQLDAO[ShortLink, ShortlinksRow, Shortlinks](sqlClient) { | ||
val collection = Shortlinks | ||
|
||
def idColumn(x: Shortlinks): Rep[String] = x._Id | ||
|
||
override def isDeletedColumn(x: Tables.Shortlinks): Rep[Boolean] = false | ||
|
||
def parse(r: ShortlinksRow): Fox[ShortLink] = | ||
Fox.successful( | ||
ShortLink( | ||
ObjectId(r._Id), | ||
r.key, | ||
r.longlink | ||
) | ||
) | ||
|
||
def insertOne(sl: ShortLink): Fox[Unit] = | ||
for { | ||
_ <- run(sqlu"""insert into webknossos.shortLinks(_id, key, longlink) | ||
values(${sl._id}, ${sl.key}, ${sl.longLink})""") | ||
} yield () | ||
|
||
def findOne(id: String): Fox[ShortLink] = | ||
for { | ||
r <- run(sql"select #$columns from webknossos.shortLinks where id = $id".as[ShortlinksRow]) | ||
parsed <- parseFirst(r, id) | ||
} yield parsed | ||
|
||
def findOneByKey(key: String): Fox[ShortLink] = | ||
for { | ||
r <- run(sql"select #$columns from webknossos.shortLinks where key = $key".as[ShortlinksRow]) | ||
parsed <- parseFirst(r, key) | ||
} yield parsed | ||
|
||
} |
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,14 @@ | ||
START TRANSACTION; | ||
|
||
CREATE TABLE webknossos.shortLinks( | ||
_id CHAR(24) PRIMARY KEY DEFAULT '', | ||
key CHAR(16) NOT NULL UNIQUE, | ||
longLink Text NOT NULL | ||
); | ||
|
||
CREATE INDEX ON webknossos.shortLinks(key); | ||
|
||
UPDATE webknossos.releaseInformation | ||
SET schemaVersion = 88; | ||
|
||
COMMIT TRANSACTION; |
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,7 @@ | ||
START TRANSACTION; | ||
|
||
DROP TABLE webknossos.shortLinks; | ||
|
||
UPDATE webknossos.releaseInformation SET schemaVersion = 87; | ||
|
||
COMMIT TRANSACTION; |
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
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
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
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
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
Oops, something went wrong.