Added Optional channel description name to channel creation, adjusted tests
Build Docker and Deploy / Build Docker Container (push) Successful in 2m22s Details
Build Docker and Deploy / Deploy to Server (push) Successful in 11s Details

This commit is contained in:
Julian Graf 2024-02-24 12:20:41 +01:00
parent 51f5f1005a
commit 77cfe75043
4 changed files with 44 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
"blackforestbytes.com/simplecloudnotifier/db/impl/primary"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
@ -180,6 +181,8 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
type body struct {
Name string `json:"name"`
Subscribe *bool `json:"subscribe"`
Description *string `json:"description"`
}
var u uri
@ -233,7 +236,15 @@ func (h APIHandler) CreateChannel(g *gin.Context) ginresp.HTTPResponse {
subscribeKey := h.app.GenerateRandomAuthKey()
channel, err := h.database.CreateChannel(ctx, u.UserID, channelDisplayName, channelInternalName, subscribeKey)
cChannel := primary.CreateChanel{
UserId: u.UserID,
IntName: channelInternalName,
SubscribeKey: subscribeKey,
DisplayName: channelDisplayName,
Description: b.Description,
}
channel, err := h.database.CreateChannel(ctx, cChannel)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create channel", err)
}

View File

@ -58,7 +58,15 @@ func (db *Database) GetChannelByID(ctx db.TxContext, chanid models.ChannelID) (*
return &channel, nil
}
func (db *Database) CreateChannel(ctx db.TxContext, userid models.UserID, dispName string, intName string, subscribeKey string) (models.Channel, error) {
type CreateChanel struct {
UserId models.UserID
DisplayName string
IntName string
SubscribeKey string
Description *string
}
func (db *Database) CreateChannel(ctx db.TxContext, channel CreateChanel) (models.Channel, error) {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return models.Channel{}, err
@ -66,10 +74,11 @@ func (db *Database) CreateChannel(ctx db.TxContext, userid models.UserID, dispNa
entity := models.ChannelDB{
ChannelID: models.NewChannelID(),
OwnerUserID: userid,
DisplayName: dispName,
InternalName: intName,
SubscribeKey: subscribeKey,
OwnerUserID: channel.UserId,
DisplayName: channel.DisplayName,
InternalName: channel.IntName,
SubscribeKey: channel.SubscribeKey,
DescriptionName: channel.Description,
TimestampCreated: time2DB(time.Now()),
TimestampLastSent: nil,
MessagesSent: 0,

View File

@ -4,6 +4,7 @@ import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/db/impl/primary"
"blackforestbytes.com/simplecloudnotifier/db/simplectx"
"blackforestbytes.com/simplecloudnotifier/google"
"blackforestbytes.com/simplecloudnotifier/models"
@ -331,7 +332,13 @@ func (app *Application) GetOrCreateChannel(ctx *AppContext, userid models.UserID
subscribeKey := app.GenerateRandomAuthKey()
newChan, err := app.Database.Primary.CreateChannel(ctx, userid, displayChanName, intChanName, subscribeKey)
channel := primary.CreateChanel{
UserId: userid,
IntName: intChanName,
SubscribeKey: subscribeKey,
DisplayName: displayChanName,
}
newChan, err := app.Database.Primary.CreateChannel(ctx, channel)
if err != nil {
return models.Channel{}, err
}

View File

@ -54,6 +54,16 @@ func TestCreateChannel(t *testing.T) {
tt.AssertMappedSet(t, "channels", []string{"asdf", "test"}, clist.Channels, "display_name")
tt.AssertMappedSet(t, "channels", []string{"asdf", "test"}, clist.Channels, "internal_name")
}
tt.RequestAuthPost[gin.H](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid), gin.H{
"name": "withdesc",
"description": "desc",
})
{
clist := tt.RequestAuthGet[chanlist](t, admintok, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", uid))
tt.AssertEqual(t, "chan.len", 3, len(clist.Channels))
tt.AssertEqual(t, "description_name", "desc", clist.Channels[2]["description_name"])
}
}
func TestCreateChannelNameTooLong(t *testing.T) {