|
|
|
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 JSON binding",
|
|
|
|
})
|
|
|
|
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": err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := res.LastInsertId()
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
|
|
"error": false,
|
|
|
|
"id": id,
|
|
|
|
"message": "New product created successfully",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
|
|
|
"error": true,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
|
|
|
"error": true,
|
|
|
|
"message": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
affectedId, _ := res.RowsAffected()
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
|
|
"error": false,
|
|
|
|
"id": affectedId,
|
|
|
|
"message": "Product have been deleted successfully",
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|