Refactor models to use single struct per entity

This commit is contained in:
2024-09-15 21:07:46 +02:00
parent 6d432b9de4
commit 527a659a1b
41 changed files with 778 additions and 1576 deletions

View File

@@ -46,7 +46,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
Selector *string `json:"selector" form:"selector" enums:"owned,subscribed_any,all_any,subscribed,all"`
}
type response struct {
Channels []models.ChannelWithSubscriptionJSON `json:"channels"`
Channels []models.ChannelWithSubscription `json:"channels"`
}
var u uri
@@ -65,15 +65,13 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
sel := strings.ToLower(langext.Coalesce(q.Selector, "owned"))
var res []models.ChannelWithSubscriptionJSON
if sel == "owned" {
channels, err := h.database.ListChannelsByOwner(ctx, u.UserID, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(true) })
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, response{Channels: channels}, "INCLUDE_KEY"))
} else if sel == "subscribed_any" {
@@ -81,7 +79,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels}))
} else if sel == "all_any" {
@@ -89,7 +87,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels}))
} else if sel == "subscribed" {
@@ -97,7 +95,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels}))
} else if sel == "all" {
@@ -105,7 +103,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err)
}
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) })
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels}))
} else {
@@ -113,8 +111,6 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
}
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
})
}
@@ -127,7 +123,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param cid path string true "ChannelID"
//
// @Success 200 {object} models.ChannelWithSubscriptionJSON
// @Success 200 {object} models.ChannelWithSubscription
// @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"
@@ -161,7 +157,7 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel, "INCLUDE_KEY"))
})
}
@@ -175,7 +171,7 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param post_body body handler.CreateChannel.body false " "
//
// @Success 200 {object} models.ChannelWithSubscriptionJSON
// @Success 200 {object} models.ChannelWithSubscription
// @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 409 {object} ginresp.apiError "channel already exists"
@@ -258,11 +254,11 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)), "INCLUDE_KEY"))
} else {
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel.WithSubscription(nil), "INCLUDE_KEY"))
}
@@ -282,7 +278,7 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param send_key body string false "Send `true` to create a new send_key"
// @Param display_name body string false "Change the cahnnel display-name (only chnages to lowercase/uppercase are allowed - internal_name must stay the same)"
//
// @Success 200 {object} models.ChannelWithSubscriptionJSON
// @Success 200 {object} models.ChannelWithSubscription
// @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"
@@ -381,7 +377,7 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel, "INCLUDE_KEY"))
})
}
@@ -419,9 +415,9 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
Trimmed *bool `json:"trimmed" form:"trimmed"`
}
type response struct {
Messages []models.MessageJSON `json:"messages"`
NextPageToken string `json:"next_page_token"`
PageSize int `json:"page_size"`
Messages []models.Message `json:"messages"`
NextPageToken string `json:"next_page_token"`
PageSize int `json:"page_size"`
}
var u uri
@@ -466,14 +462,12 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
}
var res []models.MessageJSON
if trimmed {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.Trim() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
} else {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: messages, NextPageToken: npt.Token(), PageSize: pageSize}))
}
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
}

View File

@@ -31,7 +31,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
type response struct {
Clients []models.ClientJSON `json:"clients"`
Clients []models.Client `json:"clients"`
}
var u uri
@@ -52,9 +52,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
}
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: clients}))
})
}
@@ -68,7 +66,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param cid path string true "ClientID"
//
// @Success 200 {object} models.ClientJSON
// @Success 200 {object} models.Client
// @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 "client not found"
@@ -102,7 +100,7 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client))
})
}
@@ -117,7 +115,7 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param post_body body handler.AddClient.body false " "
//
// @Success 200 {object} models.ClientJSON
// @Success 200 {object} models.Client
// @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 500 {object} ginresp.apiError "internal server error"
@@ -164,7 +162,7 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client))
})
}
@@ -178,7 +176,7 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param cid path string true "ClientID"
//
// @Success 200 {object} models.ClientJSON
// @Success 200 {object} models.Client
// @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 "client not found"
@@ -217,7 +215,7 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client))
})
}
@@ -235,7 +233,7 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param clientname body string false "Change the clientname (send an empty string to clear it)"
// @Param pro_token body string false "Send a verification of premium purchase"
//
// @Success 200 {object} models.ClientJSON
// @Success 200 {object} models.Client
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
// @Failure 401 {object} ginresp.apiError "client is not authorized / has missing permissions"
// @Failure 404 {object} ginresp.apiError "client not found"
@@ -322,7 +320,7 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client))
})
}

