feat: get user table
This commit is contained in:
@@ -6,12 +6,13 @@ import (
|
||||
"net/http"
|
||||
"ordr-api/dto"
|
||||
"ordr-api/queries"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func CreateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
func SetUserName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, err := pool.Acquire(ctx)
|
||||
defer conn.Release()
|
||||
@@ -96,8 +97,52 @@ func DeactivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user