-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
6 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/org/sailcbi/APIServer/Api/Endpoints/Dto/Sunset/Get.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,30 @@ | ||
package org.sailcbi.APIServer.Api.Endpoints.Dto.Sunset | ||
|
||
import play.api.libs.json.{JsValue, Json} | ||
|
||
/** | ||
* !!!!!!!!!!!! | ||
* This file is AUTO-GENERATED by cbidb-schema | ||
* Do not manually alter this file, or your changes will be lost | ||
* !!!!!!!!!!!! | ||
*/ | ||
case class DtoSunsetGetResponseSuccess ( | ||
forDate: String, | ||
twilightStart: String, | ||
sunrise: String, | ||
sunset: String, | ||
twilightEnd: String, | ||
dayLengthSeconds: Int, | ||
sonarNoon: String, | ||
nauticalTwilightStart: String, | ||
nauticalTwilightEnd: String, | ||
astronomicalTwilightStart: String, | ||
astronomicalTwilightEnd: String, | ||
) | ||
|
||
object DtoSunsetGetResponseSuccess { | ||
implicit val format = Json.format[DtoSunsetGetResponseSuccess] | ||
def apply(v: JsValue): DtoSunsetGetResponseSuccess | ||
= v.as[DtoSunsetGetResponseSuccess] | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.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,24 @@ | ||
package org.sailcbi.APIServer.Api.Endpoints.Public | ||
|
||
import com.coleji.neptune.Core.{ParsedRequest, PermissionsAuthority} | ||
import org.sailcbi.APIServer.Entities.EntityDefinitions.SunsetTime | ||
import org.sailcbi.APIServer.Entities.cacheable.sunset.{SunsetCache, SunsetCacheKey} | ||
import org.sailcbi.APIServer.UserTypes.PublicRequestCache | ||
import play.api.libs.json.{Json, Writes} | ||
import play.api.mvc.{Action, AnyContent, InjectedController} | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class Sunset @Inject()(implicit val exec: ExecutionContext) extends InjectedController { | ||
def get(year: Int, month: Int, day: Int)(implicit PA: PermissionsAuthority): Action[AnyContent] = Action.async(req => { | ||
PA.withRequestCache(PublicRequestCache)(None, ParsedRequest(req), rc => { | ||
val (rows, _) = SunsetCache.get(rc, SunsetCacheKey(year, month)) | ||
// implicit val writes: Writes[SunsetTime] = SunsetTime.storableJsonWrites | ||
rows.find(_.values.forDate.get.getDayOfMonth == day) match { | ||
case Some(r) => Future(Ok(Json.toJson(r))) | ||
case None => Future(NotFound) | ||
} | ||
}) | ||
}) | ||
} |
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
38 changes: 38 additions & 0 deletions
38
app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCache.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,38 @@ | ||
package org.sailcbi.APIServer.Entities.cacheable.sunset | ||
|
||
import com.coleji.neptune.Core.{CacheableFactory, RequestCache} | ||
import com.coleji.neptune.Storable.StorableQuery.QueryBuilder | ||
import com.coleji.neptune.Util.Serde | ||
import org.sailcbi.APIServer.Entities.EntityDefinitions.SunsetTime | ||
import org.sailcbi.APIServer.Entities.cacheable.CacheKeys | ||
|
||
import java.time.{Duration, LocalDate} | ||
|
||
object SunsetCache extends CacheableFactory[SunsetCacheKey, List[SunsetTime]] { | ||
override protected val lifetime: Duration = Duration.ofHours(24) | ||
override protected def calculateKey(config: SunsetCacheKey): String = CacheKeys.sunset(config) | ||
override protected def serialize(result: List[SunsetTime]): String = Serde.serializeList(result) | ||
override protected def deseralize(resultString: String): List[SunsetTime] = Serde.deserializeList(resultString) | ||
override protected def generateResult(rc: RequestCache, config: SunsetCacheKey): List[SunsetTime] = { | ||
val qb = QueryBuilder.from(SunsetTime) | ||
.where(List( | ||
SunsetTime.fields.forDate.alias.greaterEqualConstant(LocalDate.of(config.year, config.month, 1)), | ||
SunsetTime.fields.forDate.alias.lessThanConstant(LocalDate.of(config.year, config.month, 1).plusMonths(1)) | ||
)) | ||
.select(List( | ||
SunsetTime.fields.forDate, | ||
SunsetTime.fields.twilightStart, | ||
SunsetTime.fields.sunrise, | ||
SunsetTime.fields.sunset, | ||
SunsetTime.fields.twilightEnd, | ||
SunsetTime.fields.dayLengthSeconds, | ||
SunsetTime.fields.sonarNoon, | ||
SunsetTime.fields.nauticalTwilightStart, | ||
SunsetTime.fields.nauticalTwilightEnd, | ||
SunsetTime.fields.astronomicalTwilightStart, | ||
SunsetTime.fields.astronomicalTwilightEnd | ||
)) | ||
|
||
rc.executeQueryBuilder(qb).map(SunsetTime.construct).sortWith((a, b) => a.values.forDate.get.isBefore(b.values.forDate.get)) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCacheKey.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,3 @@ | ||
package org.sailcbi.APIServer.Entities.cacheable.sunset | ||
|
||
case class SunsetCacheKey(year: Int, month: Int) |
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