feat: wrap up user API functionality
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -146,3 +147,92 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
ctx.JSON(http.StatusOK, users)
|
||||
}
|
||||
}
|
||||
|
||||
func GetCurrentAuthenticatedUser(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()
|
||||
|
||||
user_profile, _ := ctx.Get("user_profile")
|
||||
|
||||
sub_id := user_profile.(dto.UserProfileResponse).Sub
|
||||
|
||||
var user dto.UserResponse
|
||||
|
||||
query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_USER_OBJECT, sub_id).Scan(&user.Id, &user.Name, &user.Job_Position, &user.Active, &user.Admin)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetCurrentAuthenticatedUser(): ERROR.... Failed to query")
|
||||
log.Printf("GetCurrentAuthenticatedUser(): ERROR in querying user table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, user)
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePosition(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)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
position_name := ctx.Query("position_name")
|
||||
if position_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreatePosition(): ERROR... Position name not supplied!")
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.CREATE_POSITION, position_name)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "CreatePosition(): ERROR... exec failed")
|
||||
log.Println("CreatePosition(): ERROR... exec failed... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SetUserPosition(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()
|
||||
|
||||
position_name := ctx.Query("position")
|
||||
user_id := ctx.Query("user_id")
|
||||
|
||||
if position_name == "" || user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "SetUserPosition(): ERROR... Missing required parameter")
|
||||
}
|
||||
|
||||
var position_id string
|
||||
var position_name_query string
|
||||
query_err := conn.QueryRow(context.Background(), queries.POSITION_GET_POSITION, position_name).Scan(&position_id, &position_name_query)
|
||||
if query_err != nil {
|
||||
if query_err == pgx.ErrNoRows {
|
||||
ctx.String(http.StatusBadRequest, "SetUserPosition(): ERROR... No such position exists.")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "SetUserPosition(): ERROR... Failed to query")
|
||||
log.Println("SetUserPosition(): ERROR... Failed to query position table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, exec_err := conn.Exec(context.Background(), queries.USER_SET_POSITION, position_id, user_id)
|
||||
if exec_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "SetUserPosition(): ERROR... failed to update user object")
|
||||
log.Println("SetUserPosition(): ERROR... Failed to update user object... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user