-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61d4c33
commit f3ced70
Showing
16 changed files
with
220 additions
and
224 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
60 changes: 60 additions & 0 deletions
60
app/com/mehmetakiftutuncu/muezzinapi/controllers/NukeController.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,60 @@ | ||
package com.mehmetakiftutuncu.muezzinapi.controllers | ||
|
||
import java.time.Duration | ||
import javax.inject.{Inject, Singleton} | ||
|
||
import com.github.mehmetakiftutuncu.errors.{CommonError, Errors} | ||
import com.google.firebase.database.DatabaseReference.CompletionListener | ||
import com.google.firebase.database.{DatabaseError, DatabaseReference} | ||
import com.mehmetakiftutuncu.muezzinapi.data.FirebaseRealtimeDatabase._ | ||
import com.mehmetakiftutuncu.muezzinapi.data.AbstractFirebaseRealtimeDatabase | ||
import com.mehmetakiftutuncu.muezzinapi.utilities.{AbstractConf, ControllerBase, Log, Timer} | ||
import play.api.libs.json.Json | ||
import play.api.mvc.{Action, AnyContent} | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Promise | ||
|
||
@Singleton | ||
class NukeController @Inject()(Conf: AbstractConf, | ||
FirebaseRealtimeDatabase: AbstractFirebaseRealtimeDatabase) extends ControllerBase { | ||
private val whatCanBeNuked: Set[String] = Set("countries", "prayerTimes") | ||
|
||
def nuke(target: String, code: String): Action[AnyContent] = Action.async { | ||
if (!whatCanBeNuked.contains(target)) { | ||
futureFailWithErrors("You don't know what you're nuking!", Errors(CommonError.invalidData.reason("Invalid nuke target!").data(target))) | ||
} else if (!Conf.getString("muezzinApi.nuke.code").contains(code)) { | ||
futureFailWithErrors("You don't know nuke codes!", Errors(CommonError.authorization.reason("Invalid nuke code!"))) | ||
} else { | ||
Timer.start(s"nuke.$target") | ||
|
||
val reference: DatabaseReference = FirebaseRealtimeDatabase.root / target | ||
|
||
val promise: Promise[Errors] = Promise[Errors]() | ||
|
||
reference.removeValue(new CompletionListener { | ||
override def onComplete(databaseError: DatabaseError, databaseReference: DatabaseReference): Unit = { | ||
val errors: Errors = if (databaseError != null) { | ||
Errors(CommonError.database.reason(databaseError.toException.getMessage)) | ||
} else { | ||
Errors.empty | ||
} | ||
|
||
promise.success(errors) | ||
} | ||
}) | ||
|
||
promise.future.map { errors: Errors => | ||
val duration: Duration = Timer.stop(s"nuke.$target") | ||
|
||
if (errors.hasErrors) { | ||
failWithErrors(s"""Failed to nuke "$target"!""", errors) | ||
} else { | ||
Log.debug(s"""Successfully nuked "$target" in ${duration.toMillis} ms.""") | ||
|
||
success(Json.obj("target" -> "destroyed")) | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package com.mehmetakiftutuncu.muezzinapi.models | ||
|
||
case class Place(countryId: Int, cityId: Int, districtId: Option[Int]) { | ||
case class Place(countryId: Int, cityId: Option[Int], districtId: Option[Int]) { | ||
def toForm: Map[String, Seq[String]] = { | ||
Map( | ||
"Country" -> Seq(countryId.toString), | ||
"State" -> Seq(cityId.toString), | ||
"period" -> Seq("Aylik") | ||
) ++ districtId.map(id => Map("City" -> Seq(id.toString))).getOrElse(Map.empty[String, Seq[String]]) | ||
val countryMap: Map[String, Seq[String]] = Map("ulkeId" -> Seq(countryId.toString)) | ||
val cityMap: Map[String, Seq[String]] = cityId.map(id => Map("ilId" -> Seq(id.toString))).getOrElse(Map.empty) | ||
val districtMap: Map[String, Seq[String]] = districtId.map(id => Map("ilceId" -> Seq(id.toString))).getOrElse(Map.empty) | ||
|
||
countryMap ++ cityMap ++ districtMap | ||
} | ||
|
||
def toLog: String = s"""country "$countryId", city "$cityId" and district "${districtId.map(_.toString).getOrElse("")}"""" | ||
def toLog: String = s"""country "$countryId", city "${cityId.map(_.toString).getOrElse("")}" and district "${districtId.map(_.toString).getOrElse("")}"""" | ||
|
||
def toPath: String = s"country/$countryId${cityId.map("/city/" + _).getOrElse("")}${districtId.map("/district/" + _).getOrElse("")}" | ||
|
||
def toPath: String = s"country/$countryId/city/$cityId${districtId.map("/district/" + _.toString).getOrElse("")}" | ||
def toKey: String = s"$countryId${cityId.map("." + _).getOrElse("")}${districtId.map("." + _).getOrElse("")}" | ||
} |
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.