From 82ac24be728f0f895f28bfb7de2196f7eec81232 Mon Sep 17 00:00:00 2001 From: Jonathan Cole Date: Sun, 3 Dec 2023 16:19:01 -0500 Subject: [PATCH] sunset endpoitn working --- .../APIServer/Api/Endpoints/Public/Sunset.scala | 13 +++++++++---- .../Entities/cacheable/sunset/SunsetCache.scala | 14 ++++++++++++-- conf/routes-build/src/01_internet | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.scala b/app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.scala index e5f62c10..afd14ffc 100644 --- a/app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.scala +++ b/app/org/sailcbi/APIServer/Api/Endpoints/Public/Sunset.scala @@ -1,19 +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 +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)(implicit PA: PermissionsAuthority): Action[AnyContent] = Action.async(req => { + 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)) - Future(Ok(Json.toJson(rows))) +// 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) + } }) }) } diff --git a/app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCache.scala b/app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCache.scala index 0b59a956..e8b896ed 100644 --- a/app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCache.scala +++ b/app/org/sailcbi/APIServer/Entities/cacheable/sunset/SunsetCache.scala @@ -20,9 +20,19 @@ object SunsetCache extends CacheableFactory[SunsetCacheKey, List[SunsetTime]] { SunsetTime.fields.forDate.alias.lessThanConstant(LocalDate.of(config.year, config.month, 1).plusMonths(1)) )) .select(List( - SunsetTime.fields.forDate + 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) + rc.executeQueryBuilder(qb).map(SunsetTime.construct).sortWith((a, b) => a.values.forDate.get.isBefore(b.values.forDate.get)) } } \ No newline at end of file diff --git a/conf/routes-build/src/01_internet b/conf/routes-build/src/01_internet index 825a8cbc..4958a757 100644 --- a/conf/routes-build/src/01_internet +++ b/conf/routes-build/src/01_internet @@ -6,7 +6,7 @@ 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.Subset.get(year: Int, month: Int) +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()