feat(services): added domain exceptions to services
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
package com.project.movienight.application.services
|
||||||
|
|
||||||
|
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.RemoveFilmFromLibraryCommand
|
||||||
|
import com.project.movienight.application.ports.input.RemoveFilmFromLibraryUseCase
|
||||||
|
import com.project.movienight.application.ports.output.FilmLibraryRepositoryPort
|
||||||
|
import com.project.movienight.application.ports.output.IdGenerator
|
||||||
|
import com.project.movienight.domain.exception.DomainException
|
||||||
|
import com.project.movienight.domain.exception.EntityNotFoundException
|
||||||
|
import com.project.movienight.domain.model.FilmLibrary
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class FilmLibraryService(
|
||||||
|
private val filmLibraryRepository: FilmLibraryRepositoryPort,
|
||||||
|
private val idGenerator: IdGenerator,
|
||||||
|
) : CreateFilmLibraryUseCase,
|
||||||
|
AddFilmToLibraryUseCase,
|
||||||
|
RemoveFilmFromLibraryUseCase,
|
||||||
|
GetFilmLibraryUseCase {
|
||||||
|
override fun create(command: CreateFilmLibraryCommand): FilmLibrary {
|
||||||
|
val existingLibrary = findByUserId(command.userId)
|
||||||
|
if (existingLibrary != null) {
|
||||||
|
return existingLibrary
|
||||||
|
}
|
||||||
|
|
||||||
|
return filmLibraryRepository.save(
|
||||||
|
FilmLibrary(
|
||||||
|
id = idGenerator.generateId(),
|
||||||
|
userId = command.userId,
|
||||||
|
filmId = idGenerator.generateId(),
|
||||||
|
comment = command.name,
|
||||||
|
isViewed = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addFilm(command: AddFilmToLibraryCommand): FilmLibrary {
|
||||||
|
val existingLibrary = findByUserId(command.userId)
|
||||||
|
if (existingLibrary == null) {
|
||||||
|
return filmLibraryRepository.save(
|
||||||
|
FilmLibrary(
|
||||||
|
id = idGenerator.generateId(),
|
||||||
|
userId = command.userId,
|
||||||
|
filmId = command.filmId,
|
||||||
|
comment = null,
|
||||||
|
isViewed = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filmLibraryRepository.save(
|
||||||
|
existingLibrary.copy(
|
||||||
|
filmId = command.filmId,
|
||||||
|
isViewed = false,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun removeFilm(command: RemoveFilmFromLibraryCommand): FilmLibrary {
|
||||||
|
val existingLibrary =
|
||||||
|
findByUserId(command.userId)
|
||||||
|
?: throw EntityNotFoundException(entity = "Film library", id = command.userId.toString())
|
||||||
|
|
||||||
|
if (command.libraryId != null && command.libraryId != existingLibrary.id) {
|
||||||
|
throw EntityNotFoundException(entity = "Film library", id = command.libraryId.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingLibrary.filmId != command.filmId) {
|
||||||
|
throw DomainException("Film with id ${command.filmId} not found in user's library")
|
||||||
|
}
|
||||||
|
|
||||||
|
filmLibraryRepository.deleteById(existingLibrary.id)
|
||||||
|
return existingLibrary
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getLibrary(query: GetFilmLibraryQuery): FilmLibrary =
|
||||||
|
findByUserId(query.userId)
|
||||||
|
?: throw EntityNotFoundException(entity = "Film library", id = query.userId.toString())
|
||||||
|
|
||||||
|
private fun findByUserId(userId: UUID): FilmLibrary? =
|
||||||
|
filmLibraryRepository.findAll().firstOrNull { it.userId == userId }
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ import com.project.movienight.application.ports.input.EditFilmUseCase
|
|||||||
import com.project.movienight.application.ports.output.FilmRepositoryPort
|
import com.project.movienight.application.ports.output.FilmRepositoryPort
|
||||||
import com.project.movienight.application.ports.output.IdGenerator
|
import com.project.movienight.application.ports.output.IdGenerator
|
||||||
import com.project.movienight.config.FilmServiceProperties
|
import com.project.movienight.config.FilmServiceProperties
|
||||||
|
import com.project.movienight.domain.exception.BlockedValueException
|
||||||
|
import com.project.movienight.domain.exception.EntityNotFoundException
|
||||||
import com.project.movienight.domain.model.Film
|
import com.project.movienight.domain.model.Film
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
@@ -22,10 +24,10 @@ class FilmService(
|
|||||||
DeleteFilmUseCase {
|
DeleteFilmUseCase {
|
||||||
override fun create(command: CreateFilmCommand): Film {
|
override fun create(command: CreateFilmCommand): Film {
|
||||||
if (filmConfig.isBlocked(command.title)) {
|
if (filmConfig.isBlocked(command.title)) {
|
||||||
throw IllegalArgumentException("Film with this title is not acceptable")
|
throw BlockedValueException(target = "Film", field = "title")
|
||||||
}
|
}
|
||||||
if (filmConfig.isBlocked(command.description)) {
|
if (filmConfig.isBlocked(command.description)) {
|
||||||
throw IllegalArgumentException("Film with this description is not acceptable")
|
throw BlockedValueException(target = "Film", field = "description")
|
||||||
}
|
}
|
||||||
|
|
||||||
val film =
|
val film =
|
||||||
@@ -42,13 +44,13 @@ class FilmService(
|
|||||||
command: EditFilmCommand,
|
command: EditFilmCommand,
|
||||||
): Film {
|
): Film {
|
||||||
if (filmConfig.isBlocked(command.title)) {
|
if (filmConfig.isBlocked(command.title)) {
|
||||||
throw IllegalArgumentException("Film with this title is not acceptable")
|
throw BlockedValueException(target = "Film", field = "title")
|
||||||
}
|
}
|
||||||
if (filmConfig.isBlocked(command.description)) {
|
if (filmConfig.isBlocked(command.description)) {
|
||||||
throw IllegalArgumentException("Film with this description is not acceptable")
|
throw BlockedValueException(target = "Film", field = "description")
|
||||||
}
|
}
|
||||||
|
|
||||||
var film = filmRepository.findById(id) ?: throw IllegalArgumentException("Film with id $id not found")
|
var film = filmRepository.findById(id) ?: throw EntityNotFoundException(entity = "Film", id = id.toString())
|
||||||
|
|
||||||
film = film.copy(title = command.title, description = command.description)
|
film = film.copy(title = command.title, description = command.description)
|
||||||
|
|
||||||
@@ -56,7 +58,7 @@ class FilmService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(id: UUID) {
|
override fun delete(id: UUID) {
|
||||||
filmRepository.findById(id) ?: throw IllegalArgumentException("Film with id $id not found")
|
filmRepository.findById(id) ?: throw EntityNotFoundException(entity = "Film", id = id.toString())
|
||||||
|
|
||||||
filmRepository.deleteById(id)
|
filmRepository.deleteById(id)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import com.project.movienight.application.ports.input.EditUserUseCase
|
|||||||
import com.project.movienight.application.ports.output.IdGenerator
|
import com.project.movienight.application.ports.output.IdGenerator
|
||||||
import com.project.movienight.application.ports.output.UserRepositoryPort
|
import com.project.movienight.application.ports.output.UserRepositoryPort
|
||||||
import com.project.movienight.config.UserServiceProperties
|
import com.project.movienight.config.UserServiceProperties
|
||||||
|
import com.project.movienight.domain.exception.BlockedValueException
|
||||||
|
import com.project.movienight.domain.exception.EntityNotFoundException
|
||||||
import com.project.movienight.domain.model.User
|
import com.project.movienight.domain.model.User
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
@@ -22,7 +24,7 @@ class UserService(
|
|||||||
DeleteUserUseCase {
|
DeleteUserUseCase {
|
||||||
override fun create(command: CreateUserCommand): User {
|
override fun create(command: CreateUserCommand): User {
|
||||||
if (userConfig.isBlocked(command.name)) {
|
if (userConfig.isBlocked(command.name)) {
|
||||||
throw IllegalArgumentException("User with this name is not acceptable")
|
throw BlockedValueException(target = "User", field = "name")
|
||||||
}
|
}
|
||||||
|
|
||||||
val user =
|
val user =
|
||||||
@@ -40,10 +42,10 @@ class UserService(
|
|||||||
command: EditUserCommand,
|
command: EditUserCommand,
|
||||||
): User {
|
): User {
|
||||||
if (userConfig.isBlocked(command.name)) {
|
if (userConfig.isBlocked(command.name)) {
|
||||||
throw IllegalArgumentException("User with this name is not acceptable")
|
throw BlockedValueException(target = "User", field = "name")
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = userRepository.findById(id) ?: throw IllegalArgumentException("User with id $id not found")
|
var user = userRepository.findById(id) ?: throw EntityNotFoundException(entity = "User", id = id.toString())
|
||||||
|
|
||||||
user = user.copy(name = command.name)
|
user = user.copy(name = command.name)
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ class UserService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun delete(id: UUID) {
|
override fun delete(id: UUID) {
|
||||||
userRepository.findById(id) ?: throw IllegalArgumentException("User with id $id not found")
|
userRepository.findById(id) ?: throw EntityNotFoundException(entity = "User", id = id.toString())
|
||||||
|
|
||||||
userRepository.deleteById(id)
|
userRepository.deleteById(id)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user