<type>(scope): <description>
[body] [footer(s)]
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package com.project.movienight.application.services
|
||||
|
||||
import com.project.movienight.adapters.metrics.BusinessMetricsService
|
||||
import com.project.movienight.application.ports.input.AddFilmToLibraryCommand
|
||||
import com.project.movienight.application.ports.input.AddFilmToLibraryUseCase
|
||||
import com.project.movienight.application.ports.input.CreateFilmLibraryCommand
|
||||
import com.project.movienight.application.ports.input.CreateFilmLibraryUseCase
|
||||
import com.project.movienight.application.ports.input.GetFilmLibraryQuery
|
||||
import com.project.movienight.application.ports.input.GetFilmLibraryUseCase
|
||||
import com.project.movienight.application.ports.input.ListFilmLibraryEntriesUseCase
|
||||
import com.project.movienight.application.ports.input.MarkFilmViewedCommand
|
||||
import com.project.movienight.application.ports.input.MarkFilmViewedUseCase
|
||||
import com.project.movienight.application.ports.input.RemoveFilmFromLibraryCommand
|
||||
import com.project.movienight.application.ports.input.RemoveFilmFromLibraryUseCase
|
||||
import com.project.movienight.application.ports.output.FilmLibraryRepositoryPort
|
||||
@@ -20,70 +24,119 @@ import java.util.UUID
|
||||
class FilmLibraryService(
|
||||
private val filmLibraryRepository: FilmLibraryRepositoryPort,
|
||||
private val idGenerator: IdGenerator,
|
||||
private val businessMetricsService: BusinessMetricsService,
|
||||
) : CreateFilmLibraryUseCase,
|
||||
AddFilmToLibraryUseCase,
|
||||
MarkFilmViewedUseCase,
|
||||
RemoveFilmFromLibraryUseCase,
|
||||
GetFilmLibraryUseCase {
|
||||
GetFilmLibraryUseCase,
|
||||
ListFilmLibraryEntriesUseCase {
|
||||
override fun create(command: CreateFilmLibraryCommand): FilmLibrary {
|
||||
val existingLibrary = findByUserId(command.userId)
|
||||
if (existingLibrary != null) {
|
||||
return existingLibrary
|
||||
}
|
||||
findByUserId(command.userId)?.let { return it }
|
||||
|
||||
return filmLibraryRepository.save(
|
||||
FilmLibrary(
|
||||
id = idGenerator.generateId(),
|
||||
userId = command.userId,
|
||||
filmId = idGenerator.generateId(),
|
||||
comment = command.name,
|
||||
isViewed = false,
|
||||
),
|
||||
)
|
||||
val libraryId = idGenerator.generateId()
|
||||
val saved =
|
||||
filmLibraryRepository.save(
|
||||
FilmLibrary(
|
||||
id = libraryId,
|
||||
userId = command.userId,
|
||||
filmId = libraryId,
|
||||
comment = command.name,
|
||||
isViewed = false,
|
||||
watchedAt = null,
|
||||
),
|
||||
)
|
||||
businessMetricsService.recordLibraryEvent()
|
||||
return saved
|
||||
}
|
||||
|
||||
override fun addFilm(command: AddFilmToLibraryCommand): FilmLibrary {
|
||||
val existingLibrary = findByUserId(command.userId)
|
||||
if (existingLibrary == null) {
|
||||
return filmLibraryRepository.save(
|
||||
val existingEntry = findByUserAndFilmId(command.userId, command.filmId)
|
||||
if (existingEntry != null) {
|
||||
val saved =
|
||||
filmLibraryRepository.save(
|
||||
existingEntry.copy(
|
||||
isViewed = false,
|
||||
watchedAt = null,
|
||||
),
|
||||
)
|
||||
businessMetricsService.recordLibraryEvent()
|
||||
return saved
|
||||
}
|
||||
|
||||
val saved =
|
||||
filmLibraryRepository.save(
|
||||
FilmLibrary(
|
||||
id = idGenerator.generateId(),
|
||||
userId = command.userId,
|
||||
filmId = command.filmId,
|
||||
comment = null,
|
||||
isViewed = false,
|
||||
watchedAt = null,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return filmLibraryRepository.save(
|
||||
existingLibrary.copy(
|
||||
filmId = command.filmId,
|
||||
isViewed = false,
|
||||
),
|
||||
)
|
||||
businessMetricsService.recordLibraryEvent()
|
||||
return saved
|
||||
}
|
||||
|
||||
override fun removeFilm(command: RemoveFilmFromLibraryCommand): FilmLibrary {
|
||||
val existingLibrary =
|
||||
findByUserId(command.userId)
|
||||
?: throw EntityNotFoundException(entity = "Film library", id = command.userId.toString())
|
||||
if (command.libraryId != null) {
|
||||
filmLibraryRepository.findById(command.libraryId)
|
||||
?: throw EntityNotFoundException(entity = "Film library", id = command.libraryId.toString())
|
||||
} else {
|
||||
findByUserAndFilmId(command.userId, command.filmId)
|
||||
?: throw EntityNotFoundException(entity = "Film library", id = command.filmId.toString())
|
||||
}
|
||||
|
||||
if (command.libraryId != null && command.libraryId != existingLibrary.id) {
|
||||
throw EntityNotFoundException(entity = "Film library", id = command.libraryId.toString())
|
||||
}
|
||||
|
||||
if (existingLibrary.filmId != command.filmId) {
|
||||
if (existingLibrary.userId != command.userId || existingLibrary.filmId != command.filmId) {
|
||||
throw DomainException("Film with id ${command.filmId} not found in user's library")
|
||||
}
|
||||
|
||||
filmLibraryRepository.deleteById(existingLibrary.id)
|
||||
businessMetricsService.recordLibraryEvent()
|
||||
return existingLibrary
|
||||
}
|
||||
|
||||
override fun markViewed(command: MarkFilmViewedCommand): FilmLibrary {
|
||||
val existingEntry = findByUserAndFilmId(command.userId, command.filmId)
|
||||
val watchedAt = command.watchedAt ?: java.time.LocalDateTime.now()
|
||||
|
||||
val saved =
|
||||
if (existingEntry == null) {
|
||||
filmLibraryRepository.save(
|
||||
FilmLibrary(
|
||||
id = idGenerator.generateId(),
|
||||
userId = command.userId,
|
||||
filmId = command.filmId,
|
||||
comment = null,
|
||||
isViewed = true,
|
||||
watchedAt = watchedAt,
|
||||
),
|
||||
)
|
||||
} else {
|
||||
filmLibraryRepository.save(
|
||||
existingEntry.copy(
|
||||
isViewed = true,
|
||||
watchedAt = watchedAt,
|
||||
),
|
||||
)
|
||||
}
|
||||
businessMetricsService.recordLibraryEvent()
|
||||
return saved
|
||||
}
|
||||
|
||||
override fun getLibrary(query: GetFilmLibraryQuery): FilmLibrary =
|
||||
findByUserId(query.userId)
|
||||
?: throw EntityNotFoundException(entity = "Film library", id = query.userId.toString())
|
||||
|
||||
override fun list(userId: UUID): List<FilmLibrary> = filmLibraryRepository.findAll().filter { it.userId == userId }
|
||||
|
||||
private fun findByUserId(userId: UUID): FilmLibrary? =
|
||||
filmLibraryRepository.findAll().firstOrNull { it.userId == userId }
|
||||
|
||||
private fun findByUserAndFilmId(
|
||||
userId: UUID,
|
||||
filmId: UUID,
|
||||
): FilmLibrary? = filmLibraryRepository.findAll().firstOrNull { it.userId == userId && it.filmId == filmId }
|
||||
}
|
||||
|
||||
@@ -56,21 +56,23 @@ class FilmService(
|
||||
throw BlockedValueException(target = "Film", field = "description")
|
||||
}
|
||||
|
||||
val film =
|
||||
Film(
|
||||
id = idGenerator.generateId(),
|
||||
title = command.title,
|
||||
description = command.description,
|
||||
)
|
||||
val saved = filmRepository.save(film)
|
||||
|
||||
filmCreatedCounter.increment()
|
||||
|
||||
log.info("Film created: id='{}', title='{}'", saved.id, saved.title)
|
||||
return saved
|
||||
} finally {
|
||||
sample.stop(createFilmTimer)
|
||||
}
|
||||
val film =
|
||||
Film(
|
||||
id = idGenerator.generateId(),
|
||||
title = command.title,
|
||||
description = command.description,
|
||||
contentType = command.contentType,
|
||||
releaseYear = command.releaseYear,
|
||||
genres = command.genres,
|
||||
cast = command.cast,
|
||||
directors = command.directors,
|
||||
imdbRating = command.imdbRating,
|
||||
platformRating = command.platformRating,
|
||||
externalUrl = command.externalUrl,
|
||||
jellyfinItemId = command.jellyfinItemId,
|
||||
jellyfinLibraryId = command.jellyfinLibraryId,
|
||||
)
|
||||
return filmRepository.save(film)
|
||||
}
|
||||
|
||||
override fun edit(
|
||||
@@ -100,20 +102,23 @@ class FilmService(
|
||||
throw EntityNotFoundException(entity = "Film", id = id.toString())
|
||||
}
|
||||
|
||||
val updatedFilm =
|
||||
film.copy(
|
||||
title = command.title,
|
||||
description = command.description,
|
||||
)
|
||||
val saved = filmRepository.save(updatedFilm)
|
||||
film =
|
||||
film.copy(
|
||||
title = command.title,
|
||||
description = command.description,
|
||||
contentType = command.contentType,
|
||||
releaseYear = command.releaseYear,
|
||||
genres = command.genres,
|
||||
cast = command.cast,
|
||||
directors = command.directors,
|
||||
imdbRating = command.imdbRating,
|
||||
platformRating = command.platformRating,
|
||||
externalUrl = command.externalUrl,
|
||||
jellyfinItemId = command.jellyfinItemId,
|
||||
jellyfinLibraryId = command.jellyfinLibraryId,
|
||||
)
|
||||
|
||||
filmEditedCounter.increment()
|
||||
|
||||
log.info("Film edited: id='{}'", saved.id)
|
||||
return saved
|
||||
} finally {
|
||||
sample.stop(editFilmTimer)
|
||||
}
|
||||
return filmRepository.save(film)
|
||||
}
|
||||
|
||||
override fun delete(id: UUID) {
|
||||
|
||||
@@ -37,6 +37,7 @@ class UserService(
|
||||
name = command.name,
|
||||
email = command.email,
|
||||
library = null,
|
||||
jellyfinUserId = null,
|
||||
)
|
||||
return userRepository.save(user)
|
||||
}
|
||||
@@ -50,7 +51,13 @@ class UserService(
|
||||
}
|
||||
|
||||
var user = userRepository.findById(id) ?: throw EntityNotFoundException(entity = "User", id = id.toString())
|
||||
user = user.copy(name = command.name)
|
||||
|
||||
user =
|
||||
user.copy(
|
||||
name = command.name,
|
||||
jellyfinUserId = command.jellyfinUserId ?: user.jellyfinUserId,
|
||||
)
|
||||
|
||||
return userRepository.save(user)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user