SimpleCloudNotifier/scnserver/api/handler/apiPreview.go

151 lines
4.9 KiB
Go
Raw Normal View History

2024-06-12 00:35:06 +02:00
package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
2024-06-12 00:35:06 +02:00
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
2024-06-12 00:35:06 +02:00
"net/http"
)
// GetUserPreview swaggerdoc
//
// @Summary Get a user (similar to api-user-get, but can be called from anyone and only returns a subset of fields)
// @ID api-user-get-preview
// @Tags API-v2
//
// @Param uid path string true "UserID"
//
// @Success 200 {object} models.UserPreview
2024-06-12 00:35:06 +02:00
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
// @Failure 404 {object} ginresp.apiError "user not found"
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/users/{uid} [GET]
func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
2024-06-12 00:35:06 +02:00
type uri struct {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
var u uri
ctx, g, errResp := pctx.URI(&u).Start()
2024-06-12 00:35:06 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
2024-06-12 00:43:07 +02:00
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
user, err := h.database.GetUser(ctx, u.UserID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
2024-06-12 00:35:06 +02:00
return finishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
})
2024-06-12 00:35:06 +02:00
}
// GetChannelPreview swaggerdoc
//
// @Summary Get a single channel (similar to api-channels-get, but can be called from anyone and only returns a subset of fields)
// @ID api-channels-get-preview
// @Tags API-v2
//
// @Param cid path string true "ChannelID"
//
// @Success 200 {object} models.ChannelPreview
2024-06-12 00:35:06 +02:00
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
// @Failure 404 {object} ginresp.apiError "channel not found"
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/channels/{cid} [GET]
func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPResponse {
2024-06-12 00:35:06 +02:00
type uri struct {
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
}
var u uri
ctx, g, errResp := pctx.URI(&u).Start()
2024-06-12 00:35:06 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
2024-06-12 00:43:07 +02:00
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
2024-06-12 00:35:06 +02:00
channel, err := h.database.GetChannelByID(ctx, u.ChannelID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.Preview()))
})
2024-06-12 00:35:06 +02:00
}
// GetUserKeyPreview swaggerdoc
//
// @Summary Get a single key (similar to api-tokenkeys-get, but can be called from anyone and only returns a subset of fields)
// @ID api-tokenkeys-get-preview
// @Tags API-v2
//
// @Param kid path string true "TokenKeyID"
//
// @Success 200 {object} models.KeyTokenPreview
2024-06-12 00:35:06 +02:00
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
// @Failure 404 {object} ginresp.apiError "message not found"
// @Failure 500 {object} ginresp.apiError "internal server error"
//
// @Router /api/v2/preview/keys/{kid} [GET]
func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPResponse {
2024-06-12 00:35:06 +02:00
type uri struct {
2024-06-12 01:08:02 +02:00
KeyID models.KeyTokenID `uri:"kid" binding:"entityid"`
2024-06-12 00:35:06 +02:00
}
var u uri
ctx, g, errResp := pctx.URI(&u).Start()
2024-06-12 00:35:06 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
2024-06-12 00:43:07 +02:00
if permResp := ctx.CheckPermissionAny(); permResp != nil {
return *permResp
}
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
if errors.Is(err, sql.ErrNoRows) {
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
}
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview()))
2024-06-12 00:35:06 +02:00
})
2024-06-12 00:35:06 +02:00
}