From a60f0573125e9214b6187ca41fa0e6f71f5ffd98 Mon Sep 17 00:00:00 2001 From: Zibbp Date: Sat, 6 Jan 2024 19:22:26 +0000 Subject: [PATCH] fix(channel): allow specifying an external id when creating a channel or generate a random one if not present --- internal/channel/channel.go | 2 +- internal/transport/http/channel.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/channel/channel.go b/internal/channel/channel.go index 85589de7..9aae82c2 100644 --- a/internal/channel/channel.go +++ b/internal/channel/channel.go @@ -40,7 +40,7 @@ func (s *Service) CreateChannel(channelDto Channel) (*ent.Channel, error) { cha, err := s.Store.Client.Channel.Create().SetExtID(channelDto.ExtID).SetName(channelDto.Name).SetDisplayName(channelDto.DisplayName).SetImagePath(channelDto.ImagePath).Save(context.Background()) if err != nil { if _, ok := err.(*ent.ConstraintError); ok { - return nil, fmt.Errorf("channel already exists") + return nil, fmt.Errorf("channel already exists: %v", err) } log.Debug().Err(err).Msg("error creating channel") return nil, fmt.Errorf("error creating channel: %v", err) diff --git a/internal/transport/http/channel.go b/internal/transport/http/channel.go index 26c77209..b5b070da 100644 --- a/internal/transport/http/channel.go +++ b/internal/transport/http/channel.go @@ -1,7 +1,9 @@ package http import ( + "math/rand" "net/http" + "strconv" "github.com/google/uuid" "github.com/labstack/echo/v4" @@ -20,6 +22,7 @@ type ChannelService interface { } type CreateChannelRequest struct { + ExternalID string `json:"ext_id"` Name string `json:"name" validate:"required,min=2,max=50"` DisplayName string `json:"display_name" validate:"required,min=2,max=50"` ImagePath string `json:"image_path" validate:"required,min=3"` @@ -49,7 +52,13 @@ func (h *Handler) CreateChannel(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } + if ccr.ExternalID == "" { + // generate random id - doesn't need to be a uuid + ccr.ExternalID = strconv.Itoa(rand.Intn(1000000)) + } + ccDto := channel.Channel{ + ExtID: ccr.ExternalID, Name: ccr.Name, DisplayName: ccr.DisplayName, ImagePath: ccr.ImagePath,