Skip to content

Commit

Permalink
Perform actual deletion of product by index
Browse files Browse the repository at this point in the history
  • Loading branch information
ekuzmichev committed Sep 18, 2024
1 parent c2e8e88 commit 634a603
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
35 changes: 31 additions & 4 deletions src/main/scala/consumer/OzonPriceCheckerUpdateConsumer.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ru.ekuzmichev
package consumer

import bot.OzonPriceCheckerBotCommands
import bot.CallbackData.DeleteProduct
import bot.{CallbackData, OzonPriceCheckerBotCommands}
import common.{ChatId, ProductId, UserName}
import product.{ProductFetcher, ProductIdParser}
import store.*
import store.ProductStore.ProductCandidate.*
import store.ProductStore.{Product, ProductCandidate, SourceId}
import util.lang.Throwables
import util.telegram.MessageSendingUtils.sendTextMessage

import org.telegram.telegrambots.meta.api.objects.{CallbackQuery, Update}
import cats.syntax.either.*
import io.circe.parser.decode
import org.telegram.telegrambots.meta.api.objects.message.Message
import org.telegram.telegrambots.meta.api.objects.{CallbackQuery, Update}
import org.telegram.telegrambots.meta.generics.TelegramClient
import zio.{LogAnnotation, Runtime, Task, ZIO}

Expand All @@ -26,7 +30,7 @@ class OzonPriceCheckerUpdateConsumer(

private implicit val _telegramClient: TelegramClient = telegramClient

//noinspection SimplifyWhenInspection
// noinspection SimplifyWhenInspection
override def consumeZio(update: Update): Task[Unit] =
if update.hasMessage then processMessage(update.getMessage)
else if update.hasCallbackQuery then processCallbackQuery(update.getCallbackQuery)
Expand Down Expand Up @@ -124,7 +128,30 @@ class OzonPriceCheckerUpdateConsumer(
}

private def processCallbackQuery(callbackQuery: CallbackQuery): Task[Unit] =
ZIO.log(s"Callback data: ${callbackQuery.getData}")
val data = callbackQuery.getData
val userName = callbackQuery.getFrom.getUserName
val chatId = callbackQuery.getMessage.getChatId.toString

val sourceId = SourceId(userName, chatId)

ZIO
.logAnnotate(LogAnnotation("userName", userName), LogAnnotation("chatId", chatId)) {
ZIO.log(s"Callback data: $data") *>
ZIO
.fromEither(
decode[CallbackData](data).leftMap(error => Throwables.failure(s"Failed to decode callback data: $error"))
)
.flatMap { case DeleteProduct(productIndex) =>
ZIO.log(s"Removing product with index = $productIndex") *>
productStore.removeProduct(sourceId, productIndex) <*
sendTextMessage(
chatId,
s"The product at index $productIndex has been removed. \n\n" +
s"Check the actual list of watched products with ${OzonPriceCheckerBotCommands.ShowAllProducts}"
)
}
.unit
}

private def onPriceThreshold(sourceId: SourceId, productId: ProductId, priceThreshold: Int) =
sendTextMessage(
Expand Down
4 changes: 0 additions & 4 deletions src/main/scala/store/CacheProductStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,3 @@ class CacheProductStore(decoratee: ProductStore, cacheStateRepository: CacheStat

override def removeProduct(sourceId: SourceId, productIndex: Int): Task[Boolean] =
decoratee.removeProduct(sourceId, productIndex) <* replaceStateInCache()




0 comments on commit 634a603

Please sign in to comment.