feat: user CRUD
This commit is contained in:
1
api/controllers/orderController.go
Normal file
1
api/controllers/orderController.go
Normal file
@@ -0,0 +1 @@
|
||||
package controllers
|
||||
16
api/controllers/testController.go
Normal file
16
api/controllers/testController.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func BaseFunction(context *gin.Context) {
|
||||
user_profile, _ := context.Get("user_profile")
|
||||
context.IndentedJSON(http.StatusOK, user_profile)
|
||||
}
|
||||
|
||||
func PublicEndpoint(context *gin.Context) {
|
||||
context.String(http.StatusOK, "Public endpoint for you to land at")
|
||||
}
|
||||
103
api/controllers/userController.go
Normal file
103
api/controllers/userController.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"ordr-api/dto"
|
||||
"ordr-api/queries"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func CreateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, err := pool.Acquire(ctx)
|
||||
defer conn.Release()
|
||||
if err != nil {
|
||||
ctx.String(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
user_profile, _ := ctx.Get("user_profile")
|
||||
|
||||
user_name := ctx.Query("user_name")
|
||||
|
||||
if user_name == "" {
|
||||
log.Println("CreateUser(): ERROR: user name not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
updateCommandTag, update_err := conn.Exec(context.Background(), queries.USER_UPDATE_QUERY, user_name, user_profile.(dto.UserProfileResponse).Sub)
|
||||
if update_err != nil {
|
||||
log.Printf("%s", update_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if updateCommandTag.RowsAffected() == 0 {
|
||||
_, query_err := conn.Exec(context.Background(), queries.USER_CREATE_QUERY, user_profile.(dto.UserProfileResponse).Sub, user_name)
|
||||
if query_err != nil {
|
||||
log.Printf("%s", query_err.Error())
|
||||
ctx.String(http.StatusInternalServerError, query_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func PromoteUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
defer conn.Release()
|
||||
if conn_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, conn_err.Error())
|
||||
}
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "PromoteUser(): ERROR Missing user id")
|
||||
return
|
||||
}
|
||||
|
||||
_, update_err := conn.Exec(context.Background(), queries.USER_SET_IS_ADMIN_QUERY, user_id)
|
||||
if update_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, update_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func DemoteUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, conn_err.Error())
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "ERROR: User Id Not Supplied")
|
||||
return
|
||||
}
|
||||
_, exec_err := conn.Exec(context.Background(), queries.USER_REVOKE_ADMIN_QUERY, user_id)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, exec_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func DeactivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("DeactivateUser(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
}
|
||||
|
||||
conn.Exec(context.Background(), queries.USER_SET_INACTIVE_QUERY, user_id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user