From 77cfe75043a5ccd748f29003f32fc6a3eed9e611 Mon Sep 17 00:00:00 2001 From: Julian Graf Date: Sat, 24 Feb 2024 12:20:41 +0100 Subject: [PATCH] Added Optional channel description name to channel creation, adjusted tests --- scnserver/api/handler/apiChannel.go | 13 ++++++++++++- scnserver/db/impl/primary/channels.go | 19 ++++++++++++++----- scnserver/logic/application.go | 9 ++++++++- scnserver/test/channel_test.go | 10 ++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/scnserver/api/handler/apiChannel.go b/scnserver/api/handler/apiChannel.go index 8692485..a3b644d 100644 --- a/scnserver/api/handler/apiChannel.go +++ b/scnserver/api/handler/apiChannel.go @@ -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) } diff --git a/scnserver/db/impl/primary/channels.go b/scnserver/db/impl/primary/channels.go index 4afe6e8..b333151 100644 --- a/scnserver/db/impl/primary/channels.go +++ b/scnserver/db/impl/primary/channels.go @@ -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, diff --git a/scnserver/logic/application.go b/scnserver/logic/application.go index ce631c4..7c45ab0 100644 --- a/scnserver/logic/application.go +++ b/scnserver/logic/application.go @@ -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 } diff --git a/scnserver/test/channel_test.go b/scnserver/test/channel_test.go index 9f78901..48c9f03 100644 --- a/scnserver/test/channel_test.go +++ b/scnserver/test/channel_test.go @@ -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) {