This commit is contained in:
Elena
2026-05-22 15:41:24 +03:00
parent 9e9e2244ab
commit 6d35b2f5a6
@@ -1,192 +1,197 @@
//package com.project.movienight.application.services package com.project.movienight.application.services
//
//import com.project.movienight.adapters.metrics.BusinessMetricsService import com.project.movienight.adapters.metrics.BusinessMetricsService
//import com.project.movienight.application.ports.input.AddFilmToLibraryCommand import com.project.movienight.application.ports.input.AddFilmToLibraryCommand
//import com.project.movienight.application.ports.input.AddFilmToLibraryUseCase import com.project.movienight.application.ports.input.AddFilmToLibraryUseCase
//import com.project.movienight.application.ports.input.CreateFilmLibraryCommand import com.project.movienight.application.ports.input.CreateFilmLibraryCommand
//import com.project.movienight.application.ports.input.CreateFilmLibraryUseCase import com.project.movienight.application.ports.input.CreateFilmLibraryUseCase
//import com.project.movienight.application.ports.input.GetFilmLibraryQuery import com.project.movienight.application.ports.input.GetFilmLibraryQuery
//import com.project.movienight.application.ports.input.GetFilmLibraryUseCase import com.project.movienight.application.ports.input.GetFilmLibraryUseCase
//import com.project.movienight.application.ports.input.ListFilmLibraryEntriesUseCase import com.project.movienight.application.ports.input.ListFilmLibraryEntriesUseCase
//import com.project.movienight.application.ports.input.MarkFilmViewedCommand import com.project.movienight.application.ports.input.MarkFilmViewedCommand
//import com.project.movienight.application.ports.input.MarkFilmViewedUseCase import com.project.movienight.application.ports.input.MarkFilmViewedUseCase
//import com.project.movienight.application.ports.input.RemoveFilmFromLibraryCommand import com.project.movienight.application.ports.input.RemoveFilmFromLibraryCommand
//import com.project.movienight.application.ports.input.RemoveFilmFromLibraryUseCase import com.project.movienight.application.ports.input.RemoveFilmFromLibraryUseCase
//import com.project.movienight.application.ports.output.FilmLibraryRepositoryPort import com.project.movienight.application.ports.output.FilmLibraryRepositoryPort
//import com.project.movienight.application.ports.output.IdGenerator import com.project.movienight.application.ports.output.IdGenerator
//import com.project.movienight.domain.exception.DomainException import com.project.movienight.domain.exception.DomainException
//import com.project.movienight.domain.exception.EntityNotFoundException import com.project.movienight.domain.exception.EntityNotFoundException
//import com.project.movienight.domain.model.FilmLibrary import com.project.movienight.domain.model.FilmLibrary
//import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
//import org.springframework.stereotype.Service import org.springframework.stereotype.Service
//import java.util.UUID import java.util.UUID
//
//@Service @Service
//class FilmLibraryService( class FilmLibraryService(
// private val filmLibraryRepository: FilmLibraryRepositoryPort, private val filmLibraryRepository: FilmLibraryRepositoryPort,
// private val idGenerator: IdGenerator, private val idGenerator: IdGenerator,
// private val businessMetricsService: BusinessMetricsService, private val businessMetricsService: BusinessMetricsService,
//) : CreateFilmLibraryUseCase, ) : CreateFilmLibraryUseCase,
// AddFilmToLibraryUseCase, AddFilmToLibraryUseCase,
// MarkFilmViewedUseCase, MarkFilmViewedUseCase,
// RemoveFilmFromLibraryUseCase, RemoveFilmFromLibraryUseCase,
// GetFilmLibraryUseCase, GetFilmLibraryUseCase,
// ListFilmLibraryEntriesUseCase { ListFilmLibraryEntriesUseCase {
// private val log = LoggerFactory.getLogger(javaClass) private val log = LoggerFactory.getLogger(javaClass)
//
// override fun create(command: CreateFilmLibraryCommand): FilmLibrary { override fun create(command: CreateFilmLibraryCommand): FilmLibrary {
// log.info("Creating film library for user: {}", command.userId) log.info("Creating film library for user: {}", command.userId)
// log.debug("Create library request: userId={}, name={}", command.userId, command.name) log.debug("Create library request: userId={}, name={}", command.userId, command.name)
//
// val existing = findByUserId(command.userId) val existing = findByUserId(command.userId)
// if (existing != null) { if (existing != null) {
// log.debug("Library already exists for user {}: libraryId={}", command.userId, existing.id) log.debug("Library already exists for user {}: libraryId={}", command.userId, existing.id)
// return existing return existing
// } }
//
// log.warn("Library not found for user {}, cannot create", command.userId) log.warn("Library not found for user {}, cannot create", command.userId)
// throw EntityNotFoundException(entity = "Film library", id = command.userId.toString()) throw EntityNotFoundException(entity = "Film library", id = command.userId.toString())
// } }
//
// override fun addFilm(command: AddFilmToLibraryCommand): FilmLibrary { override fun addFilm(command: AddFilmToLibraryCommand): FilmLibrary {
// log.info("Adding film to library: userId={}, filmId={}", command.userId, command.filmId) log.info("Adding film to library: userId={}, filmId={}", command.userId, command.filmId)
//
// val existingEntry = findByUserAndFilmId(command.userId, command.filmId) val existingEntry = findByUserAndFilmId(command.userId, command.filmId)
// if (existingEntry != null) { if (existingEntry != null) {
// log.debug("Film already in library, resetting as not viewed: entryId={}", existingEntry.id) log.debug("Film already in library, resetting as not viewed: entryId={}", existingEntry.id)
// val saved = val saved =
// filmLibraryRepository.save( filmLibraryRepository.save(
// existingEntry.copy( existingEntry.copy(
// isViewed = false, isViewed = false,
// watchedAt = null, watchedAt = null,
// ), ),
// ) )
// businessMetricsService.recordLibraryEvent() businessMetricsService.recordLibraryEvent()
// log.info( log.info(
// "Film re-added to library: userId={}, filmId={}, entryId={}", "Film re-added to library: userId={}, filmId={}, entryId={}",
// command.userId, command.userId,
// command.filmId, command.filmId,
// saved.id, saved.id,
// ) )
// return saved return saved
// } }
//
// val saved = val saved =
// filmLibraryRepository.save( filmLibraryRepository.save(
// FilmLibrary( FilmLibrary(
// id = idGenerator.generateId(), id = idGenerator.generateId(),
// userId = command.userId, userId = command.userId,
// filmId = command.filmId, filmId = command.filmId,
// comment = null, comment = null,
// isViewed = false, isViewed = false,
// watchedAt = null, watchedAt = null,
// ), ),
// ) )
// businessMetricsService.recordLibraryEvent() businessMetricsService.recordLibraryEvent()
// log.info("Film added to library: userId={}, filmId={}, entryId={}", command.userId, command.filmId, saved.id) log.info(
// return saved "Film added to library: userId={}, filmId={}, entryId={}",
// } command.userId,
// command.filmId,
// override fun removeFilm(command: RemoveFilmFromLibraryCommand): FilmLibrary { saved.id,
// log.info("Removing film from library: userId={}, filmId={}", command.userId, command.filmId) )
// return saved
// val existingLibrary = }
// if (command.libraryId != null) {
// log.debug("Looking up by libraryId: {}", command.libraryId) override fun removeFilm(command: RemoveFilmFromLibraryCommand): FilmLibrary {
// filmLibraryRepository.findById(command.libraryId) log.info("Removing film from library: userId={}, filmId={}", command.userId, command.filmId)
// ?: throw EntityNotFoundException(entity = "Film library", id = command.libraryId.toString())
// } else { val existingLibrary =
// log.debug("Looking up by userId and filmId") if (command.libraryId != null) {
// findByUserAndFilmId(command.userId, command.filmId) log.debug("Looking up by libraryId: {}", command.libraryId)
// ?: throw EntityNotFoundException(entity = "Film library", id = command.filmId.toString()) filmLibraryRepository.findById(command.libraryId)
// } ?: throw EntityNotFoundException(entity = "Film library", id = command.libraryId.toString())
// } else {
// if (existingLibrary.userId != command.userId || existingLibrary.filmId != command.filmId) { log.debug("Looking up by userId and filmId")
// log.warn("Film not found in user's library: userId={}, filmId={}", command.userId, command.filmId) findByUserAndFilmId(command.userId, command.filmId)
// throw DomainException("Film with id ${command.filmId} not found in user's library") ?: throw EntityNotFoundException(entity = "Film library", id = command.filmId.toString())
// } }
//
// filmLibraryRepository.deleteById(existingLibrary.id) if (existingLibrary.userId != command.userId || existingLibrary.filmId != command.filmId) {
// businessMetricsService.recordLibraryEvent() log.warn("Film not found in user's library: userId={}, filmId={}", command.userId, command.filmId)
// log.info( throw DomainException("Film with id ${command.filmId} not found in user's library")
// "Film removed from library: userId={}, filmId={}, entryId={}", }
// command.userId,
// command.filmId, filmLibraryRepository.deleteById(existingLibrary.id)
// existingLibrary.id, businessMetricsService.recordLibraryEvent()
// ) log.info(
// return existingLibrary "Film removed from library: userId={}, filmId={}, entryId={}",
// } command.userId,
// command.filmId,
// override fun markViewed(command: MarkFilmViewedCommand): FilmLibrary { existingLibrary.id,
// log.info("Marking film as viewed: userId={}, filmId={}", command.userId, command.filmId) )
// return existingLibrary
// val existingEntry = findByUserAndFilmId(command.userId, command.filmId) }
// val watchedAt = command.watchedAt ?: java.time.LocalDateTime.now()
// override fun markViewed(command: MarkFilmViewedCommand): FilmLibrary {
// log.debug("Marking as viewed at: {}", watchedAt) log.info("Marking film as viewed: userId={}, filmId={}", command.userId, command.filmId)
//
// val saved = val existingEntry = findByUserAndFilmId(command.userId, command.filmId)
// if (existingEntry == null) { val watchedAt = command.watchedAt ?: java.time.LocalDateTime.now()
// log.debug("Film not in library, creating new entry as viewed")
// filmLibraryRepository.save( log.debug("Marking as viewed at: {}", watchedAt)
// FilmLibrary(
// id = idGenerator.generateId(), val saved =
// userId = command.userId, if (existingEntry == null) {
// filmId = command.filmId, log.debug("Film not in library, creating new entry as viewed")
// comment = null, filmLibraryRepository.save(
// isViewed = true, FilmLibrary(
// watchedAt = watchedAt, id = idGenerator.generateId(),
// ), userId = command.userId,
// ) filmId = command.filmId,
// } else { comment = null,
// log.debug( isViewed = true,
// "Updating existing entry: entryId={}, was viewed={}", watchedAt = watchedAt,
// existingEntry.id, ),
// existingEntry.isViewed, )
// ) } else {
// filmLibraryRepository.save( log.debug(
// existingEntry.copy( "Updating existing entry: entryId={}, was viewed={}",
// isViewed = true, existingEntry.id,
// watchedAt = watchedAt, existingEntry.isViewed,
// ), )
// ) filmLibraryRepository.save(
// } existingEntry.copy(
// businessMetricsService.recordLibraryEvent() isViewed = true,
// log.info( watchedAt = watchedAt,
// "Film marked as viewed: userId={}, filmId={}, entryId={}", ),
// command.userId, )
// command.filmId, }
// saved.id, businessMetricsService.recordLibraryEvent()
// ) log.info(
// return saved "Film marked as viewed: userId={}, filmId={}, entryId={}",
// } command.userId,
// command.filmId,
// override fun getLibrary(query: GetFilmLibraryQuery): FilmLibrary { saved.id,
// log.debug("Getting library for user: {}", query.userId) )
// val library = return saved
// findByUserId(query.userId) }
// ?: throw EntityNotFoundException(entity = "Film library", id = query.userId.toString())
// log.debug("Library found: userId={}, libraryId={}", query.userId, library.id) override fun getLibrary(query: GetFilmLibraryQuery): FilmLibrary {
// return library log.debug("Getting library for user: {}", query.userId)
// } val library =
// findByUserId(query.userId)
// override fun list(userId: UUID): List<FilmLibrary> { ?: throw EntityNotFoundException(entity = "Film library", id = query.userId.toString())
// log.debug("Listing all library entries for user: {}", userId) log.debug("Library found: userId={}, libraryId={}", query.userId, library.id)
// val entries = filmLibraryRepository.findAll().filter { it.userId == userId } return library
// log.info("User {} has {} films in library", userId, entries.size) }
// return entries
// } override fun list(userId: UUID): List<FilmLibrary> {
// log.debug("Listing all library entries for user: {}", userId)
// private fun findByUserId(userId: UUID): FilmLibrary? = val entries = filmLibraryRepository.findAll().filter { it.userId == userId }
// filmLibraryRepository.findAll().firstOrNull { log.info("User {} has {} films in library", userId, entries.size)
// it.userId == userId return entries
// } }
//
// private fun findByUserAndFilmId( private fun findByUserId(userId: UUID): FilmLibrary? =
// userId: UUID, filmLibraryRepository.findAll().firstOrNull {
// filmId: UUID, it.userId == userId
// ): FilmLibrary? = }
// filmLibraryRepository.findAll().firstOrNull {
// it.userId == userId && it.filmId == filmId private fun findByUserAndFilmId(
// } userId: UUID,
//} filmId: UUID,
): FilmLibrary? =
filmLibraryRepository.findAll().firstOrNull {
it.userId == userId && it.filmId == filmId
}
}