added user controller + product controller fixes
parent
4b75672b9a
commit
d973ae965e
@ -1 +1,150 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.sp4ke.xyz/AnisB/shelf_api_go/db_client"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64 `db:"id"`
|
||||
FirstName string `db:"first_name"`
|
||||
LastName string `db:"last_name"`
|
||||
UserName string `db:"user_name"`
|
||||
Password string `db:"password"`
|
||||
}
|
||||
|
||||
func GetUsers(c *gin.Context) {
|
||||
var users []User
|
||||
|
||||
err := db_client.ConnectDB.Select(&users, "SELECT * FROM users")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"error": true,
|
||||
"message": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, users)
|
||||
|
||||
}
|
||||
|
||||
func GetUser(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
var user User
|
||||
|
||||
err := db_client.ConnectDB.Get(&user, "SELECT * FROM users WHERE id=($1)", id)
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": true,
|
||||
"message": "User not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
func CreateUser(c *gin.Context) {
|
||||
var reqBody User
|
||||
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 users (first_name, last_name, user_name, password) VALUES ($1, $2, $3, $4)",
|
||||
reqBody.FirstName,
|
||||
reqBody.LastName,
|
||||
reqBody.UserName,
|
||||
reqBody.Password)
|
||||
|
||||
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 user created successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateUser(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
var reqBody User
|
||||
|
||||
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 users SET first_name=$1, last_name=$2, user_name=$3, password=$4 WHERE id=$5 RETURNING *",
|
||||
reqBody.FirstName,
|
||||
reqBody.LastName,
|
||||
reqBody.UserName,
|
||||
reqBody.Password,
|
||||
id)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": true,
|
||||
"message": "User 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": "User have been updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteUser(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, _ := strconv.Atoi(idStr)
|
||||
|
||||
res, err := db_client.ConnectDB.Exec("DELETE FROM users WHERE id=($1)", id)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": true,
|
||||
"message": "User not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
affectedId, _ := res.RowsAffected()
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"error": false,
|
||||
"id": affectedId,
|
||||
"message": "User have been deleted successfully",
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
DROP TABLE IF EXISTS products;
|
||||
DROP TABLE IF EXISTS products;
|
||||
DROP TABLE IF EXISTS users;
|
Loading…
Reference in New Issue