View File

@@ -33,7 +33,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
UserID models.UserID `uri:"uid" binding:"entityid"`
}
type response struct {
Keys []models.KeyTokenJSON `json:"keys"`
Keys []models.KeyToken `json:"keys"`
}
var u uri
@@ -54,9 +54,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err)
}
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: toks}))
})
}
@@ -71,7 +69,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param kid path string true "TokenKeyID"
//
// @Success 200 {object} models.KeyTokenWithTokenJSON
// @Success 200 {object} models.KeyToken
// @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"
@@ -109,7 +107,7 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, keytoken, "INCLUDE_TOKEN"))
})
}
@@ -124,7 +122,7 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
// @Param uid path string true "UserID"
// @Param kid path string true "TokenKeyID"
//
// @Success 200 {object} models.KeyTokenJSON
// @Success 200 {object} models.KeyToken
// @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"
@@ -158,7 +156,7 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken))
})
}
@@ -174,7 +172,7 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param post_body body handler.UpdateUserKey.body false " "
//
// @Success 200 {object} models.KeyTokenJSON
// @Success 200 {object} models.KeyToken
// @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"
@@ -260,7 +258,7 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
keytoken.Channels = *b.Channels
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken))
})
}
@@ -275,7 +273,7 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param post_body body handler.CreateUserKey.body false " "
//
// @Success 200 {object} models.KeyTokenJSON
// @Success 200 {object} models.KeyToken
// @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"
@@ -333,7 +331,7 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
return finishSuccess(ginext.JSONWithFilter(http.StatusOK, keytok, "INCLUDE_TOKEN"))
})
}
@@ -348,7 +346,7 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param uid path string true "UserID"
// @Param kid path string true "TokenKeyID"
//
// @Success 200 {object} models.KeyTokenJSON
// @Success 200 {object} models.KeyToken
// @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"
@@ -391,7 +389,7 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, client))
})
}

View File

@@ -50,9 +50,9 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
KeyTokens []string `json:"used_key" form:"used_key"`
}
type response struct {
Messages []models.MessageJSON `json:"messages"`
NextPageToken string `json:"next_page_token"`
PageSize int `json:"page_size"`
Messages []models.Message `json:"messages"`
NextPageToken string `json:"next_page_token"`
PageSize int `json:"page_size"`
}
var q query
@@ -151,15 +151,13 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
}
var res []models.MessageJSON
if trimmed {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.PreMarshal().Trim() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
} else {
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.PreMarshal() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
}
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
})
}
@@ -174,7 +172,7 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param mid path string true "MessageID"
//
// @Success 200 {object} models.MessageJSON
// @Success 200 {object} models.Message
// @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"
@@ -211,7 +209,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
// or we subscribe (+confirmed) to the channel and have read/admin key
if ctx.CheckPermissionMessageRead(msg) {
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal()))
}
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
@@ -229,7 +227,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
}
// => perm okay
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal()))
}
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
@@ -246,7 +244,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param mid path string true "MessageID"
//
// @Success 200 {object} models.MessageJSON
// @Success 200 {object} models.Message
// @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"
@@ -293,7 +291,7 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal()))
})
}

View File

