Skip to content

Commit

Permalink
sunset api
Browse files Browse the repository at this point in the history
  • Loading branch information
coleji committed Dec 3, 2023
1 parent 3465b73 commit f12eb5c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/org/sailcbi/APIServer/Api/Endpoints/Dto/Sunset/Get.scala
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 app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.scala
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)
}
})
})
}
2 changes: 2 additions & 0 deletions app/org/sailcbi/APIServer/Entities/cacheable/CacheKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.sailcbi.APIServer.Entities.cacheable

import org.sailcbi.APIServer.Entities.cacheable.ApClassInstances.ApClassInstancesCacheKey
import org.sailcbi.APIServer.Entities.cacheable.MembershipSales.MembershipSalesCacheKey
import org.sailcbi.APIServer.Entities.cacheable.sunset.SunsetCacheKey

import java.time.format.DateTimeFormatter

Expand All @@ -18,4 +19,5 @@ object CacheKeys {
def apClassTypes = "ap-class-types"
def programTypes = "program-types"
def apClassInstancesThisSeason = "apClassInstancesThisSeason"
def sunset(config: SunsetCacheKey): String = s"sunset-${config.year}-${config.month}"
}
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))
}
}
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)
2 changes: 2 additions & 0 deletions conf/routes-build/src/01_internet
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ POST /stripe/create-charge-from-token org.sailcbi.APIServer.Api.Endpoints.
GET /stripe/token-details org.sailcbi.APIServer.Api.Endpoints.Stripe.GetTokenDetails.get(token: String)
POST /stripe/sync-db org.sailcbi.APIServer.Api.Endpoints.Stripe.SyncLocalDBWithStripe.post()

GET /sunset org.sailcbi.APIServer.Api.Endpoints.Public.Sunset.get(year: Int, month: Int, day: Int)

GET /efuse/cbidb-public-web org.sailcbi.APIServer.Api.Endpoints.Public.CheckEFusePublicWeb.get()

GET /static-yearly-data org.sailcbi.APIServer.Api.Endpoints.Public.StaticYearlyData.get()
Expand Down

0 comments on commit f12eb5c

Please sign in to comment.