Skip to content

Commit

Permalink
backend for: admin should be able to remove images #82
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Nov 26, 2016
1 parent 94b54b3 commit a9148b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
26 changes: 15 additions & 11 deletions app/controllers/Gallery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object Gallery extends Controller with Secured with Instrumented {
val regions = Set(region).filter(_ != "all")

val userDetails = module == "filelist"
val query = getQuery(asUserId, rate, round, Some(pager), userDetails, rated, regions)
val query = getQuery(asUserId, rate, round.id, Some(pager), userDetails, rated, regions)
pager.setCount(query.count())

val files = filesByUserId(query, pager, userDetails)
Expand Down Expand Up @@ -130,11 +130,11 @@ object Gallery extends Controller with Secured with Instrumented {
def getSortedImages(
asUserId: Long,
rate: Option[Int],
round: Round,
roundId: Option[Long],
module: String,
pager: Pager = Pager.pageOffset(1)): Seq[ImageWithRating] = {
val userDetails = module == "filelist"
filesByUserId(getQuery(asUserId, rate, round, Some(pager), userDetails), pager, userDetails)
filesByUserId(getQuery(asUserId, rate, roundId, Some(pager), userDetails), pager, userDetails)
}

def isNotAuthorized(user: User, maybeRound: Option[Round], roundContest: Long, rounds: Seq[Round]): Boolean = {
Expand Down Expand Up @@ -177,7 +177,7 @@ object Gallery extends Controller with Secured with Instrumented {
def getQuery(
userId: Long,
rate: Option[Int],
round: Round,
roundId: Option[Long],
pager: Option[Pager] = None,
userDetails: Boolean = false,
rated: Option[Boolean] = None,
Expand All @@ -188,7 +188,7 @@ object Gallery extends Controller with Secured with Instrumented {
userId = userIdOpt,
rate = rate,
rated = rated,
roundId = round.id,
roundId = roundId,
regions = regions,
order = Map("rate" -> -1, "s.page_id" -> 1),
grouped = userIdOpt.isEmpty && !userDetails,
Expand Down Expand Up @@ -238,10 +238,14 @@ object Gallery extends Controller with Secured with Instrumented {
}
}

// def removeImage(imageId: Long, roundId: Long) = withAuth(RoundPermission(User.ADMIN_ROLES, roundId)) {
// SelectionJdbc.removeImage(imageId, roundId)
// }

def removeImage(pageId: Long, roundId: Long, region: String = "all", rate: Option[Int], module: String) =
withAuth(RoundPermission(User.ADMIN_ROLES, roundId)) {
user =>
implicit request =>
SelectionJdbc.removeImage(pageId, roundId)
val round = RoundJdbc.find(roundId).get
checkLargeIndex(user, rate, pageId, region, round, module)
}

def checkLargeIndex(asUser: User,
rate: Option[Int],
Expand All @@ -250,7 +254,7 @@ object Gallery extends Controller with Secured with Instrumented {
round: Round,
module: String): Result = {

val query = getQuery(asUser.id.get, rate, round)
val query = getQuery(asUser.id.get, rate, round.id)

val rank = query.imageRank(pageId)

Expand Down Expand Up @@ -298,7 +302,7 @@ object Gallery extends Controller with Secured with Instrumented {
val maybeRound = if (roundId == 0) RoundJdbc.current(user) else RoundJdbc.find(roundId)
val round = maybeRound.get

val query = getQuery(asUserId, rate, round, regions = Set(region).filter(_ != "all"))
val query = getQuery(asUserId, rate, round.id, regions = Set(region).filter(_ != "all"))
val rank = query.imageRank(pageId)
val offset = Math.max(0, rank - 3)
val files = query.copy(limit = Some(Limit(Some(5), Some(offset)))).list()
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/Rounds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ object Rounds extends Controller with Secured {
stat
}

def removeImage(imageId: Long, roundId: Long) = {

}

val imagesForm = Form("images" -> optional(text))

val selectRoundForm = Form("currentRound" -> optional(text))
Expand Down
7 changes: 6 additions & 1 deletion app/db/scalikejdbc/SelectionJdbc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,13 @@ object SelectionJdbc extends SQLSyntaxSupport[Selection] with SelectionDao {
.append(isNotDeleted)
}.map(_.int(1)).single().apply().get

def destroyAll(pageId: Long)(implicit session: DBSession = autoSession): Unit = withSQL {
def destroyAll(pageId: Long): Unit = withSQL {
update(SelectionJdbc).set(column.deletedAt -> DateTime.now).where.eq(column.pageId, pageId)
}.update.apply()

def removeImage(pageId: Long, roundId: Long): Unit = withSQL {
delete.from(SelectionJdbc as s).where
.eq(s.pageId, pageId).and
.eq(s.round, roundId)
}.update().apply()
}

0 comments on commit a9148b5

Please sign in to comment.