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
/** Let's change the test to ignore the request/response/routing stuff and just test the business logic.
*
* It would be easier to test this logic if we made the dependencies
* explicit (method params), and moved it out to the companion object.
*/
trait EndpointsV2 {
this: UserServiceComponent with GiftServiceComponent =>
import EndpointsV2._
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))
gift <- maybeSendGift(user, giftService)
response <- Future.fromTry(WebApp.mkResponse(gift))
} yield response
response.recover(WebApp.mkErrorResponse)
}
}
object EndpointsV2 {
private def isToday(birthday: LocalDate): Boolean = {
val today = DateTimeUtils.now.toLocalDate
today.getDayOfMonth == birthday.getDayOfMonth &&
today.getMonth == birthday.getMonth
}
private[endpoints] def maybeSendGift(user: User, giftService: GiftService) = {
val birthdayToday = isToday(user.birthday)
if (birthdayToday) giftService.order(user.id, user.favoriteThing)
else Future.successful(("not today", None))
}
}