Skip to content

Commit

Permalink
refactor: improve queries & read data from loader
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhhaudev committed Nov 7, 2024
1 parent df04760 commit ad9b9ed
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 60 deletions.
17 changes: 6 additions & 11 deletions src/datastore/postgres/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@ func (a *authorRepository) FindByID(ctx context.Context, id int) (*model.Author,
return &author, nil
}

func (a *authorRepository) FindByIDs(ctx context.Context, ids []int) ([]*model.Author, error) {
func (a *authorRepository) GetByIDs(ctx context.Context, ids []int) ([]*model.Author, error) {
var authors []*model.Author
if err := a.db.WithContext(ctx).Preload("Books").Where("id IN (?)", ids).Find(&authors).Error; err != nil {
if err := a.db.WithContext(ctx).Preload("Books", func(db *gorm.DB) *gorm.DB {
return db.Order("created_at DESC")
}).Where("id IN (?)", ids).
Order("created_at DESC").
Find(&authors).Error; err != nil {
return nil, err
}

return authors, nil
}

func (a *authorRepository) FindAuthorsByBookID(ctx context.Context, bookID int) ([]*model.Author, error) {
var book model.Book
if err := a.db.WithContext(ctx).Preload("Authors").First(&book, bookID).Error; err != nil {
return nil, err
}

return book.Authors, nil
}

func (a *authorRepository) Create(ctx context.Context, model *model.Author) (*model.Author, error) {
if err := a.db.WithContext(ctx).Create(model).Error; err != nil {
return nil, err
Expand Down
12 changes: 2 additions & 10 deletions src/datastore/postgres/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres

import (
"context"

"github.com/thanhhaudev/go-graphql/src/graph/model"
"github.com/thanhhaudev/go-graphql/src/repository"
"gorm.io/gorm"
Expand All @@ -11,7 +12,7 @@ type bookRepository struct {
db *gorm.DB
}

func (b *bookRepository) FindByIDs(ctx context.Context, ids []int) ([]*model.Book, error) {
func (b *bookRepository) GetByIDs(ctx context.Context, ids []int) ([]*model.Book, error) {
var books []*model.Book
if err := b.db.WithContext(ctx).Preload("Authors").Find(&books, ids).Error; err != nil {
return nil, err
Expand All @@ -20,15 +21,6 @@ func (b *bookRepository) FindByIDs(ctx context.Context, ids []int) ([]*model.Boo
return books, nil
}

func (b *bookRepository) FindBooksByAuthorID(ctx context.Context, authorID int) ([]*model.Book, error) {
var author model.Author
if err := b.db.WithContext(ctx).Preload("Books").First(&author, authorID).Error; err != nil {
return nil, err
}

return author.Books, nil
}

func (b *bookRepository) GetAll(ctx context.Context) ([]*model.Book, error) {
var books []*model.Book
if err := b.db.WithContext(ctx).Find(&books).Error; err != nil {
Expand Down
22 changes: 7 additions & 15 deletions src/datastore/postgres/borrower.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,24 @@ func (b *borrowerRepository) GetAll(ctx context.Context) ([]*model.Borrower, err

func (b *borrowerRepository) FindByID(ctx context.Context, id int) (*model.Borrower, error) {
var borrower model.Borrower
if err := b.db.WithContext(ctx).Preload("Borrowed").First(&borrower, id).Error; err != nil {
if err := b.db.WithContext(ctx).Preload("Borrowed.Book").First(&borrower, id).Error; err != nil {
return nil, err
}

return &borrower, nil
}

func (b *borrowerRepository) FindByIDs(ctx context.Context, ids []int) ([]*model.Borrower, error) {
func (b *borrowerRepository) GetByIDs(ctx context.Context, ids []int) ([]*model.Borrower, error) {
var borrowers []*model.Borrower
if err := b.db.WithContext(ctx).Find(&borrowers, ids).Error; err != nil {
return nil, err
}

return borrowers, nil
}

func (b *borrowerRepository) FindBorrowerBooksByID(ctx context.Context, borrowerID int) ([]*model.BorrowerBook, error) {
var borrowerBooks []*model.BorrowerBook
if err := b.db.WithContext(ctx).
Preload("Book").
Where("borrower_id = ? AND status != ?", borrowerID, model.ReturnedStatus).
Find(&borrowerBooks).Error; err != nil {
Preload("Borrowed", func(db *gorm.DB) *gorm.DB {
return db.Joins("Book").Where("status = ?", model.BorrowedStatus)
}).
Find(&borrowers, ids).Error; err != nil {
return nil, err
}

return borrowerBooks, nil
return borrowers, nil
}

func (b *borrowerRepository) FindByTelNumber(ctx context.Context, telNumber string) (*model.Borrower, error) {
Expand Down
4 changes: 2 additions & 2 deletions src/graph/resolver/author.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/graph/resolver/book.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/loader/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type authorLoader struct {

// getAuthors implements a batch function that loads authors by IDs
func (a *authorLoader) getAuthors(ctx context.Context, ids []int) ([]*model.Author, []error) {
authors, err := a.authorRepository.FindByIDs(ctx, ids)
authors, err := a.authorRepository.GetByIDs(ctx, ids)
if err != nil {
return nil, []error{err}
}
Expand Down
2 changes: 1 addition & 1 deletion src/loader/borrower.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type borrowerLoader struct {

// getBorrowers implements a batch function that loads borrowers by IDs
func (b *borrowerLoader) getBorrowers(ctx context.Context, ids []int) ([]*model.Borrower, []error) {
borrowers, err := b.borrowerRepository.FindByIDs(ctx, ids)
borrowers, err := b.borrowerRepository.GetByIDs(ctx, ids)
if err != nil {
return nil, []error{err}
}
Expand Down
10 changes: 4 additions & 6 deletions src/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@ package repository

import (
"context"

"github.com/thanhhaudev/go-graphql/src/graph/model"
)

type BookRepository interface {
GetAll(ctx context.Context) ([]*model.Book, error)
GetByIDs(ctx context.Context, ids []int) ([]*model.Book, error)
FindByID(ctx context.Context, id int) (*model.Book, error)
FindByIDs(ctx context.Context, ids []int) ([]*model.Book, error)
FindBooksByAuthorID(ctx context.Context, authorID int) ([]*model.Book, error)
Create(ctx context.Context, model *model.Book) error
Update(ctx context.Context, model *model.Book) error
Delete(ctx context.Context, id int) error
}

type AuthorRepository interface {
GetAll(ctx context.Context) ([]*model.Author, error)
GetByIDs(ctx context.Context, ids []int) ([]*model.Author, error)
FindByID(ctx context.Context, id int) (*model.Author, error)
FindByIDs(ctx context.Context, ids []int) ([]*model.Author, error)
FindAuthorsByBookID(ctx context.Context, bookID int) ([]*model.Author, error)
Create(ctx context.Context, model *model.Author) (*model.Author, error)
Update(ctx context.Context, model *model.Author) (*model.Author, error)
Delete(ctx context.Context, id int) error
}

type BorrowerRepository interface {
GetAll(ctx context.Context) ([]*model.Borrower, error)
GetByIDs(ctx context.Context, ids []int) ([]*model.Borrower, error)
FindByID(ctx context.Context, id int) (*model.Borrower, error)
FindByIDs(ctx context.Context, ids []int) ([]*model.Borrower, error)
FindByTelNumber(ctx context.Context, telNumber string) (*model.Borrower, error)
FindBorrowerBooksByID(ctx context.Context, borrowerID int) ([]*model.BorrowerBook, error)
Create(ctx context.Context, model *model.Borrower) (*model.Borrower, error)
Update(ctx context.Context, model *model.Borrower) (*model.Borrower, error)
BorrowBook(ctx context.Context, borrower *model.Borrower, book *model.Book) error
Expand Down
12 changes: 9 additions & 3 deletions src/service/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/thanhhaudev/go-graphql/src/graph/model"
"github.com/thanhhaudev/go-graphql/src/loader"
"github.com/thanhhaudev/go-graphql/src/repository"
)

Expand All @@ -26,11 +27,16 @@ func (a *AuthorService) GetAll(ctx context.Context) ([]*model.Author, error) {

// FindByID returns an author by id
func (a *AuthorService) FindByID(ctx context.Context, id int) (*model.Author, error) {
return a.authorRepository.FindByID(ctx, id)
return loader.FindAuthor(ctx, id)
}

func (a *AuthorService) FindAuthorsByBookID(ctx context.Context, bookID int) ([]*model.Author, error) {
return a.authorRepository.FindAuthorsByBookID(ctx, bookID)
func (a *AuthorService) GetByBookID(ctx context.Context, bookID int) ([]*model.Author, error) {
book, err := loader.FindBook(ctx, bookID)
if err != nil {
return nil, err
}

return book.Authors, nil
}

func NewAuthorService(authorRepository repository.AuthorRepository) *AuthorService {
Expand Down
21 changes: 16 additions & 5 deletions src/service/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/thanhhaudev/go-graphql/src/graph/model"
"github.com/thanhhaudev/go-graphql/src/loader"
"github.com/thanhhaudev/go-graphql/src/repository"
)

Expand All @@ -18,7 +19,7 @@ func (b *BookService) Create(ctx context.Context, input *model.CreateBookInput)
return nil, err
}

authors, err := b.authorRepository.FindByIDs(ctx, input.AuthorIds)
authors, err := b.authorRepository.GetByIDs(ctx, input.AuthorIds)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -48,7 +49,7 @@ func (b *BookService) Update(ctx context.Context, id int, input *model.UpdateBoo
}

if input.AuthorIds != nil && len(input.AuthorIds) > 0 {
authors, err := b.authorRepository.FindByIDs(ctx, input.AuthorIds)
authors, err := b.authorRepository.GetByIDs(ctx, input.AuthorIds)
if err != nil {
return nil, err
}
Expand All @@ -74,11 +75,21 @@ func (b *BookService) GetAll(ctx context.Context) ([]*model.Book, error) {
}

func (b *BookService) FindByID(ctx context.Context, id int) (*model.Book, error) {
return b.bookRepository.FindByID(ctx, id)
book, err := loader.FindBook(ctx, id)
if err != nil {
return nil, err
}

return book, nil
}

func (b *BookService) FindBooksByAuthorID(ctx context.Context, authorID int) ([]*model.Book, error) {
return b.bookRepository.FindBooksByAuthorID(ctx, authorID)
func (b *BookService) GetByAuthorID(ctx context.Context, authorID int) ([]*model.Book, error) {
author, err := loader.FindAuthor(ctx, authorID)
if err != nil {
return nil, err
}

return author.Books, nil
}

func NewBookService(b repository.BookRepository, au repository.AuthorRepository) *BookService {
Expand Down
11 changes: 6 additions & 5 deletions src/service/borrower.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/thanhhaudev/go-graphql/src/graph/model"
"github.com/thanhhaudev/go-graphql/src/loader"
"github.com/thanhhaudev/go-graphql/src/repository"
"gorm.io/datatypes"
)
Expand Down Expand Up @@ -47,18 +48,18 @@ func (b *BorrowerService) GetAll(ctx context.Context) ([]*model.Borrower, error)
}

func (b *BorrowerService) FindByID(ctx context.Context, id int) (*model.Borrower, error) {
return b.borrowerRepository.FindByID(ctx, id)
return loader.FindBorrower(ctx, id)
}

func (b *BorrowerService) FindBooksByID(ctx context.Context, borrowerID int) ([]*model.Book, error) {
borrowed, err := b.borrowerRepository.FindBorrowerBooksByID(ctx, borrowerID)
borrower, err := loader.FindBorrower(ctx, borrowerID)
if err != nil {
return nil, err
}

books := make([]*model.Book, 0, len(borrowed))
for _, bb := range borrowed {
books = append(books, bb.Book)
books := make([]*model.Book, len(borrower.Borrowed))
for i, bb := range borrower.Borrowed {
books[i] = bb.Book
}

return books, nil
Expand Down

0 comments on commit ad9b9ed

Please sign in to comment.