Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

Add series issue 6 #81

Closed
wants to merge 68 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
0686385
Add new entity representing series of books.
kev711 Mar 11, 2021
c5d2ca8
Add new entity mapping series of books and books with a serial order …
kev711 Mar 11, 2021
3590469
Add bookSeriesMapping to Book
kev711 Mar 11, 2021
b8aed12
Add copyright text
kev711 Mar 14, 2021
e21db5a
Update BookSeries entity's member "seriesName"
kev711 Mar 14, 2021
75ccd9d
Update BookSeriesMapping entity - Unique constraint, Relationship fie…
kev711 Mar 14, 2021
e0718d1
Add tests for entities Book Series Mapping and Book Series.
kev711 Mar 14, 2021
2af638c
Add boilerplate code for entity repositories.
kev711 Mar 14, 2021
eee400e
Add repository methods for Book, BookSeriesMapping
kev711 Mar 21, 2021
370dc0b
Add test classes for BookSeries & BookSeriesMapping repository
kev711 Mar 21, 2021
d10d705
Add new tests for Book repository
kev711 Mar 21, 2021
8309e31
Update setup method to clear tables with Foreign Key dependencies first
kev711 Mar 21, 2021
fe14f80
Fix test issues due to merge
kev711 Mar 21, 2021
d238142
Remove wildcard imports & adhere to 100 char limit
knjk04 Mar 25, 2021
2208799
Extract create book code to method
knjk04 Mar 25, 2021
ca46272
Extract create book code to method
knjk04 Mar 25, 2021
8dc9835
Add missing copyright notice
knjk04 Mar 25, 2021
9b37d9d
Extract create book code to method
knjk04 Mar 25, 2021
d0cde5c
Find by title
knjk04 Mar 25, 2021
254c14b
Extract duplicate code to method
knjk04 Mar 25, 2021
c9cbb82
Add delete book mutation
knjk04 Mar 27, 2021
03299dd
Merge pull request #83 from Project-Books/delete-book-mutation-82
knjk04 Mar 27, 2021
1688866
Resolve for PR-81 comments
kev711 Mar 27, 2021
5ba90da
Add DDL for Flyway migration.
kev711 Mar 29, 2021
b9e9305
Add publisher entity
knjk04 Mar 30, 2021
071b289
Update expired Slack link
knjk04 Mar 30, 2021
cbf5b70
Add migrations
knjk04 Mar 30, 2021
88cec9c
Rename publisher_book foreign key constraints
knjk04 Mar 30, 2021
c7f17ff
Merge pull request #86 from Project-Books/publisher-entity-66
knjk04 Mar 30, 2021
67774cd
Update sample query
knjk04 Mar 30, 2021
f527576
Make the Author the owner side of the many-to-many relationship
knjk04 Mar 31, 2021
dae9e4b
Extract common create book code to new method
knjk04 Mar 31, 2021
59e45fc
Fix failing tests
knjk04 Mar 31, 2021
fba2ad4
Make Author take in a set of books
knjk04 Mar 31, 2021
20c7494
Add Author with an empty set in mutation
knjk04 Mar 31, 2021
8a875ab
Merge pull request #89 from Project-Books/author-owner-80
knjk04 Mar 31, 2021
9eabd49
Update sample query
knjk04 Mar 31, 2021
4fa6954
Add removeBooks() method
knjk04 Mar 31, 2021
15cdecc
Set ddl auto to create in dev setup
knjk04 Mar 31, 2021
caa2dad
Merge pull request #91 from Project-Books/fix-multiple-authors-saved-75
knjk04 Mar 31, 2021
ed1c2bd
Create stale.yml
knjk04 Mar 31, 2021
123b6f5
Merge pull request #96 from Project-Books/follow-up-pr-95
knjk04 Mar 31, 2021
ea26737
Remove publishername graphql file
knjk04 Mar 31, 2021
9fc3b12
Add missing Publisher schema file
knjk04 Mar 31, 2021
11057c4
Add new entity representing series of books.
kev711 Mar 11, 2021
a293078
Add new entity mapping series of books and books with a serial order …
kev711 Mar 11, 2021
11ff6e4
Rebase onto main
kev711 Mar 11, 2021
c3826fb
Add copyright text
kev711 Mar 14, 2021
ebebbd5
Update BookSeries entity's member "seriesName"
kev711 Mar 14, 2021
789e0bd
Update BookSeriesMapping entity - Unique constraint, Relationship fie…
kev711 Mar 14, 2021
58ebd53
Add tests for entities Book Series Mapping and Book Series.
kev711 Mar 14, 2021
89248b0
Add boilerplate code for entity repositories.
kev711 Mar 14, 2021
fe11c63
Add repository methods for Book, BookSeriesMapping
kev711 Mar 21, 2021
5b7dc1c
Add test classes for BookSeries & BookSeriesMapping repository
kev711 Mar 21, 2021
346f344
Rebase onto main
kev711 Mar 21, 2021
84a2e53
Update setup method to clear tables with Foreign Key dependencies first
kev711 Mar 21, 2021
42a6622
Fix test issues due to merge
kev711 Mar 21, 2021
a78fc25
Remove wildcard imports & adhere to 100 char limit
knjk04 Mar 25, 2021
b62559f
Extract create book code to method
knjk04 Mar 25, 2021
7863aad
Extract create book code to method
knjk04 Mar 25, 2021
4327b42
Add missing copyright notice
knjk04 Mar 25, 2021
06ec459
Extract create book code to method
knjk04 Mar 25, 2021
4866d66
Find by title
knjk04 Mar 25, 2021
bfcc19c
Extract duplicate code to method
knjk04 Mar 25, 2021
f4fedf8
Resolve for PR-81 comments
kev711 Mar 27, 2021
d76c2d5
Add DDL for Flyway migration.
kev711 Mar 29, 2021
e36fc6e
Update tests to new model
kev711 Apr 14, 2021
0e04fdb
Merge remote-tracking branch 'origin/add-series-issue-6' into add-ser…
kev711 Apr 19, 2021
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
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<img src="https://codecov.io/gh/Project-Books/books-api/branch/main/graph/badge.svg?token=5OF8MVDZW7"/>
</a>