@@ -19,7 +19,7 @@ import (
//
// @Param uid path string true "UserID"
//
// @Success 200 {object} models.UserPreviewJSON
// @Success 200 {object} models.UserPreview
// @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"
@@ -65,7 +65,7 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param cid path string true "ChannelID"
//
// @Success 200 {object} models.ChannelPreviewJSON
// @Success 200 {object} models.ChannelPreview
// @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"
@@ -98,7 +98,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
return finishSuccess(ginext.JSON(http.StatusOK, channel.Preview()))
})
}
@@ -111,7 +111,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
//
// @Param kid path string true "TokenKeyID"
//
// @Success 200 {object} models.KeyTokenPreviewJSON
// @Success 200 {object} models.KeyTokenPreview
// @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"
@@ -144,7 +144,7 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview()))
})
}

View File

@@ -60,7 +60,7 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
ChannelOwnerUserID *models.UserID `json:"channel_owner_user_id" form:"channel_owner_user_id"`
}
type response struct {
Subscriptions []models.SubscriptionJSON `json:"subscriptions"`
Subscriptions []models.Subscription `json:"subscriptions"`
}
var u uri
@@ -129,9 +129,7 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
}
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
})
}
@@ -158,7 +156,7 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
ChannelID models.ChannelID `uri:"cid" binding:"entityid"`
}
type response struct {
Subscriptions []models.SubscriptionJSON `json:"subscriptions"`
Subscriptions []models.Subscription `json:"subscriptions"`
}
var u uri
@@ -182,14 +180,12 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
}
clients, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})})
subs, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})})
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
}
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: subs}))
})
}
@@ -203,7 +199,7 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
// @Param uid path string true "UserID"
// @Param sid path string true "SubscriptionID"
//
// @Success 200 {object} models.SubscriptionJSON
// @Success 200 {object} models.Subscription
// @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 "subscription not found"
@@ -240,7 +236,7 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription))
})
}
@@ -254,7 +250,7 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
// @Param uid path string true "UserID"
// @Param sid path string true "SubscriptionID"
//
// @Success 200 {object} models.SubscriptionJSON
// @Success 200 {object} models.Subscription
// @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 "subscription not found"
@@ -296,7 +292,7 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription))
})
}
@@ -312,7 +308,7 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
// @Param query_data query handler.CreateSubscription.query false " "
// @Param post_data body handler.CreateSubscription.body false " "
//
// @Success 200 {object} models.SubscriptionJSON
// @Success 200 {object} models.Subscription
// @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 500 {object} ginresp.apiError "internal server error"
@@ -397,7 +393,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
existingSub.Confirmed = true
}
return finishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, existingSub))
}
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
@@ -405,7 +401,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, sub))
})
}
@@ -420,7 +416,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
// @Param sid path string true "SubscriptionID"
// @Param post_data body handler.UpdateSubscription.body false " "
//
// @Success 200 {object} models.SubscriptionJSON
// @Success 200 {object} models.Subscription
// @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 "subscription not found"
@@ -478,7 +474,7 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, subscription))
})
}

View File

@@ -22,7 +22,7 @@ import (
//
// @Param post_body body handler.CreateUser.body false " "
//
// @Success 200 {object} models.UserJSONWithClientsAndKeys
// @Success 200 {object} models.UserWithClientsAndKeys
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
// @Failure 500 {object} ginresp.apiError "internal server error"
//
@@ -120,7 +120,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
if b.NoClient {
return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
} else {
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
if err != nil {
@@ -132,7 +132,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients([]models.Client{client}, adminKey, sendKey, readKey)))
}
})
}
@@ -145,7 +145,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
//
// @Param uid path string true "UserID"
//
// @Success 200 {object} models.UserJSON
// @Success 200 {object} models.User
// @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"
@@ -178,7 +178,7 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal()))
})
@@ -196,7 +196,7 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
// @Param username body string false "Change the username (send an empty string to clear it)"
// @Param pro_token body string false "Send a verification of premium purchase"
//
// @Success 200 {object} models.UserJSON
// @Success 200 {object} models.User
// @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"
@@ -271,6 +271,6 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
}
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal()))
})
}