Skip to content

Commit 4f30449

Browse files
committed
+
1 parent 863f2dc commit 4f30449

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/main/java/com/example/controller/BookController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class BookController {
2323
@Inject
2424
private BookService service;
2525

26-
@Get
26+
@Get("/dto")
2727
public List<BookDTO> books() {
2828
return service.findAll();
2929
}
@@ -43,6 +43,11 @@ public Long count() {
4343
return service.countBooks();
4444
}
4545

46+
@Get("/pages")
47+
public Long pages(){
48+
return service.sumPages();
49+
}
50+
4651
@Post
4752
public HttpMessage create(Book book) {
4853
service.create(book);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.controller.api;
2+
3+
public final class URlConstants {
4+
5+
public URlConstants() throws IllegalAccessException {
6+
throw new IllegalAccessException("it is just for constants");
7+
}
8+
9+
private static final String BOOKS = "books";
10+
private static final String TITLE = "title";
11+
private static final String MODEL = "model";
12+
private static final String INIT = "init";
13+
private static final String COUNT = "count";
14+
}

src/main/java/com/example/repository/BookRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.micronaut.data.repository.CrudRepository;
1111

1212
import java.util.List;
13+
import java.util.Optional;
1314

1415
@Repository
1516
public interface BookRepository extends CrudRepository<Book, Long>, JpaSpecificationExecutor<Book> {
@@ -24,5 +25,9 @@ public interface BookRepository extends CrudRepository<Book, Long>, JpaSpecifica
2425
@EntityGraph(attributePaths = {"author", "title"})
2526
List<Book> findAll();
2627

28+
Optional<Integer> findPagesById(Long id);
29+
30+
Long findSumPages();
31+
2732
List<BookDTO> find();
2833
}

src/main/java/com/example/service/BookService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.NoSuchElementException;
1313
import java.util.Optional;
14+
import java.util.stream.Collectors;
1415

1516
@Primary
1617
@Singleton
@@ -60,6 +61,24 @@ public Book fall() {
6061

6162
}
6263

64+
@Transactional
65+
public Long totalPages(List<Long> id){
66+
67+
return id.stream()
68+
.map(this::getPages)
69+
.collect(Collectors.summarizingLong(Integer::intValue))
70+
.getCount();
71+
}
72+
73+
@Transactional
74+
public Integer getPages(Long bookId){
75+
return repository.findPagesById(bookId).orElse(0);
76+
}
77+
78+
@Transactional
79+
public Long sumPages(){
80+
return repository.findSumPages();
81+
}
6382
@Transactional
6483
public void create(Book book) {
6584
repository.save(book);

src/main/resources/db/liquibase-changelog.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,4 @@
7878
</sql>
7979
</changeSet>
8080

81-
8281
</databaseChangeLog>

0 commit comments

Comments
 (0)