<a href="https://join.slack.com/t/teambookproject/shared_invite/zt-lvr8oi47-8BjZzov0QDsx4RvjkNjOcA">
<a href="https://join.slack.com/t/teambookproject/shared_invite/zt-ojsgamu9-O4v~714y6G1_0o7HtTZDqw">
<img src="https://img.shields.io/badge/slack-teambookproject-4A154B?logo=slack" alt="Slack" />
</a>

Expand All @@ -35,22 +35,21 @@ Recommended IntelliJ plugin: [JS GraphQL](https://plugins.jetbrains.com/plugin/8

1. Import as a Gradle project into your favourite IDE
2. Start the MySQL Database or run the docker-compose file `docker-compose up -d` (you may need to add `sudo` to this command)
3. Run `BooksApiApplication.java`
4. Go to `localhost:8080/graphiql`
3. Set the active Spring profile to dev (see how to do this in [IntelliJ](https://github.com/Project-Books/books-api/wiki/Change-active-Spring-profile-in-IntelliJ))
4. Run `BooksApiApplication.java`
5. Go to `localhost:8080/graphiql`

Sample query:
```graphql
{
findAllBooks {
title
authors {
firstName
lastName
fullName
}
genre
isbn13
yearOfPublication
publisher
format
}
}
Expand Down
71 changes: 39 additions & 32 deletions src/main/java/com/karankumar/booksapi/BooksApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import com.karankumar.booksapi.model.BookFormat;
import com.karankumar.booksapi.model.BookGenre;
import com.karankumar.booksapi.model.Language;
import com.karankumar.booksapi.model.PublisherName;
import com.karankumar.booksapi.model.Publisher;
import com.karankumar.booksapi.repository.AuthorRepository;
import com.karankumar.booksapi.repository.BookRepository;
import com.karankumar.booksapi.repository.PublisherRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;
import java.util.Set;

@SpringBootApplication
public class BooksApiApplication {
public static void main(String[] args) {
Expand All @@ -36,53 +40,56 @@ public static void main(String[] args) {

@Bean
public CommandLineRunner populateData(AuthorRepository authorRepository,
BookRepository bookRepository) {
BookRepository bookRepository,
PublisherRepository publisherRepository) {
return args -> {
Author author = new Author("J.K. Rowling");
author.setAbout("A fantastic author");
authorRepository.save(author);
Publisher publisher = new Publisher("Bloomsbury");
publisherRepository.save(publisher);

Book book1 = new Book(
Book book1 = createBook(
"Harry Potter and the Philosopher's stone",
new Author[] {author},
Language.ENGLISH,
"Philosopher's stone blurb",
BookGenre.FANTASY,
BookFormat.PAPERBACK
1997,
"9781408810545"
);
book1.setYearOfPublication(1997);
book1.setIsbn13("9781408810545");
book1.setPublisher(PublisherName.BLOOMSBURY);
bookRepository.save(book1);
publisher.addBook(book1);

Book book2 = new Book(
Book book2 = createBook(
"Harry Potter and the Chamber of Secrets",
new Author[] {author},
Language.ENGLISH,
"Chamber of secrets blurb",
BookGenre.FANTASY,
BookFormat.PAPERBACK
1998,
"1234567898765"
);
book2.setIsbn13("1234567898765");
book2.setGenre(BookGenre.FANTASY);
book2.setPublisher(PublisherName.BLOOMSBURY);
book2.setFormat(BookFormat.PAPERBACK);
bookRepository.save(book2);

Author author2 = new Author("J.R.R. Tolkien");
author2.setAbout("Another fantastic author");
authorRepository.save(author2);
Book book3 = new Book(
Book book3 = createBook(
"The Hobbit",
new Author[] {author, author2},
Language.ENGLISH,
"Hobbit blurb",
BookGenre.FANTASY,
BookFormat.PAPERBACK
1937,
"1234567898761"
);
book3.setYearOfPublication(1937);
book3.setPublisher(PublisherName.HARPER_COLLINS);
bookRepository.save(book3);
Author author2 = new Author("J.R.R. Tolkien", Set.of(book3));
author2.setAbout("Another fantastic author");
authorRepository.save(author2);

Author author = new Author("J.K. Rowling", Set.of(book1, book2, book3));
author.setAbout("A fantastic author");
authorRepository.save(author);
};
}

private Book createBook(String title, String blurb, int yearOfPublication, String isbn13) {
Book book = new Book(
title,
Language.ENGLISH,
blurb,
BookGenre.FANTASY,
BookFormat.PAPERBACK
);
book.setYearOfPublication(yearOfPublication);
book.setIsbn13(isbn13);
return book;
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/karankumar/booksapi/model/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ public class Author {

private String about;

public Author(@NonNull String fullName) {
public Author(@NonNull String fullName, @NonNull Set<Book> books) {
this.fullName = fullName;
books.forEach(this::addBook);
}

public void addBook(@NonNull Book book) {
books.add(book);
book.getAuthors().add(this);
}

public void removeBook(@NonNull Book book) {
books.remove(book);
book.getAuthors().remove(this);
}
}
18 changes: 9 additions & 9 deletions src/main/java/com/karankumar/booksapi/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
Expand Down Expand Up @@ -70,23 +72,21 @@ public class Book {
@Column(nullable = false)
private String blurb;

private PublisherName publisher;
@ManyToMany(mappedBy = "books", fetch = FetchType.LAZY)
private Set<Publisher> publishers = new HashSet<>();

@Column(nullable = false)
private BookFormat format;

public Book(@NonNull String title, @NonNull Author[] authors, @NonNull Language language,
@NonNull String blurb, @NonNull BookGenre genre, @NonNull BookFormat format) {
@OneToMany(mappedBy = "book")
private List<BookSeriesMapping> bookSeriesMapping;

public Book(@NonNull String title, @NonNull Language language, @NonNull String blurb,
@NonNull BookGenre genre, @NonNull BookFormat format) {
this.title = title;
Arrays.stream(authors).forEach(this::addAuthor);
this.language = language;
this.blurb = blurb;
this.genre = genre;
this.format = format;
}

public void addAuthor(@NonNull Author author) {
authors.add(author);
author.getBooks().add(this);
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/karankumar/booksapi/model/BookSeries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 Karan Kumar
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.

* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

package com.karankumar.booksapi.model;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.NonNull;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookSeries {
knjk04 marked this conversation as resolved.
Show resolved Hide resolved
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

@Column(nullable = false, name = "series_name")
private String seriesName;

@OneToMany(mappedBy = "bookSeries")
private List<BookSeriesMapping> bookSeriesMapping;

public BookSeries(@NonNull String seriesName){
this.seriesName = seriesName;
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/karankumar/booksapi/model/BookSeriesMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
The book project lets a user keep track of different books they would like to read, are currently reading, have read or did not finish.
Copyright (C) 2020 Karan Kumar

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
*/

package com.karankumar.booksapi.model;

import lombok.*;

import javax.persistence.*;

@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(
name="Book_To_BookSeries_Mapping",
uniqueConstraints = {
@UniqueConstraint(
name = "UK_bookSeriesId_bookId",
columnNames = {"book_series_id","book_id"}
),
@UniqueConstraint(
name = "UK_bookSeriesId_serialNumber",
columnNames = {"book_series_id","serial_number"}
)
}
)
public class BookSeriesMapping {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

@ManyToOne(optional = false)
private BookSeries bookSeries;

@ManyToOne(optional = false)
private Book book;

@Column(nullable = false, name = "serial_number")
private Integer seriesPosition;

public BookSeriesMapping(@NonNull BookSeries bookSeries, @NonNull Book bookInSeries,
@NonNull Integer seriesPosition) {
this.bookSeries = bookSeries;
this.book = bookInSeries;
this.seriesPosition = seriesPosition;
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/karankumar/booksapi/model/Publisher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 Karan Kumar
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.

* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
package com.karankumar.booksapi.model;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import java.util.HashSet;
import java.util.Set;

@Entity
@Data
@EqualsAndHashCode()
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Publisher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.NONE)
private Long id;

private String name;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "publisher_book",
joinColumns = @JoinColumn(name = "publisher_id"),
inverseJoinColumns = @JoinColumn(name = "book_id")
)
private Set<Book> books = new HashSet<>();

public Publisher(@NonNull String name) {
this.name = name;
}

public void addBook(@NonNull Book book) {
books.add(book);
book.getPublishers().add(this);
}
}
Loading