Skip to content

Commit eae5bf1

Browse files
refactor: implement custom exceptions into the service layer
1 parent e40017c commit eae5bf1

13 files changed

Lines changed: 153 additions & 136 deletions

src/main/java/com/vianavitor/simplelibrarygame/service/AdministratorService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.vianavitor.simplelibrarygame.service;
22

33
import com.vianavitor.simplelibrarygame.dto.utils.UserInfo;
4+
import com.vianavitor.simplelibrarygame.exception.InvalidOperationException;
5+
import com.vianavitor.simplelibrarygame.exception.UserDeactivatedException;
46
import com.vianavitor.simplelibrarygame.model.Administrator;
57
import com.vianavitor.simplelibrarygame.repository.AdministratorRepository;
68
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,19 +19,19 @@ public class AdministratorService {
1719
@Autowired
1820
private PasswordEncoder encoder;
1921

20-
public Long login(String username, String password) {
22+
public Long login(String username, String password) throws InvalidOperationException, UserDeactivatedException{
2123
Administrator administrator = (Administrator) repository.findByUsername(username)
22-
.orElseThrow(() -> new RuntimeException("invalid username or password"));
24+
.orElseThrow(() -> new InvalidOperationException("invalid username or password"));
2325

2426
boolean invalidPassword = !encoder.matches(password, administrator.getPassword());
2527

2628
if (invalidPassword) {
27-
throw new RuntimeException("invalid username or password");
29+
throw new InvalidOperationException("invalid username or password");
2830
}
2931

3032
boolean wasUserDeactivated = !administrator.isActive();
3133
if (wasUserDeactivated) {
32-
throw new RuntimeException("this user was deactivated, talk with a professor or administrador to get more information");
34+
throw new UserDeactivatedException("this user was deactivated, talk with a professor or administrador to get more information");
3335
}
3436

3537
// TODO: create JWT Token for authentication

src/main/java/com/vianavitor/simplelibrarygame/service/AuthorService.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.DuplicateResourceException;
4+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
35
import com.vianavitor.simplelibrarygame.model.Author;
46
import com.vianavitor.simplelibrarygame.model.Book;
57
import com.vianavitor.simplelibrarygame.repository.AuthorRepository;
@@ -23,34 +25,34 @@ public AuthorService(AuthorRepository repository, Author author) {
2325
this.author = author;
2426
}
2527

26-
public void add(String name) {
28+
public void add(String name) throws DuplicateResourceException {
2729
repository.findByName(name)
2830
.ifPresent((a) -> {
29-
throw new RuntimeException("author already registered");
31+
throw new DuplicateResourceException("author already registered");
3032
});
3133

3234
author.setName(name);
3335

3436
repository.save(author);
3537
}
3638

37-
public Author getByName(String name) {
39+
public Author getByName(String name) throws ResourceNotFoundException {
3840
return repository.findByName(name)
39-
.orElseThrow(() -> new RuntimeException("not found author"));
41+
.orElseThrow(() -> new ResourceNotFoundException("not found author"));
4042
}
4143

4244
public List<Author> getAll() {
4345
return (List<Author>) repository.findAll();
4446
}
4547

46-
public Author get(Long id) {
48+
public Author get(Long id) throws ResourceNotFoundException {
4749
return repository.findById(id)
48-
.orElseThrow(() -> new RuntimeException("not found author"));
50+
.orElseThrow(() -> new ResourceNotFoundException("not found author"));
4951
}
5052

5153
public List<Book> getAuthorBooks(Long id) {
5254
return repository.findById(id)
53-
.orElseThrow(() -> new RuntimeException("not found author"))
55+
.orElseThrow(() -> new ResourceNotFoundException("not found author"))
5456
.getBooks();
5557
}
5658
}

src/main/java/com/vianavitor/simplelibrarygame/service/BookReadHistoryService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
34
import com.vianavitor.simplelibrarygame.model.Book;
45
import com.vianavitor.simplelibrarygame.model.BookReadHistory;
56
import com.vianavitor.simplelibrarygame.model.Student;
@@ -30,12 +31,12 @@ public class BookReadHistoryService {
3031
@Autowired
3132
private StudentStatsRepository statsRepository;
3233

33-
public void register(BookReadHistory data) {
34+
public void register(BookReadHistory data) throws ResourceNotFoundException{
3435
Book book = bookRepository.findById(data.getBook().getId())
35-
.orElseThrow(() -> new RuntimeException("not found book"));
36+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
3637

3738
Student student = studentRepository.findById(data.getUser().getId())
38-
.orElseThrow(() -> new RuntimeException("not found student"));
39+
.orElseThrow(() -> new ResourceNotFoundException("not found student"));
3940

4041
Optional<BookReadHistory> result = repository.findByStudent(student)
4142
.stream()
@@ -62,23 +63,23 @@ public void register(BookReadHistory data) {
6263
statsRepository.save(student.getStats());
6364
}
6465

65-
public List<BookReadHistory> getByStudent(Long studentId) {
66+
public List<BookReadHistory> getByStudent(Long studentId) throws ResourceNotFoundException {
6667
Student student = studentRepository.findById(studentId)
67-
.orElseThrow(() -> new RuntimeException("not found student"));
68+
.orElseThrow(() -> new ResourceNotFoundException("not found student"));
6869

6970
return repository.findByStudent(student);
7071
}
7172

72-
public List<BookReadHistory> getByBook(Long bookId) {
73+
public List<BookReadHistory> getByBook(Long bookId) throws ResourceNotFoundException {
7374
Book book = bookRepository.findById(bookId)
74-
.orElseThrow(() -> new RuntimeException("not found book"));
75+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
7576

7677
return repository.findByBook(book);
7778
}
7879

79-
public BookReadHistory getByStudentTheLastOne(Long studentId) {
80+
public BookReadHistory getByStudentTheLastOne(Long studentId) throws ResourceNotFoundException {
8081
Student student = studentRepository.findById(studentId)
81-
.orElseThrow(() -> new RuntimeException("not found student"));
82+
.orElseThrow(() -> new ResourceNotFoundException("not found student"));
8283

8384
AtomicReference<BookReadHistory> lastOne = new AtomicReference<>();
8485
repository.findByStudent(student)

src/main/java/com/vianavitor/simplelibrarygame/service/BookService.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.DuplicateResourceException;
4+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
5+
import com.vianavitor.simplelibrarygame.exception.UnconfirmedOperationException;
6+
import com.vianavitor.simplelibrarygame.exception.UnsupportedFileTypeException;
37
import com.vianavitor.simplelibrarygame.model.Author;
48
import com.vianavitor.simplelibrarygame.model.Book;
59
import com.vianavitor.simplelibrarygame.model.Genre;
@@ -52,11 +56,11 @@ public BookService(
5256
this.imagePath = imagePath;
5357
}
5458

55-
public void add(Book newBook, boolean confirmed) {
59+
public void add(Book newBook, boolean confirmed) throws UnconfirmedOperationException {
5660
if (!confirmed) {
5761
boolean exists = repository.existsByTitle(newBook.getTitle());
5862
if (exists) {
59-
throw new RuntimeException("there is a book with the same title registered, do you still want to proceed?");
63+
throw new UnconfirmedOperationException("there is a book with the same title registered, do you still want to proceed?");
6064
}
6165
}
6266

@@ -71,9 +75,9 @@ public void add(Book newBook, boolean confirmed) {
7175
Book book = repository.save(newBook);
7276
}
7377

74-
public Book changeImage(Long id, MultipartFile file) throws IOException {
78+
public Book changeImage(Long id, MultipartFile file) throws ResourceNotFoundException, IOException {
7579
Book book = repository.findById(id)
76-
.orElseThrow(() -> new RuntimeException("not found book"));
80+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
7781

7882
File directory = new File(imagePath + "/");
7983
File destination = getFile(file, directory, book);
@@ -85,7 +89,7 @@ public Book changeImage(Long id, MultipartFile file) throws IOException {
8589
return repository.save(book);
8690
}
8791

88-
private static @NonNull File getFile(MultipartFile file, File directory, Book book) throws IOException {
92+
private static @NonNull File getFile(MultipartFile file, File directory, Book book) throws UnsupportedFileTypeException {
8993
if (!directory.exists()) {
9094
directory.mkdirs(); // Ensure directory exists
9195
}
@@ -97,7 +101,7 @@ public Book changeImage(Long id, MultipartFile file) throws IOException {
97101
String splited[] = file.getContentType().split("/");
98102

99103
if (splited.length <= 1) {
100-
throw new RuntimeException("invalid file content type format");
104+
throw new UnsupportedFileTypeException("invalid file content type format");
101105
}
102106

103107
String extension = splited[1];
@@ -106,15 +110,15 @@ public Book changeImage(Long id, MultipartFile file) throws IOException {
106110
case "jpeg", "png", "gif", "bmp":
107111
break;
108112
default:
109-
throw new IOException("unsupported image type: " + extension);
113+
throw new UnsupportedFileTypeException("unsupported image type: " + extension);
110114
}
111115

112116
return new File(directory, book.getId().toString() + "." + extension);
113117
}
114118

115-
public void rate(Long id, int rate) {
119+
public void rate(Long id, int rate) throws ResourceNotFoundException{
116120
Book book = repository.findById(id)
117-
.orElseThrow(() -> new RuntimeException("not found book"));
121+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
118122

119123
double value = book.getRatingValue();
120124
int count = book.getRatingCount();
@@ -127,20 +131,20 @@ public void rate(Long id, int rate) {
127131
repository.save(book);
128132
}
129133

130-
public void setAvailable(Long id, boolean value) {
134+
public void setAvailable(Long id, boolean value) throws ResourceNotFoundException {
131135
Book book = repository.findById(id)
132-
.orElseThrow(() -> new RuntimeException("not found book"));
136+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
133137

134138
book.setAvailable(value);
135139
repository.save(book);
136140
}
137141

138-
public Book modify(Long id, Book data, boolean confirmed, Map<Long, Book> cache) {
142+
public Book modify(Long id, Book data, boolean confirmed, Map<Long, Book> cache) throws ResourceNotFoundException, DuplicateResourceException {
139143
// TODO: implement a more efficient way to stores a cache to deal with no long accessed data
140144
Book book = cache.containsKey(id)
141145
? cache.remove(id)
142146
: repository.findById(id)
143-
.orElseThrow(() -> new RuntimeException("not found book"));
147+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
144148

145149
if (!cache.containsKey(id)) {
146150
cache.put(id, book);
@@ -159,7 +163,7 @@ public Book modify(Long id, Book data, boolean confirmed, Map<Long, Book> cache)
159163
.findFirst();
160164

161165
if (duplicateBookTitle.isPresent()) {
162-
throw new RuntimeException("there is a book with the same title registered, even so do you wish to proceed?");
166+
throw new DuplicateResourceException("there is a book with the same title registered, even so do you wish to proceed?");
163167
}
164168
}
165169
}

src/main/java/com/vianavitor/simplelibrarygame/service/BookSummaryService.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
34
import com.vianavitor.simplelibrarygame.model.Book;
45
import com.vianavitor.simplelibrarygame.model.BookSummary;
56
import com.vianavitor.simplelibrarygame.model.Student;
@@ -23,12 +24,12 @@ public class BookSummaryService {
2324
@Autowired
2425
private StudentRepository studentRepository;
2526

26-
public void submit(String text, Long bookId, Long studentId) {
27+
public void submit(String text, Long bookId, Long studentId) throws ResourceNotFoundException {
2728
Book book = bookRepository.findById(bookId)
28-
.orElseThrow(() -> new RuntimeException("not found book"));
29+
.orElseThrow(() -> new ResourceNotFoundException("not found book"));
2930

3031
Student student = studentRepository.findById(studentId)
31-
.orElseThrow(() -> new RuntimeException("student not found"));
32+
.orElseThrow(() -> new ResourceNotFoundException("student not found"));
3233

3334
BookSummary summary = new BookSummary();
3435
summary.setBook(book);
@@ -37,16 +38,16 @@ public void submit(String text, Long bookId, Long studentId) {
3738
repository.save(summary);
3839
}
3940

40-
public List<BookSummary> getByStudent(Long studentId) {
41+
public List<BookSummary> getByStudent(Long studentId) throws ResourceNotFoundException {
4142
Student student = studentRepository.findById(studentId)
42-
.orElseThrow(() -> new RuntimeException("student not found"));
43+
.orElseThrow(() -> new ResourceNotFoundException("student not found"));
4344

4445
return repository.findByStudent(student);
4546
}
4647

47-
public List<BookSummary> getByBook(Long bookId) {
48+
public List<BookSummary> getByBook(Long bookId) throws ResourceNotFoundException {
4849
Book book = bookRepository.findById(bookId)
49-
.orElseThrow(() -> new RuntimeException("book not found"));
50+
.orElseThrow(() -> new ResourceNotFoundException("book not found"));
5051

5152
return repository.findByBook(book);
5253
}

src/main/java/com/vianavitor/simplelibrarygame/service/ClassroomService.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.DuplicateResourceException;
4+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
35
import com.vianavitor.simplelibrarygame.model.Book;
46
import com.vianavitor.simplelibrarygame.model.Classroom;
57
import com.vianavitor.simplelibrarygame.model.Professor;
@@ -46,10 +48,10 @@ private String generatePublicCode() {
4648
return UUID.randomUUID().toString().replaceAll("-", "");
4749
}
4850

49-
public void create(String name) {
51+
public void create(String name) throws DuplicateResourceException {
5052
boolean exists = repository.findByName(name).isPresent();
5153
if (exists) {
52-
throw new RuntimeException("this classroom already exists");
54+
throw new DuplicateResourceException("this classroom already exists");
5355
}
5456

5557
classroom.setPublicCode(this.generatePublicCode());
@@ -58,31 +60,31 @@ public void create(String name) {
5860
repository.save(classroom);
5961
}
6062

61-
public Set<UserClassroom> modifyUsersInClassroom(Long id, Set<UserClassroom> students) {
63+
public Set<UserClassroom> modifyUsersInClassroom(Long id, Set<UserClassroom> students) throws ResourceNotFoundException {
6264
classroom = repository.findById(id)
63-
.orElseThrow(() -> new RuntimeException("classroom not found"));
65+
.orElseThrow(() -> new ResourceNotFoundException("classroom not found"));
6466

6567
classroom.setUsers(students);
6668
return repository.save(classroom).getUsers();
6769
}
6870

69-
public Classroom changeName(Long id, String name) {
71+
public Classroom changeName(Long id, String name) throws ResourceNotFoundException, DuplicateResourceException {
7072
classroom = repository.findById(id)
71-
.orElseThrow(() -> new RuntimeException("classroom not found"));
73+
.orElseThrow(() -> new ResourceNotFoundException("classroom not found"));
7274

7375
boolean exists = repository.findByName(name).isPresent();
7476
if (exists) {
75-
throw new RuntimeException("this classroom already exists");
77+
throw new DuplicateResourceException("this classroom already exists");
7678
}
7779

7880
classroom.setName(name);
7981

8082
return repository.save(classroom);
8183
}
8284

83-
public void delete(Long id) {
85+
public void delete(Long id) throws ResourceNotFoundException {
8486
classroom = repository.findById(id)
85-
.orElseThrow(() -> new RuntimeException("classroom not found"));
87+
.orElseThrow(() -> new ResourceNotFoundException("classroom not found"));
8688

8789
repository.delete(classroom);
8890
}

src/main/java/com/vianavitor/simplelibrarygame/service/GenreService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vianavitor.simplelibrarygame.service;
22

3+
import com.vianavitor.simplelibrarygame.exception.ResourceNotFoundException;
34
import com.vianavitor.simplelibrarygame.model.Genre;
45
import com.vianavitor.simplelibrarygame.repository.GenreRepository;
56
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,13 +17,13 @@ public List<Genre> getAll() {
1617
return repository.findAll();
1718
}
1819

19-
public Genre getById(Long id) {
20+
public Genre getById(Long id) throws ResourceNotFoundException {
2021
return repository.findById(id)
21-
.orElseThrow(() -> new RuntimeException("this genre don't exists"));
22+
.orElseThrow(() -> new ResourceNotFoundException("this genre don't exists"));
2223
}
2324

24-
public Genre getByName(String name) {
25+
public Genre getByName(String name) throws ResourceNotFoundException {
2526
return repository.findByName(name)
26-
.orElseThrow(() -> new RuntimeException("this genre don't exists"));
27+
.orElseThrow(() -> new ResourceNotFoundException("this genre don't exists"));
2728
}
2829
}

0 commit comments

Comments
 (0)