Skip to content

Commit

Permalink
Create books services
Browse files Browse the repository at this point in the history
  • Loading branch information
meyiapir committed Mar 10, 2023
1 parent 92d70da commit 0b8f3f0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
Expand Down Expand Up @@ -354,6 +356,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
Expand Down
19 changes: 19 additions & 0 deletions pkg/handler/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package handler

import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

type errorResponse struct {
Message string `json:"message"`
}

type statusResponse struct {
Status string `json:"status"`
}

func newErrorResponse(c *gin.Context, statusCode int, message string) {
logrus.Error(message)
c.AbortWithStatusJSON(statusCode, errorResponse{message})
}
62 changes: 62 additions & 0 deletions pkg/repository/books_postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package repository

import (
"booksApi"
"github.com/jmoiron/sqlx"
)

type BooksPostgres struct {
db *sqlx.DB
}

func NewBookPostgres(db *sqlx.DB) *BooksPostgres {
return &BooksPostgres{db: db}
}

func (r *BooksPostgres) GetAll() ([]booksApi.Book, error) {
var books []booksApi.Book
query := "SELECT * FROM books"
err := r.db.Select(&books, query)
if err != nil {
return nil, err
}
return books, nil
}

func (r *BooksPostgres) GetById(id int) (booksApi.Book, error) {
var book booksApi.Book
query := "SELECT * FROM books WHERE id = $1"
err := r.db.Get(&book, query, id)
if err != nil {
return booksApi.Book{}, err
}
return book, nil
}

func (r *BooksPostgres) Create(input booksApi.Book) (int, error) {
query := "INSERT INTO books (title, author, year) VALUES ($1, $2, $3) RETURNING id"
var id int
err := r.db.Get(&id, query, input.Title, input.Author, input.Year)
if err != nil {
return 0, err
}
return id, nil
}

func (r *BooksPostgres) Update(id int, input booksApi.UpdateBookInput) error {
query := "UPDATE books SET title = $1, author = $2, year = $3 WHERE id = $4"
_, err := r.db.Exec(query, input.Title, input.Author, input.Year, id)
if err != nil {
return err
}
return nil
}

func (r *BooksPostgres) Delete(id int) error {
query := "DELETE FROM books WHERE id = $1"
_, err := r.db.Exec(query, id)
if err != nil {
return err
}
return nil
}
46 changes: 46 additions & 0 deletions pkg/service/bookService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package service

import (
"booksApi"
"booksApi/pkg/repository"
)

type BookService struct {
repo repository.Book
}

func NewBookService(repo repository.Book) *BookService {
return &BookService{repo: repo}
}

func (s *BookService) GetById(id int) (booksApi.Book, error) {
book, err := s.repo.GetById(id)
if err != nil {
return booksApi.Book{}, err
}
return book, nil
}

func (s *BookService) Create(book booksApi.Book) (int, error) {
id, err := s.repo.Create(book)
if err != nil {
return 0, err
}
return id, nil
}

func (s *BookService) Update(id int, input booksApi.UpdateBookInput) error {
err := s.repo.Update(id, input)
if err != nil {
return err
}
return nil
}

func (s *BookService) Delete(id int) error {
err := s.repo.Delete(id)
if err != nil {
return err
}
return nil
}
22 changes: 22 additions & 0 deletions pkg/service/booksService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package service

import (
"booksApi"
"booksApi/pkg/repository"
)

type BooksService struct {
repo repository.Books
}

func NewBooksService(repo repository.Books) *BooksService {
return &BooksService{repo: repo}
}

func (s *BooksService) GetAll() ([]booksApi.Book, error) {
books, err := s.repo.GetAll()
if err != nil {
return nil, err
}
return books, nil
}

0 comments on commit 0b8f3f0

Please sign in to comment.