Skip to content

Commit

Permalink
implement service
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashChauhan51 committed Jul 26, 2024
1 parent d8db8e6 commit 907c6f5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/books_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// @Success 200 {array} models.Book
// @Router /books [get]
func GetBooks(c *gin.Context) {
books, err := services.FetchBooksAsync()
books, err := services.BookService.FetchBooksAsync()

Check failure on line 17 in internal/controllers/books_controller.go

View workflow job for this annotation

GitHub Actions / build

invalid method expression services.BookService.FetchBooksAsync (needs pointer receiver (*services.BookService).FetchBooksAsync)

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down
2 changes: 1 addition & 1 deletion internal/core/interfaces/services/book_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package interfaces
import "github.com/VikashChauhan51/go-sample-api/internal/dto"

type BookService interface {
FetchBooksAsync() (*[]dto.Book, error)
FetchBooksAsync() ([]dto.Book, error)
}
39 changes: 24 additions & 15 deletions internal/infra/services/book_service.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package services

import (
"time"

repo "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/repositories"
svc "github.com/VikashChauhan51/go-sample-api/internal/core/interfaces/services"
"github.com/VikashChauhan51/go-sample-api/internal/dto"
)

// Simulate fetching books from an external API
func FetchBooksAsync() ([]dto.Book, error) {
type BookService struct {
bookRepository repo.BookRepository
}

r := make(chan []dto.Book)
go func() {
time.Sleep(2 * time.Second) // Simulate external API call
books := []dto.Book{
{ID: 1, Title: "The Lord of the Rings", Author: "J. R. R. Tolkien"},
{ID: 2, Title: "Pride and Prejudice", Author: "Jane Austen"},
{ID: 3, Title: "To Kill a Mockingbird", Author: "Harper Lee"},
}
r <- books // Send book list to channel
func NewBookService(bookRepository repo.BookRepository) svc.BookService {
return &BookService{bookRepository}
}

}()
result := <-r
func (b *BookService) FetchBooksAsync() ([]dto.Book, error) {

books, err := b.bookRepository.FetchBooksAsync()
if err != nil {
return nil, err
}

result := make([]dto.Book, len(*books))
// map result
for index, val := range *books {
result[index] = dto.Book{
ID: val.ID,
Title: val.Title,
Author: val.Author,
}
}
return result, nil

}
15 changes: 15 additions & 0 deletions test/mocks/book_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mocks

import (
"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
"github.com/stretchr/testify/mock"
)

type BookRepository struct {
mock.Mock
}

func (m *BookRepository) FetchBooksAsync() (*[]entities.Book, error) {
args := m.Called()
return args.Get(0).(*[]entities.Book), args.Error(1)
}
4 changes: 2 additions & 2 deletions test/repositories/book_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestFetchBooksAsync(t *testing.T) {
mockDB := new(mocks.MockDB)
repo := repo.NewBookRepository(mockDB)
bookRepository := repo.NewBookRepository(mockDB)

expectedBooks := []entities.Book{
{ID: 1, Title: "Title1", Author: "Author1"},
Expand All @@ -26,7 +26,7 @@ func TestFetchBooksAsync(t *testing.T) {
*arg = expectedBooks
}).Return(&gorm.DB{Error: nil}).Once()

result, err := repo.FetchBooksAsync()
result, err := bookRepository.FetchBooksAsync()

assert.NoError(t, err)
assert.NotNil(t, result)
Expand Down
30 changes: 30 additions & 0 deletions test/services/book_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package services

import (
"testing"

"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
svc "github.com/VikashChauhan51/go-sample-api/internal/infra/services"
"github.com/VikashChauhan51/go-sample-api/test/mocks"

"github.com/stretchr/testify/assert"
)

func TestFetchBooksAsync(t *testing.T) {
bookRepository := new(mocks.BookRepository)
bookService := svc.NewBookService(bookRepository)

expectedBooks := []entities.Book{
{ID: 1, Title: "Title1", Author: "Author1"},
{ID: 2, Title: "Title2", Author: "Author2"},
}

bookRepository.On("FetchBooksAsync").Return(&expectedBooks, nil)
result, err := bookService.FetchBooksAsync()

assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, len(expectedBooks), len(result))

bookRepository.AssertExpectations(t)
}

0 comments on commit 907c6f5

Please sign in to comment.