класс избранное репозиторий и сервис
This commit is contained in:
parent
58581e731b
commit
fa6fbd2323
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.backend.favorites.repository;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import com.example.backend.core.repository.MapRepository;
|
||||||
|
import com.example.backend.favorites.model.FavoriteEntity;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class FavoriteRepository extends MapRepository<FavoriteEntity> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.example.backend.favorites.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.example.backend.core.errors.NotFoundException;
|
||||||
|
import com.example.backend.favorites.model.FavoriteEntity;
|
||||||
|
import com.example.backend.favorites.repository.FavoriteRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FavoriteService {
|
||||||
|
private final FavoriteRepository repository;
|
||||||
|
|
||||||
|
public FavoriteService(FavoriteRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<FavoriteEntity> getAll(Integer userId) {
|
||||||
|
if (Objects.equals(userId, 0)) {
|
||||||
|
return repository.getAll();
|
||||||
|
}
|
||||||
|
return repository.getAll().stream()
|
||||||
|
.filter(item -> item.getUser().getId().equals(userId))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FavoriteEntity get(Integer id) {
|
||||||
|
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public FavoriteEntity create(FavoriteEntity entity) {
|
||||||
|
return repository.create(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FavoriteEntity update(Integer id, FavoriteEntity entity) {
|
||||||
|
final FavoriteEntity exisEntity = get(id);
|
||||||
|
exisEntity.setUser(entity.getUser());
|
||||||
|
exisEntity.setMovie(entity.getMovie());
|
||||||
|
return repository.update(exisEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FavoriteEntity delete(Integer id) {
|
||||||
|
final FavoriteEntity exisEntity = get(id);
|
||||||
|
return repository.delete(exisEntity);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user