GetUser() works

This commit is contained in:
2022-11-18 23:12:37 +01:00
parent 5991631bfa
commit 55f53deadf
16 changed files with 411 additions and 38 deletions

View File

@@ -2,18 +2,19 @@ package apierr
type APIError int
//goland:noinspection GoSnakeCaseUsage
const (
NO_ERROR APIError = 0000
MISSING_UID APIError = 1101
MISSING_TOK APIError = 1102
MISSING_TITLE APIError = 1103
INVALID_PRIO APIError = 1104
REQ_METHOD APIError = 1105
INVALID_CLIENTTYPE APIError = 1106
MISSING_QUERY_PARAM APIError = 1151
MISSING_BODY_PARAM APIError = 1152
MISSING_URI_PARAM APIError = 1153
MISSING_UID APIError = 1101
MISSING_TOK APIError = 1102
MISSING_TITLE APIError = 1103
INVALID_PRIO APIError = 1104
REQ_METHOD APIError = 1105
INVALID_CLIENTTYPE APIError = 1106
BINDFAIL_QUERY_PARAM APIError = 1151
BINDFAIL_BODY_PARAM APIError = 1152
BINDFAIL_URI_PARAM APIError = 1153
NO_TITLE APIError = 1201
TITLE_TOO_LONG APIError = 1202
@@ -31,8 +32,9 @@ const (
FAILED_VERIFY_PRO_TOKEN APIError = 3001
INVALID_PRO_TOKEN APIError = 3002
COMMIT_FAILED = 9001
DATABASE_ERROR = 9002
COMMIT_FAILED = 9001
DATABASE_ERROR = 9002
PERM_QUERY_FAIL = 9003
FIREBASE_COM_FAILED APIError = 9901
FIREBASE_COM_ERRORED APIError = 9902

View File

@@ -5,6 +5,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/api/models"
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
"database/sql"
"github.com/gin-gonic/gin"
"net/http"
)
@@ -35,13 +36,12 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
ClientType string `form:"client_type"`
}
ctx := h.app.StartRequest(g)
defer ctx.Cancel()
var b body
if err := g.ShouldBindJSON(&b); err != nil {
return ginresp.InternAPIError(apierr.MISSING_BODY_PARAM, "Failed to read body", err)
ctx, errResp := h.app.StartRequest(g, nil, nil, &b)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
var clientType models.ClientType
if b.ClientType == string(models.ClientTypeAndroid) {
@@ -92,8 +92,46 @@ func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSON()))
}
// GetUser swaggerdoc
//
// @Summary Create a new user
// @ID api-user-create
//
// @Param post_body body handler.CreateUser.body false " "
// @Param uid path int true "UserID"
//
// @Success 200 {object} models.UserJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid} [GET]
func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
type uri struct {
UserID int64 `uri:"uid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
user, err := h.app.Database.GetUser(ctx, u.UserID)
if err == sql.ErrNoRows {
return ginresp.InternAPIError(apierr.USER_NOT_FOUND, "User not found", err)
}
if err != nil {
return ginresp.InternAPIError(apierr.DATABASE_ERROR, "Failed to query user", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
}
func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {

View File

@@ -0,0 +1,80 @@
package models
import (
"database/sql"
"github.com/blockloop/scan"
"time"
)
type Channel struct {
ChannelID int64
OwnerUserID int64
Name string
SubscribeKey string
SendKey string
TimestampCreated time.Time
TimestampLastRead *time.Time
TimestampLastSent *time.Time
MessagesSent int
}
func (c Channel) JSON() ChannelJSON {
return ChannelJSON{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
Name: c.Name,
SubscribeKey: c.SubscribeKey,
SendKey: c.SendKey,
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
TimestampLastRead: timeOptFmt(c.TimestampLastRead, time.RFC3339Nano),
TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano),
MessagesSent: c.MessagesSent,
}
}
type ChannelJSON struct {
ChannelID int64 `json:"channel_id"`
OwnerUserID int64 `json:"owner_user_id"`
Name string `json:"name"`
SubscribeKey string `json:"subscribe_key"`
SendKey string `json:"send_key"`
TimestampCreated string `json:"timestamp_created"`
TimestampLastRead *string `json:"timestamp_last_read"`
TimestampLastSent *string `json:"timestamp_last_sent"`
MessagesSent int `json:"messages_sent"`
}
type ChannelDB struct {
ChannelID int64 `db:"channel_id"`
OwnerUserID int64 `db:"owner_user_id"`
Name string `db:"name"`
SubscribeKey string `db:"subscribe_key"`
SendKey string `db:"send_key"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastRead *int64 `db:"timestamp_last_read"`
TimestampLastSent *int64 `db:"timestamp_last_sent"`
MessagesSent int `db:"messages_sent"`
}
func (c ChannelDB) Model() Channel {
return Channel{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
Name: c.Name,
SubscribeKey: c.SubscribeKey,
SendKey: c.SendKey,
TimestampCreated: time.UnixMilli(c.TimestampCreated),
TimestampLastRead: timeOptFromMilli(c.TimestampLastRead),
TimestampLastSent: timeOptFromMilli(c.TimestampLastSent),
MessagesSent: c.MessagesSent,
}
}
func DecodeChannel(r *sql.Rows) (Channel, error) {
var udb ChannelDB
err := scan.RowStrict(&udb, r)
if err != nil {
return Channel{}, err
}
return udb.Model(), nil
}

View File

@@ -1,12 +1,16 @@
package models
import "time"
import (
"database/sql"
"github.com/blockloop/scan"
"time"
)
type User struct {
UserID int64
Username *string
ReadKey string
SendKey string
ReadKey string
AdminKey string
TimestampCreated time.Time
TimestampLastRead *time.Time
@@ -49,3 +53,45 @@ type UserJSON struct {
QuotaDay *string `json:"quota_day"`
IsPro bool `json:"is_pro"`
}
type UserDB struct {
UserID int64 `db:"user_id"`
Username *string `db:"username"`
SendKey string `db:"send_key"`
ReadKey string `db:"read_key"`
AdminKey string `db:"admin_key"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastRead *int64 `db:"timestamp_lastread"`
TimestampLastSent *int64 `db:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent"`
QuotaToday int `db:"quota_today"`
QuotaDay *string `db:"quota_day"`
IsPro bool `db:"is_pro"`
ProToken *string `db:"pro_token"`
}
func (u UserDB) Model() User {
return User{
UserID: u.UserID,
Username: u.Username,
SendKey: u.SendKey,
ReadKey: u.ReadKey,
AdminKey: u.AdminKey,
TimestampCreated: time.UnixMilli(u.TimestampCreated),
TimestampLastRead: timeOptFromMilli(u.TimestampLastRead),
TimestampLastSent: timeOptFromMilli(u.TimestampLastSent),
MessagesSent: u.MessagesSent,
QuotaToday: u.QuotaToday,
QuotaDay: u.QuotaDay,
IsPro: u.IsPro,
}
}
func DecodeUser(r *sql.Rows) (User, error) {
var udb UserDB
err := scan.RowStrict(&udb, r)
if err != nil {
return User{}, err
}
return udb.Model(), nil
}

View File

@@ -12,3 +12,10 @@ func timeOptFmt(t *time.Time, fmt string) *string {
return langext.Ptr(t.Format(fmt))
}
}
func timeOptFromMilli(millis *int64) *time.Time {
if millis == nil {
return nil
}
return langext.Ptr(time.UnixMilli(*millis))
}