Skip to content

Commit

Permalink
Merge pull request #1 from Edmartt/edmartt/feat/create-books
Browse files Browse the repository at this point in the history
Edmartt/feat/create books
Edmartt authored Sep 27, 2024
2 parents 96b8297 + 7fed8ca commit 1818779
Showing 10 changed files with 57 additions and 17 deletions.
4 changes: 2 additions & 2 deletions dbconfig.yml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ development:
datasource: test.db
dir: migrations/sqlite3

postgres:
production:
dialect: postgres
datasource: host=${PG_HOST} dbname=${PG_DB} user=${PG_USER} password=${PG_PASSWORD} port=${PORT}sslmode=disable
datasource: sslmode=disable dbname=${PG_DB} user=${PG_USER} password=${PG_PASSWORD} port=${PORT} host=${PG_HOST}
dir: migrations/postgres
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqw
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
26 changes: 25 additions & 1 deletion internal/application/handlers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package application

import (
"log"
"net/http"

"github.com/edmartt/bookstatic-book-service/internal/books/data"
"github.com/edmartt/bookstatic-book-service/internal/books/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

type HTTPHandler struct {
@@ -37,7 +40,28 @@ func (h HTTPHandler) ReadBook(context *gin.Context) {
context.JSON(http.StatusOK, book)
}

func (h HTTPHandler) createBook(context *gin.Context) {
func (h HTTPHandler) CreateBook(context *gin.Context) {
book := models.Books{}
book.UUID = uuid.NewString()

err := context.BindJSON(&book)
jsonResponse := httpResponse{}

if err != nil {
jsonResponse.Response = "bad request"
context.JSON(http.StatusBadRequest, jsonResponse)
return
}

dbResponse, err := h.bookRepository.Create(book)

if err != nil {
log.Println(err.Error())
context.JSON(http.StatusBadRequest, err.Error())
return
}

context.JSON(http.StatusCreated, dbResponse)

}

3 changes: 2 additions & 1 deletion internal/application/server.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ type HTTPServer struct {
}

func (h HTTPServer) setBookRoutes(router *gin.RouterGroup) {
router.GET("/book/:id", h.Handler.ReadBook)
router.GET("/books/:id", h.Handler.ReadBook)
router.POST("/books", h.Handler.CreateBook)
}

func (h HTTPServer) setRouter() *gin.Engine {
12 changes: 3 additions & 9 deletions internal/books/data/storage.go
Original file line number Diff line number Diff line change
@@ -21,27 +21,21 @@ func NewRepository(db database.IDBConnection) *BookDataAccess {
func (b BookDataAccess) Create(book models.Books) (string, error) {
conn := b.db.GetConnection()

dbResponse, err := conn.NamedExec("INSERT INTO books (id, isbn, title, pages, current_page, author, year, status) VALUES(:id, :isbn, :title, :pages, :current_page, :author, :year, :status)", &book)
_, err := conn.NamedExec("INSERT INTO books (uuid, isbn, title, pages, current_page, author, year, status) VALUES(:uuid, :isbn, :title, :pages, :current_page, :author, :year, :status)", &book)

if err != nil {
return "", err
}

lastID, err := dbResponse.LastInsertId()

if err != nil {
return "", err
}

bookData := fmt.Sprintf("%d %s %s", lastID, book.ISBN, book.Title)
bookData := fmt.Sprintf("%s %s %s", book.UUID, book.ISBN, book.Title)

return bookData, nil
}

func (b BookDataAccess) Read(id string) (*models.Books, error) {
conn := b.db.GetConnection()

err := conn.Get(&b.book, "SELECT isbn, title, pages, current_page, author, year, status FROM books WHERE id = ?", id)
err := conn.Get(&b.book, "SELECT uuid, isbn, title, pages, current_page, author, year, status FROM books WHERE uuid = ?", id)

if err != nil {
return nil, err
2 changes: 1 addition & 1 deletion internal/books/models/models.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ const (

// Books model
type Books struct {
ID string `json:"id" db:"id"`
UUID string `db:"uuid"`
ISBN string `json:"isbn" db:"isbn"`
Title string `json:"title" db:"title"`
Pages string `json:"pages" db:"pages"`
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ func main() {
log.Fatalf("ERROR ENV LOAD: %v", err)
}

dbConnectObject := database.SQLite{}
dbConnectObject := database.Postgres{}
getConn := dbConnectObject.GetConnection()

database.PingDB(getConn)
19 changes: 19 additions & 0 deletions migrations/postgres/20240926223942-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

-- +migrate Up

CREATE TABLE IF NOT EXISTS books(
id SERIAL PRIMARY KEY,
uuid character varying(30),
isbn character varying(30),
title TEXT,
pages character varying(30),
current_page character varying(30),
author TEXT,
year character varying(30),
status TEXT CHECK (status IN ('read', 'to_be_read', 'reading', 'pending_purchase'))

);

-- +migrate Down

DROP TABLE books;
3 changes: 1 addition & 2 deletions migrations/sqlite3/20240906050249-init.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

-- +migrate Up

CREATE TABLE books(
CREATE TABLE IF NOT EXISTS books(
id INTEGER PRIMARY KEY AUTOINCREMENT,
uuid TEXT,
isbn TEXT,

0 comments on commit 1818779

Please sign in to comment.