-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoryServiceImpl.java
More file actions
54 lines (41 loc) · 1.68 KB
/
CategoryServiceImpl.java
File metadata and controls
54 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.mehmetpekdemir.librarymanagementsystem.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.mehmetpekdemir.librarymanagementsystem.entity.Category;
import com.mehmetpekdemir.librarymanagementsystem.exception.NotFoundException;
import com.mehmetpekdemir.librarymanagementsystem.repository.CategoryRepository;
import com.mehmetpekdemir.librarymanagementsystem.service.CategoryService;
@Service
public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;
public CategoryServiceImpl(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Override
public List<Category> findAllCategories() {
return categoryRepository.findAll();
}
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Override
public Category findCategoryById(Long id) {
return categoryRepository.findById(id)
.orElseThrow(() -> new NotFoundException(String.format("Category not found with ID %d", id)));
}
@Override
public void createCategory(Category category) {
categoryRepository.save(category);
}
@Override
public void updateCategory(Category category) {
categoryRepository.save(category);
}
@Override
public void deleteCategory(Long id) {
final Category category = categoryRepository.findById(id)
.orElseThrow(() -> new NotFoundException(String.format("Category not found with ID %d", id)));
categoryRepository.deleteById(category.getId());
}
}