diff --git a/dbconfig.yml b/dbconfig.yml index 3980d13..793ea78 100644 --- a/dbconfig.yml +++ b/dbconfig.yml @@ -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 diff --git a/go.mod b/go.mod index 2ccecca..20dcbae 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3f4c83e..644b471 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/application/handlers.go b/internal/application/handlers.go index 4097172..fee9fe6 100644 --- a/internal/application/handlers.go +++ b/internal/application/handlers.go @@ -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) } diff --git a/internal/application/server.go b/internal/application/server.go index 9022166..6750e6a 100644 --- a/internal/application/server.go +++ b/internal/application/server.go @@ -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 { diff --git a/internal/books/data/storage.go b/internal/books/data/storage.go index 630be0d..aeeb9e2 100644 --- a/internal/books/data/storage.go +++ b/internal/books/data/storage.go @@ -21,19 +21,13 @@ 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 } @@ -41,7 +35,7 @@ func (b BookDataAccess) Create(book models.Books) (string, error) { 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 diff --git a/internal/books/models/models.go b/internal/books/models/models.go index a2d2f54..070ba47 100644 --- a/internal/books/models/models.go +++ b/internal/books/models/models.go @@ -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"` diff --git a/main.go b/main.go index 2279855..1f4989f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/migrations/postgres/20240926223942-init.sql b/migrations/postgres/20240926223942-init.sql new file mode 100644 index 0000000..444d836 --- /dev/null +++ b/migrations/postgres/20240926223942-init.sql @@ -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; diff --git a/migrations/sqlite3/20240906050249-init.sql b/migrations/sqlite3/20240906050249-init.sql index db0e80e..bbdbc41 100644 --- a/migrations/sqlite3/20240906050249-init.sql +++ b/migrations/sqlite3/20240906050249-init.sql @@ -1,7 +1,6 @@ - -- +migrate Up -CREATE TABLE books( +CREATE TABLE IF NOT EXISTS books( id INTEGER PRIMARY KEY AUTOINCREMENT, uuid TEXT, isbn TEXT,