Added GetProduct, GetProducts, UpdateProduct, DeleteProduct
parent
ca00c36e25
commit
2bbef5ad4e
@ -1,4 +1,8 @@
|
|||||||
|
# build, generated files
|
||||||
dist
|
dist
|
||||||
.env
|
|
||||||
.air.toml
|
|
||||||
tmp/
|
tmp/
|
||||||
|
.air.toml
|
||||||
|
|
||||||
|
|
||||||
|
# environment variable files
|
||||||
|
app.env
|
@ -1,15 +0,0 @@
|
|||||||
POSTGRES_HOST=127.0.0.1
|
|
||||||
POSTGRES_DB=shelf
|
|
||||||
POSTGRES_TEST_DB=shelf_test
|
|
||||||
POSTGRES_USER=postgres
|
|
||||||
POSTGRES_PASSWORD=postgres
|
|
||||||
POSTGRES_PORT=5432
|
|
||||||
ENV=dev
|
|
||||||
|
|
||||||
POSTGRES_DRIVER=postgres
|
|
||||||
POSTGRES_SOURCE=postgresql://postgres:postgres@localhost:5432/shelf?sslmode=disable
|
|
||||||
|
|
||||||
BCRYPT_PASSWORD=speak-friend-and-enter
|
|
||||||
SALT_ROUNDS=10
|
|
||||||
TOKEN_SECRET=alohomora123!
|
|
||||||
TOKEN_SECRET_TEST=eyJhbGciOiJIUzI1NiIsInR
|
|
@ -0,0 +1,145 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.sp4ke.xyz/AnisB/shelf_api_go/db_client"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Product struct {
|
||||||
|
Id int64 `db:"id"`
|
||||||
|
Name string `db:"name"`
|
||||||
|
Price float64 `db:"price"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SQL
|
||||||
|
// row := db_client.ConnectDB.QueryRow("", id)
|
||||||
|
|
||||||
|
// SQLX
|
||||||
|
// db_client.ConnectDB.Get() / SINGLE ROW
|
||||||
|
// db_client.ConnectDB.Select() / SLICE OF ROWS ( query )
|
||||||
|
|
||||||
|
func GetProducts(c *gin.Context) {
|
||||||
|
var products []Product
|
||||||
|
|
||||||
|
err := db_client.ConnectDB.Select(&products, "SELECT * FROM products")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, products)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetProduct(c *gin.Context) {
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, _ := strconv.Atoi(idStr)
|
||||||
|
var product Product
|
||||||
|
|
||||||
|
err := db_client.ConnectDB.Get(&product, "SELECT * FROM products WHERE id=($1)", id)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Product not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, product)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateProduct(c *gin.Context) {
|
||||||
|
var reqBody Product
|
||||||
|
if err := c.ShouldBindJSON(&reqBody); err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Invalid request body",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := db_client.ConnectDB.Exec("INSERT INTO products (name, price) VALUES ($1, $2)",
|
||||||
|
reqBody.Name,
|
||||||
|
reqBody.Price)
|
||||||
|
|
||||||
|
// Tow key methods we get form res
|
||||||
|
// res.LastInsertId
|
||||||
|
// res.RowsAffected
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Could Create Product",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id, _ := res.LastInsertId()
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"error": false,
|
||||||
|
"id": id,
|
||||||
|
"message": "New product created",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateProduct(c *gin.Context) {
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, _ := strconv.Atoi(idStr)
|
||||||
|
var reqBody Product
|
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(&reqBody); err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Invalid request body",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := db_client.ConnectDB.Exec("UPDATE products SET name=$1, price=$2 WHERE id=$3 RETURNING *", reqBody.Name, reqBody.Price, id)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Product not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
AffectedId, _ := res.RowsAffected()
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"error": false,
|
||||||
|
"id": AffectedId,
|
||||||
|
"message": "Product have been updated successfully",
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteProduct(c *gin.Context) {
|
||||||
|
idStr := c.Param("id")
|
||||||
|
id, _ := strconv.Atoi(idStr)
|
||||||
|
|
||||||
|
res, err := db_client.ConnectDB.Exec("DELETE FROM products WHERE id=($1)", id)
|
||||||
|
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": true,
|
||||||
|
"message": "Product not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
AffectedId, _ := res.RowsAffected()
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"error": false,
|
||||||
|
"id": AffectedId,
|
||||||
|
"message": "Product have been deleted successfully",
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
package controllers
|
Binary file not shown.
@ -0,0 +1,32 @@
|
|||||||
|
package db_client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.sp4ke.xyz/AnisB/shelf_api_go/config"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ConnectDB *sqlx.DB
|
||||||
|
|
||||||
|
func InitialiseDBConnection() {
|
||||||
|
config, err := config.LoadConfig(".")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not load config: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sqlx.Open(config.PostgresDriver, config.PostgresSource)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not connect to database: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not ping database: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnectDB = db
|
||||||
|
}
|
@ -1,33 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type product struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Price float64 `json:"price"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func getProducts(db *sql.DB, start, count int) ([]product, error) {
|
|
||||||
return nil, errors.New("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) getProduct(db *sql.DB) error {
|
|
||||||
return errors.New("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) updateProduct(db *sql.DB) error {
|
|
||||||
return errors.New("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) deleteProduct(db *sql.DB) error {
|
|
||||||
return errors.New("Not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *product) createProduct(db *sql.DB) error {
|
|
||||||
return errors.New("Not implemented")
|
|
||||||
}
|
|
Loading…
Reference in New Issue