104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
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)
|
|
}
|
|
}
|