-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |