feat: get user table
This commit is contained in:
@@ -18,6 +18,7 @@ func IsAdmin(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
log.Println(conn_err)
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
user_profile, _ := ctx.Get("user_profile")
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
10
api/dto/user_response.go
Normal file
10
api/dto/user_response.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package dto
|
||||
|
||||
// User response for exposing to the front-end
|
||||
type UserResponse struct {
|
||||
Id int
|
||||
Name string
|
||||
Job_Position string
|
||||
Active bool
|
||||
Admin bool
|
||||
}
|
||||
@@ -62,9 +62,9 @@ func main() {
|
||||
router.GET("/auth/logout", auth.LogoutHandler)
|
||||
router.GET("/auth/logout_callback", auth.LogoutCallbackHandler(store))
|
||||
router.GET("/callback", auth.AuthenticationCallbackHandler(authenticator))
|
||||
router.GET("/public", controllers.PublicEndpoint)
|
||||
router.GET("/users", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.GetUserTable(pool))
|
||||
|
||||
router.POST("/user/create", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), controllers.CreateUser(pool))
|
||||
router.POST("/user/name", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), controllers.SetUserName(pool))
|
||||
|
||||
router.PUT("/user/promote", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.PromoteUser(pool))
|
||||
router.PUT("/user/demote", middleware.IsAuthenticated(authenticator), middleware.GetUserProfile, middleware.UserInDatabase(pool), middleware.UserIsActive(pool), middleware.IsAdmin(pool), controllers.DemoteUser(pool))
|
||||
|
||||
@@ -19,3 +19,17 @@ UPDATE ordr_user SET is_admin = FALSE WHERE id = $1;
|
||||
const USER_SET_INACTIVE_QUERY string = `
|
||||
UPDATE ordr_user SET active = FALSE WHERE id = $1;
|
||||
`
|
||||
|
||||
const USER_GET_TABLE_DATA string = `
|
||||
SELECT
|
||||
id,
|
||||
user_name,
|
||||
job_position,
|
||||
active,
|
||||
is_admin
|
||||
FROM
|
||||
ordr_user
|
||||
ORDER BY user_name
|
||||
OFFSET $2
|
||||
LIMIT $3;
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user