Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing classes #297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ then
build maven_module "spring-boot/spring-boot-camel"
build_maven_module "logging/structured-logging"
build_maven_module "spring-boot/zero-downtime"
build_maven_module "resilience4j/springboot-resilience4j"
build_maven_module "spring-boot/feature-flags"
build_gradle_module "aws/spring-cloud-caching-redis"
# build_maven_module "resilience4j/springboot-resilience4j"
# build_maven_module "spring-boot/feature-flags"
# build_gradle_module "aws/spring-cloud-caching-redis"
build_maven_module "logging/spring-boot"
build_maven_module "logging/logback"
build_maven_module "logging/log4j"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.reflectoring.beginnersguide;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BeginnersGuideApplication {

public static void main(String[] args) {
SpringApplication.run(BeginnersGuideApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.reflectoring.beginnersguide.controllers.dto;

public class BookResponse {
private long id;
private String title;
private String author;
private String publishedOn;
private long currentlyAvailableNumber;

private BookResponse(long id, String title, String author,
String publishedOn,
long currentlyAvailableNumber) {
this.id = id;
this.title = title;
this.author = author;
this.publishedOn = publishedOn;
this.currentlyAvailableNumber = currentlyAvailableNumber;
}

public long getId() {
return id;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public String getPublishedOn() {
return publishedOn;
}

public long getCurrentlyAvailableNumber() {
return currentlyAvailableNumber;
}

public static Builder builder(){
return new Builder();
}

public static class Builder{
private long id;
private String title;
private String author;
private String publishedOn;
private long currentlyAvailableNumber;

public Builder id(long id) {
this.id = id;
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder author(String author) {
this.author = author;
return this;
}

public Builder publishedOn(String publishedOn) {
this.publishedOn = publishedOn;
return this;
}

public Builder currentlyAvailableNumber(
long currentlyAvailableNumber) {
this.currentlyAvailableNumber = currentlyAvailableNumber;
return this;
}

public BookResponse build(){
return new BookResponse(id, title, author, publishedOn,
currentlyAvailableNumber);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.reflectoring.beginnersguide.domain;

import javax.persistence.*;
import java.sql.Date;
import java.util.List;

@Entity(name = "book")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
// @GeneratedValue( strategy = GenerationType.IDENTITY)
// @GeneratedValue(strategy = GenerationType.SEQUENCE,
// generator = "book_generator")
// @SequenceGenerator(name = "book_generator",
// sequenceName = "book_seq",
// initialValue = 10)
// @GeneratedValue(strategy = GenerationType.TABLE,
// generator = "book_generator")
// @TableGenerator(name = "book_generator", table = "book_id_table")
private long id;

@Column(name = "title")
private String title;

@Column(name = "author")
private String author;

@Column(name = "publication")
private Date publication;

@Column(name = "numberOfInstances")
private int numberOfInstances;

@ManyToMany(mappedBy = "borrowedBooks")
private List<User> users;

public Book() {
}

public Book(long id, String title, String author,
Date publication, int numberOfInstances,
List<User> users) {
this.id = id;
this.title = title;
this.author = author;
this.publication = publication;
this.numberOfInstances = numberOfInstances;
this.users = users;
}

public long getId() {
return id;
}

public Book id(long id) {
this.id = id;
return this;
}

public String getTitle() {
return title;
}

public Book title(String title) {
this.title = title;
return this;
}

public String getAuthor() {
return author;
}

public Book author(String author) {
this.author = author;
return this;
}

public Date getPublication() {
return publication;
}

public Book publication(Date publication) {
this.publication = publication;
return this;
}

public int getNumberOfInstances() {
return numberOfInstances;
}

public Book numberOfInstances(int numberOfInstances) {
this.numberOfInstances = numberOfInstances;
return this;
}

public List<User> getUsers() {
return users;
}

public Book users(
List<User> users) {
this.users = users;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.reflectoring.beginnersguide.repository;

import com.reflectoring.beginnersguide.domain.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface BookRepository extends JpaRepository<Book, Long> {

@Query(nativeQuery = true, value = "SELECT * FROM Book " +
"book WHERE book.currentlyAvailableNumber > 5 ")
List<Book> findWithMoreInstancesThenFive();

@Query( value = "SELECT b FROM book b where b.numberOfInstances > 5")
List<Book> findWithMoreInstancesThenFiveJPQL();

List<Book> findAllByNumberOfInstancesGreaterThan(long limit);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.reflectoring.beginnersguide.services;

import com.reflectoring.beginnersguide.controllers.dto.BookRequest;
import com.reflectoring.beginnersguide.controllers.dto.BookResponse;
import com.reflectoring.beginnersguide.domain.Book;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
class BookMapper {

BookResponse map(Book book){
return BookResponse.builder()
.id(book.getId())
.author(book.getAuthor())
.title(book.getTitle())
.currentlyAvailableNumber(
book.getNumberOfInstances())
.publishedOn(book.getPublication().toString())
.build();
}
List<BookResponse> mapAll(List<Book> books){
return books != null ? books.stream().map(this::map).collect(
Collectors.toList()) : new ArrayList<>();
}

Book map(BookRequest request){
return new Book(0L,request.getTitle(),
request.getAuthor(),
Date.valueOf(request.getPublishedOn()),
(int) request.getCurrentlyAvailableNumber(),
new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.reflectoring.beginnersguide.services;

import com.reflectoring.beginnersguide.repository.BookRepository;
import org.springframework.stereotype.Service;

@Service
public class DeleteBookService {

private final BookRepository bookRepository;

private final BookMapper bookMapper;

public DeleteBookService(BookRepository bookRepository,
BookMapper getBookMapper) {
this.bookRepository = bookRepository;
this.bookMapper = getBookMapper;
}

public void delete(long id){
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.reflectoring.beginnersguide.services;

import com.reflectoring.beginnersguide.controllers.dto.BookResponse;
import com.reflectoring.beginnersguide.repository.BookRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetBookService {

private final BookRepository bookRepository;

// @Autowired
// public GetBookService(BookRepository bookRepository) {
// this.bookRepository = bookRepository;
// }
private final BookMapper getBookMapper;

public GetBookService(BookRepository bookRepository,
BookMapper getBookMapper) {
this.bookRepository = bookRepository;
this.getBookMapper = getBookMapper;
}

// @Autowired
// private void setBookRepository(BookRepository bookRepository){
// this.bookRepository = bookRepository;
// }

public List<BookResponse> getAllBooks(){
return getBookMapper.mapAll(bookRepository.findAll());
}

public List<BookResponse> getAllBooksWhereInstancesGreaterThanFive(){
return getBookMapper.mapAll(bookRepository.findWithMoreInstancesThenFive());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.reflectoring.beginnersguide.services;

import com.reflectoring.beginnersguide.controllers.dto.BookRequest;
import com.reflectoring.beginnersguide.controllers.dto.BookResponse;
import com.reflectoring.beginnersguide.domain.Book;
import com.reflectoring.beginnersguide.domain.User;
import com.reflectoring.beginnersguide.repository.BookRepository;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;

@Service
public class UpdateBookService {
private final GetUserService getUserService;
private final BookRepository bookRepository;

public UpdateBookService(
GetUserService getUserService,
BookRepository bookRepository) {
this.getUserService = getUserService;
this.bookRepository = bookRepository;
}

public BookResponse updateBook(long id, BookRequest request){
return null;
}

public void borrow(long bookId, long userId){
User user = getUserService.getById(userId);
if(user.getBorrowedBooks().stream().anyMatch(book -> book.getId()== bookId)){
throw new IllegalStateException("User already borrowed " +
"the book");
}
Book book =
bookRepository.findById(bookId).orElseThrow(() -> new EntityNotFoundException());

if(book.getNumberOfInstances()-1 < 0){
throw new IllegalStateException("There are no available" +
" books!");
}
book.getUsers().add(user);
book.numberOfInstances(book.getNumberOfInstances()+1);
bookRepository.save(book);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BegginersGuideApplicationTests {
class BeginnersGuideApplicationTests {

@Test
void contextLoads() {
Expand Down
Loading