Skip to content

Commit 67a2042

Browse files
feat: finish implementation of the service layer
1 parent 371ed9d commit 67a2042

12 files changed

Lines changed: 391 additions & 10 deletions

src/main/java/com/vianavitor/simplelibrarygame/model/BookSummary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public void setId(Long id) {
3232
this.id = id;
3333
}
3434

35-
public Student getUser() {
35+
public Student getStudent() {
3636
return student;
3737
}
3838

39-
public void setUser(Student student) {
39+
public void setStudent(Student student) {
4040
this.student = student;
4141
}
4242

src/main/java/com/vianavitor/simplelibrarygame/model/GroupOfBook.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ public void setId(GroupOfBookId id) {
3131
this.id = id;
3232
}
3333

34-
public Group getGroups() {
34+
public Group getGroup() {
3535
return group;
3636
}
3737

38-
public void setGroups(Group group) {
38+
public void setGroup(Group group) {
3939
this.group = group;
4040
}
4141

42-
public Book getBooks() {
42+
public Book getBook() {
4343
return book;
4444
}
4545

46-
public void setBooks(Book book) {
46+
public void setBook(Book book) {
4747
this.book = book;
4848
}
4949

src/main/java/com/vianavitor/simplelibrarygame/model/utils/fields/GroupOfBookId.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import jakarta.persistence.Column;
44
import jakarta.persistence.Embeddable;
5+
import org.springframework.beans.factory.annotation.Autowired;
56

67
import java.io.Serializable;
8+
import java.util.Objects;
79

810
@Embeddable
911
public class GroupOfBookId implements Serializable {
@@ -12,4 +14,41 @@ public class GroupOfBookId implements Serializable {
1214

1315
@Column(name = "book_id")
1416
private Long bookId;
17+
18+
public Long getGroupId() {
19+
return groupId;
20+
}
21+
22+
public void setGroupId(Long groupId) {
23+
this.groupId = groupId;
24+
}
25+
26+
public Long getBookId() {
27+
return bookId;
28+
}
29+
30+
public void setBookId(Long bookId) {
31+
this.bookId = bookId;
32+
}
33+
34+
@Autowired
35+
public GroupOfBookId() {
36+
}
37+
38+
public GroupOfBookId(Long groupId, Long bookId) {
39+
this.groupId = groupId;
40+
this.bookId = bookId;
41+
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (o == null || getClass() != o.getClass()) return false;
46+
GroupOfBookId that = (GroupOfBookId) o;
47+
return Objects.equals(groupId, that.groupId) && Objects.equals(bookId, that.bookId);
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
return Objects.hash(groupId, bookId);
53+
}
1554
}

src/main/java/com/vianavitor/simplelibrarygame/repository/BookReadHistoryRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import org.springframework.data.repository.CrudRepository;
77
import org.springframework.stereotype.Repository;
88

9+
import java.util.List;
10+
911
@Repository
1012
public interface BookReadHistoryRepository extends CrudRepository<BookReadHistory, Long> {
11-
void findByBook(Book book);
13+
List<BookReadHistory> findByBook(Book book);
1214

13-
void findByStudent(Student student);
15+
List<BookReadHistory> findByStudent(Student student);
1416
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.vianavitor.simplelibrarygame.service;
2+
3+
import com.vianavitor.simplelibrarygame.model.Author;
4+
import com.vianavitor.simplelibrarygame.model.Book;
5+
import com.vianavitor.simplelibrarygame.repository.AuthorRepository;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
public class AuthorService {
12+
private AuthorRepository repository;
13+
14+
public void add(String name) {
15+
repository.findByName(name)
16+
.ifPresent((a) -> {
17+
throw new RuntimeException("author already registered");
18+
});
19+
20+
Author author = new Author();
21+
author.setName(name);
22+
23+
repository.save(author);
24+
}
25+
26+
public Author getByName(String name) {
27+
return repository.findByName(name)
28+
.orElseThrow(() -> new RuntimeException("not found author"));
29+
}
30+
31+
public List<Author> getAll() {
32+
return (List<Author>) repository.findAll();
33+
}
34+
35+
public Author get(Long id) {
36+
return repository.findById(id)
37+
.orElseThrow(() -> new RuntimeException("not found author"));
38+
}
39+
40+
public List<Book> getAuthorBooks(Long id) {
41+
return repository.findById(id)
42+
.orElseThrow(() -> new RuntimeException("not found author"))
43+
.getBooks();
44+
}
45+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.vianavitor.simplelibrarygame.service;
2+
3+
import com.vianavitor.simplelibrarygame.model.Book;
4+
import com.vianavitor.simplelibrarygame.model.BookReadHistory;
5+
import com.vianavitor.simplelibrarygame.model.Student;
6+
import com.vianavitor.simplelibrarygame.repository.BookReadHistoryRepository;
7+
import com.vianavitor.simplelibrarygame.repository.BookRepository;
8+
import com.vianavitor.simplelibrarygame.repository.StudentRepository;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.time.LocalDate;
13+
import java.util.List;
14+
import java.util.Objects;
15+
import java.util.Optional;
16+
17+
@Service
18+
public class BookReadHistoryService {
19+
@Autowired
20+
private BookReadHistoryRepository repository;
21+
22+
@Autowired
23+
private BookRepository bookRepository;
24+
25+
@Autowired
26+
private StudentRepository studentRepository;
27+
28+
public void register(BookReadHistory data) {
29+
Book book = bookRepository.findById(data.getBook().getId())
30+
.orElseThrow(() -> new RuntimeException("not found book"));
31+
32+
Student student = studentRepository.findById(data.getUser().getId())
33+
.orElseThrow(() -> new RuntimeException("not found student"));
34+
35+
Optional<BookReadHistory> result = repository.findByStudent(student)
36+
.stream()
37+
// check if the student has already read this book before...
38+
.filter((s) ->
39+
Objects.equals(s.getBook().getId(), book.getId())
40+
).findFirst();
41+
42+
if (result.isEmpty()) {
43+
data.setBook(book);
44+
data.setUser(student);
45+
} else {
46+
data = result.get();
47+
}
48+
49+
LocalDate now = LocalDate.now();
50+
data.setLastUpdate(now);
51+
52+
repository.save(data);
53+
}
54+
55+
public List<BookReadHistory> getByStudent(Long studentId) {
56+
Student student = studentRepository.findById(studentId)
57+
.orElseThrow(() -> new RuntimeException("not found student"));
58+
59+
return repository.findByStudent(student);
60+
}
61+
62+
public List<BookReadHistory> getByBook(Long bookId) {
63+
Book book = bookRepository.findById(bookId)
64+
.orElseThrow(() -> new RuntimeException("not found book"));
65+
66+
return repository.findByBook(book);
67+
}
68+
69+
public BookReadHistory getByStudentTheLastOne(Long studentId) {
70+
Student student = studentRepository.findById(studentId)
71+
.orElseThrow(() -> new RuntimeException("not found student"));
72+
73+
return repository.findByStudent(student)
74+
.stream()
75+
.findFirst()
76+
.orElseGet(BookReadHistory::new);
77+
}
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.vianavitor.simplelibrarygame.service;
2+
3+
import com.vianavitor.simplelibrarygame.model.Book;
4+
import com.vianavitor.simplelibrarygame.model.BookSummary;
5+
import com.vianavitor.simplelibrarygame.model.Student;
6+
import com.vianavitor.simplelibrarygame.repository.BookRepository;
7+
import com.vianavitor.simplelibrarygame.repository.BookSummaryRepository;
8+
import com.vianavitor.simplelibrarygame.repository.StudentRepository;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.List;
13+
14+
@Service
15+
public class BookSummaryService {
16+
17+
@Autowired
18+
private BookSummaryRepository repository;
19+
20+
@Autowired
21+
private BookRepository bookRepository;
22+
23+
@Autowired
24+
private StudentRepository studentRepository;
25+
26+
public void submit(String text, Long bookId, Long studentId) {
27+
Book book = bookRepository.findById(bookId)
28+
.orElseThrow(() -> new RuntimeException("not found book"));
29+
30+
Student student = studentRepository.findById(studentId)
31+
.orElseThrow(() -> new RuntimeException("student not found"));
32+
33+
BookSummary summary = new BookSummary();
34+
summary.setBook(book);
35+
summary.setStudent(student);
36+
37+
repository.save(summary);
38+
}
39+
40+
public List<BookSummary> getByStudent(Long studentId) {
41+
Student student = studentRepository.findById(studentId)
42+
.orElseThrow(() -> new RuntimeException("student not found"));
43+
44+
return repository.findByStudent(student);
45+
}
46+
47+
public List<BookSummary> getByBook(Long bookId) {
48+
Book book = bookRepository.findById(bookId)
49+
.orElseThrow(() -> new RuntimeException("book not found"));
50+
51+
return repository.findByBook(book);
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.vianavitor.simplelibrarygame.service;
2+
3+
import com.vianavitor.simplelibrarygame.model.Genre;
4+
import com.vianavitor.simplelibrarygame.repository.GenreRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
@Service
11+
public class GenreService {
12+
@Autowired
13+
private GenreRepository repository;
14+
15+
public List<Genre> getAll() {
16+
return repository.findAll();
17+
}
18+
19+
public Genre getById(Long id) {
20+
return repository.findById(id)
21+
.orElseThrow(() -> new RuntimeException("this genre don't exists"));
22+
}
23+
24+
public Genre getByName(String name) {
25+
return repository.findByName(name)
26+
.orElseThrow(() -> new RuntimeException("this genre don't exists"));
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.vianavitor.simplelibrarygame.service;
2+
3+
import com.vianavitor.simplelibrarygame.model.Book;
4+
import com.vianavitor.simplelibrarygame.model.Group;
5+
import com.vianavitor.simplelibrarygame.model.GroupOfBook;
6+
import com.vianavitor.simplelibrarygame.model.utils.fields.GroupOfBookId;
7+
import com.vianavitor.simplelibrarygame.repository.BookRepository;
8+
import com.vianavitor.simplelibrarygame.repository.GroupOfBookRepository;
9+
import com.vianavitor.simplelibrarygame.repository.GroupRepository;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.stereotype.Service;
12+
13+
@Service
14+
public class GroupOfBookService {
15+
@Autowired
16+
private GroupOfBookRepository repository;
17+
18+
@Autowired
19+
private BookRepository bookRepository;
20+
21+
@Autowired
22+
private GroupRepository groupRepository;
23+
24+
public void addBookToGroup(Long groupId, Long bookId) {
25+
Group group = groupRepository.findById(groupId)
26+
.orElseThrow(() -> new RuntimeException("not found group"));
27+
28+
Book book = bookRepository.findById(bookId)
29+
.orElseThrow(() -> new RuntimeException("not found book"));
30+
31+
GroupOfBookId id = new GroupOfBookId(groupId, bookId);
32+
repository.findById(id)
33+
.ifPresent((x) -> {
34+
throw new RuntimeException("book already saved into the group");
35+
});
36+
37+
GroupOfBook groupOfBook = new GroupOfBook();
38+
groupOfBook.setBook(book);
39+
groupOfBook.setGroup(group);
40+
41+
repository.save(groupOfBook);
42+
}
43+
44+
public void removeBookFromGroup(Long groupId, Long bookId) {
45+
GroupOfBookId id = new GroupOfBookId(groupId, bookId);
46+
GroupOfBook groupOfBook = repository.findById(id)
47+
.orElseThrow(() -> new RuntimeException("this book is not registered in the group"));
48+
49+
repository.delete(groupOfBook);
50+
}
51+
}

0 commit comments

Comments
 (0)