Skip to content

Commit

Permalink
SD/feat/#32 Added Controller, Service, Model, DTO classes. Added flyw…
Browse files Browse the repository at this point in the history
…ay Book migration. Changed package name typo from chohort2d to cohort2d.
  • Loading branch information
sarajaned authored and JaafarF committed Jul 4, 2023
1 parent 7eedecd commit ca1e8b4
Show file tree
Hide file tree
Showing 22 changed files with 585 additions and 31 deletions.
34 changes: 32 additions & 2 deletions online-book-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.amigoscode.chohort2d</groupId>
<groupId>com.amigoscode.cohort2d</groupId>
<artifactId>online-book-store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>online-book-store</name>
<description>Online book store</description>
<properties>
<java.version>17</java.version>
<testcontainers.version>1.17.6</testcontainers.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -43,6 +44,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<optional>true</optional>
</dependency>
<dependency>
Expand All @@ -65,8 +67,17 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -141,6 +152,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.amigoscode.cohort2d.onlinebookstore;

import com.amigoscode.cohort2d.onlinebookstore.book.BookDemoData;
import com.amigoscode.cohort2d.onlinebookstore.book.BookRepository;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class OnlineBookStoreApplication {

public static void main(String[] args) {
SpringApplication.run(OnlineBookStoreApplication.class, args);
}

@Bean
public CommandLineRunner createBookDemoData(BookRepository bookRepository, BookDemoData bookDemoData) {
return args -> {
if (bookRepository.count() == 0) {

bookRepository.saveAll(bookDemoData.generateData());

}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.amigoscode.cohort2d.onlinebookstore.book;


import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Book {

@Id
@SequenceGenerator(
name = "book_id_seq",
sequenceName = "book_id_seq",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "book_id_seq"
)
@NotNull
private Long id;


@NotNull
@Column(unique=true)
private String isbn;

@NotNull
private String title;

@NotNull
private String description;

@NotNull
private BigDecimal price;

@NotNull
private Integer numberOfPages;

@NotNull
private Integer quantity;

@NotNull
@Column(columnDefinition = "LOCALDATE")
private LocalDate publishDate;

@NotNull
@Enumerated(EnumType.STRING)
private BookFormat bookFormat;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/v1/books")
public class BookController {

private final BookService bookService;

@GetMapping()
public List<BookDTO> getBooks() {
return bookService.getAllBooks();
}

@GetMapping("{id}")
private BookDTO getBookById(@PathVariable("id") int id){
return bookService.getBookById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import java.util.List;
import java.util.Optional;

public interface BookDAO {

List<Book> findAllBooks();

Optional<Book> findById(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import java.math.BigDecimal;
import java.time.LocalDate;

public record BookDTO (
Long id,
String isbn,
String title,
String description,
BigDecimal price,
Integer numberOfPages,
Integer quantity,
LocalDate publishDate,
String bookFormat
){

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.amigoscode.cohort2d.onlinebookstore.book;


import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper(
componentModel = "spring"
)
public interface BookDTOMapper {

BookDTOMapper INSTANCE = Mappers.getMapper( BookDTOMapper.class );

BookDTO modelToDTO(Book book);

List<BookDTO> modelToDTO(Iterable<Book> books);

Book dtoToModel(BookDTO bookDTO);

List<Book> dtoToModel(Iterable<BookDTO> bookDTOS);

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import com.github.javafaker.Faker;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class BookDemoData {

private final Faker faker = new Faker();

public List<Book> generateData(){
int count = 20;

List<Book> books = new ArrayList<>();

for (int i = 0; i < count; i++) {

Book book = new Book();

book.setTitle(faker.book().title());
book.setIsbn(faker.code().isbn13());
book.setDescription(faker.book().publisher());
book.setPrice(new BigDecimal("19.99"));
book.setNumberOfPages(faker.number().numberBetween(10, 1000));
book.setQuantity(faker.number().numberBetween(1, 250));

Date date = faker.date().birthday();
book.setPublishDate(date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate());


// Alternate between Digital and physical formats when creating demo book entries
if (i % 2 == 0) {
book.setBookFormat(BookFormat.DIGITAL);
} else {
book.setBookFormat(BookFormat.PHYSICAL);
}

books.add(book);
}
return books;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

public enum BookFormat {
DIGITAL,
PHYSICAL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Repository("jpa")
public class BookJPAService implements BookDAO {

private final BookRepository bookRepository;

@Override
public List<Book> findAllBooks() {
return bookRepository.findAll();
}

@Override
public Optional<Book> findById(int id) throws ResourceNotFoundException {
return bookRepository.findById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Integer> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.amigoscode.cohort2d.onlinebookstore.book;

import com.amigoscode.cohort2d.onlinebookstore.exceptions.ResourceNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Qualifier(value = "jpa")
public class BookService {

private final BookDAO bookDAO;
private final BookDTOMapper bookDTOMapper;

public List<BookDTO> getAllBooks() {
return bookDTOMapper.modelToDTO(bookDAO.findAllBooks());
}

public BookDTO getBookById(int id){
return bookDTOMapper.modelToDTO(
bookDAO.findById(id)
.orElseThrow(
() -> new ResourceNotFoundException("Book with id [%s] not found".formatted(id))
));
}
}
Loading

0 comments on commit ca1e8b4

Please sign in to comment.