|
| 1 | +package com.gu.liveactivities |
| 2 | + |
| 3 | +import aws.AsyncDynamo |
| 4 | +import aws.DynamoJsonConversions.{fromAttributeMap, toAttributeMap} |
| 5 | +import cats.syntax.all._ |
| 6 | +import com.amazonaws.services.dynamodbv2.model._ |
| 7 | +import org.slf4j.{Logger, LoggerFactory} |
| 8 | +import play.api.libs.json._ |
| 9 | +import tracking.Repository.RepositoryResult |
| 10 | +import tracking.RepositoryError |
| 11 | + |
| 12 | +import scala.concurrent.{ExecutionContext, Future} |
| 13 | +import scala.jdk.CollectionConverters._ |
| 14 | + |
| 15 | +// MODELS ///////////////////////////////////////////// |
| 16 | + |
| 17 | +sealed trait LiveActivityData |
| 18 | +case class FootballLiveActivity( |
| 19 | + homeTeam: String, |
| 20 | + awayTeam: String, |
| 21 | + articleUrl: String |
| 22 | +) extends LiveActivityData |
| 23 | + |
| 24 | +object FootballLiveActivity { |
| 25 | + implicit val format: OFormat[FootballLiveActivity] = |
| 26 | + Json.format[FootballLiveActivity] |
| 27 | +} |
| 28 | + |
| 29 | +object LiveActivityData { |
| 30 | + implicit val format: OFormat[LiveActivityData] = |
| 31 | + new OFormat[LiveActivityData] { |
| 32 | + def writes(data: LiveActivityData): JsObject = data match { |
| 33 | + case f: FootballLiveActivity => |
| 34 | + FootballLiveActivity.format.writes(f) + ("type" -> JsString( |
| 35 | + "football" |
| 36 | + )) |
| 37 | + } |
| 38 | + def reads(json: JsValue): JsResult[LiveActivityData] = |
| 39 | + (json \ "type").validate[String].flatMap { |
| 40 | + case "football" => FootballLiveActivity.format.reads(json) |
| 41 | + case other => JsError(s"Unknown LiveActivityData type: $other") |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +case class LiveActivityMapping( |
| 47 | + liveActivityId: String, |
| 48 | + channelId: String, |
| 49 | + data: Option[LiveActivityData] |
| 50 | +) |
| 51 | +object LiveActivityMapping { |
| 52 | + implicit val format: OFormat[LiveActivityMapping] = |
| 53 | + Json.format[LiveActivityMapping] |
| 54 | +} |
| 55 | + |
| 56 | +// REPOSITORY ///////////////////////////////////////////// |
| 57 | + |
| 58 | +trait ChannelMappingsRepository { |
| 59 | + def saveMapping(mapping: LiveActivityMapping): Future[RepositoryResult[Unit]] |
| 60 | + def deleteMappingByActivityId(id: String): Future[RepositoryResult[Unit]] |
| 61 | + def getMappingByActivityId( |
| 62 | + id: String |
| 63 | + ): Future[RepositoryResult[LiveActivityMapping]] |
| 64 | +} |
| 65 | + |
| 66 | +class LiveActivityChannelRepository(client: AsyncDynamo, tableName: String)( |
| 67 | + implicit ec: ExecutionContext |
| 68 | +) extends ChannelMappingsRepository { |
| 69 | + |
| 70 | + private val logger: Logger = LoggerFactory.getLogger(this.getClass) |
| 71 | + private val IdField = "liveActivityId" |
| 72 | + |
| 73 | + override def saveMapping( |
| 74 | + mapping: LiveActivityMapping |
| 75 | + ): Future[RepositoryResult[Unit]] = { |
| 76 | + val putItemRequest = |
| 77 | + new PutItemRequest() |
| 78 | + .withTableName(tableName) |
| 79 | + .withItem(toAttributeMap(mapping).asJava) |
| 80 | + .withConditionExpression(s"attribute_not_exists($IdField)") |
| 81 | + |
| 82 | + client |
| 83 | + .putItem(putItemRequest) |
| 84 | + .map(_ => Right(()): RepositoryResult[Unit]) |
| 85 | + .recover { case ex: Exception => |
| 86 | + println("Error saving live activity mapping: " + ex.getMessage) |
| 87 | + val errorClass = ex.getClass.getName |
| 88 | + Left(RepositoryError(errorClass.toString)) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + override def getMappingByActivityId( |
| 93 | + id: String |
| 94 | + ): Future[RepositoryResult[LiveActivityMapping]] = { |
| 95 | + val getItemRequest = new GetItemRequest() |
| 96 | + .withTableName(tableName) |
| 97 | + .withKey(Map(IdField -> new AttributeValue().withS(id)).asJava) |
| 98 | + .withConsistentRead(true) |
| 99 | + |
| 100 | + client.get(getItemRequest) map { result => |
| 101 | + for { |
| 102 | + item <- Either.fromOption( |
| 103 | + Option(result.getItem), |
| 104 | + RepositoryError("Live Activity not found") |
| 105 | + ) |
| 106 | + parsed <- Either.fromOption( |
| 107 | + fromAttributeMap[LiveActivityMapping](item.asScala.toMap).asOpt, |
| 108 | + RepositoryError("Unable to parse live activity mapping") |
| 109 | + ) |
| 110 | + } yield parsed |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + override def deleteMappingByActivityId( |
| 115 | + id: String |
| 116 | + ): Future[RepositoryResult[Unit]] = { |
| 117 | + val deleteItemRequest = new DeleteItemRequest() |
| 118 | + .withTableName(tableName) |
| 119 | + .withKey(Map(IdField -> new AttributeValue().withS(id)).asJava) |
| 120 | + client.deleteItem(deleteItemRequest) map { _ => Right(()) } |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments