package com.eddsteel.posts.leastpower
package endpoints
import model._
import services._
import webapp.{Request, Response, NotFoundException}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import java.time.LocalDate
/** Endpoints for our Web app.
*
* The important feature: given a user ID, will order that user a gift
* if it's their birthday.
*
*/
trait EndpointsV1 {
this: UserServiceComponent with GiftServiceComponent =>
import EndpointsV1._
def postGift(req: Request)(implicit ec: ExecutionContext): Future[Response] = {
val response = for {
userId <- Future.fromTry(WebApp.extractAndValidateUserId(req))
maybeUser <- userService.getById(userId)
user = maybeUser.getOrElse(throw NotFoundException(userId))
isBirthday = isToday(user.birthday)
gift <- if (isBirthday) giftService.order(userId, user.favoriteThing)
else Future.successful(("not today", None))
response <- Future.fromTry(WebApp.mkResponse(gift))
} yield response
response.recover(WebApp.mkErrorResponse)
}
}
object EndpointsV1 {
private def isToday(birthday: LocalDate): Boolean = {
val today = DateTimeUtils.now.toLocalDate
today.getDayOfMonth == birthday.getDayOfMonth &&
today.getMonth == birthday.getMonth
}
}