feat: frontend
This commit is contained in:
@@ -20,7 +20,7 @@ func SetUserName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
defer conn.Release()
|
||||
if err != nil {
|
||||
// TODO: Log this error
|
||||
ctx.String(http.StatusInternalServerError, err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||
log.Printf("SetUserName(): ERROR... Failed to acquire connection... %s", err.Error())
|
||||
return
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func SetUserName(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
user_name := ctx.Query("user_name")
|
||||
|
||||
if user_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreateUser(): ERROR: user name not supplied")
|
||||
ctx.JSON(http.StatusBadRequest, "CreateUser(): ERROR: user name not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -46,18 +46,18 @@ func PromoteUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
defer conn.Release()
|
||||
if conn_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, conn_err.Error())
|
||||
ctx.JSON(http.StatusInternalServerError, conn_err.Error())
|
||||
}
|
||||
|
||||
user_id := ctx.Query("user_id")
|
||||
if user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "PromoteUser(): ERROR Missing user id")
|
||||
ctx.JSON(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())
|
||||
ctx.JSON(http.StatusInternalServerError, update_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,18 +66,18 @@ 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())
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(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())
|
||||
ctx.JSON(http.StatusInternalServerError, exec_err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ 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")
|
||||
ctx.JSON(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -101,6 +101,25 @@ func DeactivateUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func ActivateUser(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.JSON(http.StatusBadRequest, "DeactivateUser(): User id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
conn.Exec(context.Background(), queries.USER_SET_ACTIVE_QUERY, user_id)
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
@@ -113,23 +132,23 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var request dto.UserRequest
|
||||
body_read_err := ctx.ShouldBindJSON(&request)
|
||||
if body_read_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): ERROR... Invalid user query object")
|
||||
ctx.JSON(http.StatusBadRequest, "GetUserTable(): ERROR... Invalid user query object")
|
||||
return
|
||||
}
|
||||
|
||||
page := ctx.Query("page")
|
||||
if page == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetUserTable(): Missing page")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(http.StatusBadRequest, "GetUserTable(): Not an integer")
|
||||
return
|
||||
}
|
||||
rows, query_err := conn.Query(context.Background(), queries.USER_GET_TABLE_DATA, page_int*utils.PAGE_SIZE, utils.PAGE_SIZE, request.Name, request.JobPosition)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetUserTable(): Failed to query database...")
|
||||
log.Printf("GetUserTable(): ERROR... %s", query_err.Error())
|
||||
}
|
||||
defer rows.Close()
|
||||
@@ -139,7 +158,7 @@ func GetUserTable(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
var user dto.UserResponse
|
||||
scan_err := rows.Scan(&user.Id, &user.Name, &user.JobPosition, &user.Active, &user.Admin)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetUserTable(): ERROR: Failed to scan..")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetUserTable(): ERROR: Failed to scan..")
|
||||
log.Printf("GetUserTable(): ERROR... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -168,7 +187,7 @@ func GetCurrentAuthenticatedUser(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
query_err := conn.QueryRow(context.Background(), queries.GET_CURRENT_USER_OBJECT, sub_id).Scan(&user.Id, &user.Name, &user.JobPosition, &user.Active, &user.Admin)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetCurrentAuthenticatedUser(): ERROR.... Failed to query")
|
||||
ctx.JSON(http.StatusInternalServerError, "GetCurrentAuthenticatedUser(): ERROR.... Failed to query")
|
||||
log.Printf("GetCurrentAuthenticatedUser(): ERROR in querying user table... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -189,13 +208,13 @@ func CreatePosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
|
||||
position_name := ctx.Query("position_name")
|
||||
if position_name == "" {
|
||||
ctx.String(http.StatusBadRequest, "CreatePosition(): ERROR... Position name not supplied!")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(http.StatusInternalServerError, "CreatePosition(): ERROR... exec failed")
|
||||
log.Println("CreatePosition(): ERROR... exec failed... %s", exec_err.Error())
|
||||
return
|
||||
}
|
||||
@@ -215,7 +234,7 @@ func SetUserPosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
user_id := ctx.Query("user_id")
|
||||
|
||||
if position_name == "" || user_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "SetUserPosition(): ERROR... Missing required parameter")
|
||||
ctx.JSON(http.StatusBadRequest, "SetUserPosition(): ERROR... Missing required parameter")
|
||||
}
|
||||
|
||||
var position_id string
|
||||
@@ -223,17 +242,17 @@ func SetUserPosition(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
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.")
|
||||
ctx.JSON(http.StatusBadRequest, "SetUserPosition(): ERROR... No such position exists.")
|
||||
return
|
||||
}
|
||||
ctx.String(http.StatusInternalServerError, "SetUserPosition(): ERROR... Failed to query")
|
||||
ctx.JSON(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")
|
||||
ctx.JSON(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