Skip to content

Commit

Permalink
Merge pull request #344 from Zibbp/channel-creation-fix
Browse files Browse the repository at this point in the history
fix(channel): allow specifying an external id when creating a channel…
  • Loading branch information
Zibbp authored Jan 6, 2024
2 parents 1b4d525 + a60f057 commit 856d300
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions internal/transport/http/channel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package http

import (
"math/rand"
"net/http"
"strconv"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
Expand All @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 856d300

Please sign in to comment.