149 lines
4.1 KiB
Go
149 lines
4.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"ordr-api/dto"
|
|
"ordr-api/queries"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func SetUserName(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")
|
|
return
|
|
}
|
|
|
|
conn.Exec(context.Background(), queries.USER_SET_INACTIVE_QUERY, user_id)
|
|
}
|
|
}
|
|
|
|
func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
conn, conn_err := pool.Acquire(ctx)
|
|
if conn_err != nil {
|
|
log.Printf("GetUserTable(): ERROR: Failed to connect... %s", conn_err.Error())
|
|
ctx.AbortWithStatus(http.StatusInternalServerError)
|
|
}
|
|
defer conn.Release()
|
|
|
|
page := ctx.Query("page")
|
|
if page == "" {
|
|
ctx.String(http.StatusBadRequest, "GetUserTable(): Missing page")
|
|
return
|
|
}
|
|
page_int, conv_err := strconv.Atoi(page)
|
|
if conv_err != nil {
|
|
ctx.String(http.StatusBadRequest, "GetUserTable(): Not an integer")
|
|
return
|
|
}
|
|
rows, query_err := conn.Query(context.Background(), queries.USER_GET_TABLE_DATA, page_int*10, 10)
|
|
if query_err != nil {
|
|
ctx.String(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
|
log.Printf("GetUserTable(): ERROR... %s", query_err.Error())
|
|
}
|
|
defer rows.Close()
|
|
|
|
var users []dto.UserResponse
|
|
for rows.Next() {
|
|
var user dto.UserResponse
|
|
scan_err := rows.Scan(&user.Id, &user.Name, &user.Job_Position, &user.Active, &user.Admin)
|
|
if scan_err != nil {
|
|
ctx.String(http.StatusInternalServerError, "GetUserTable(): ERROR: Failed to scan..")
|
|
log.Printf("GetUserTable(): ERROR... %s", scan_err.Error())
|
|
return
|
|
}
|
|
|
|
users = append(users, user)
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, users)
|
|
}
|
|
}
|