diff --git a/pkg/internal/http/api/publishers_api.go b/pkg/internal/http/api/publishers_api.go index c7f6493..20f769e 100644 --- a/pkg/internal/http/api/publishers_api.go +++ b/pkg/internal/http/api/publishers_api.go @@ -67,7 +67,26 @@ func createPersonalPublisher(c *fiber.Ctx) error { } user := c.Locals("user").(authm.Account) - if pub, err := services.CreatePersonalPublisher(user); err != nil { + var data struct { + Name string `json:"name" validate:"required,min=4,max=32,alphanum"` + Nick string `json:"nick" validate:"required,min=2,max=64"` + Description string `json:"description"` + Avatar string `json:"avatar"` + Banner string `json:"banner"` + } + + if err := exts.BindAndValidate(c, &data); err != nil { + return err + } + + if pub, err := services.CreatePersonalPublisher( + user, + data.Name, + data.Nick, + data.Description, + data.Avatar, + data.Banner, + ); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.JSON(pub) @@ -81,7 +100,12 @@ func createOrganizationPublisher(c *fiber.Ctx) error { user := c.Locals("user").(authm.Account) var data struct { - Realm string `json:"realm"` + Realm string `json:"realm" validate:"required"` + Name string `json:"name" validate:"required,min=4,max=32,alphanum"` + Nick string `json:"nick" validate:"required,min=2,max=64"` + Description string `json:"description"` + Avatar string `json:"avatar"` + Banner string `json:"banner"` } if err := exts.BindAndValidate(c, &data); err != nil { @@ -96,7 +120,15 @@ func createOrganizationPublisher(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusForbidden, "you least need to be the admin of this realm to create a publisher") } - if pub, err := services.CreateOrganizationPublisher(user, realm); err != nil { + if pub, err := services.CreateOrganizationPublisher( + user, + realm, + data.Name, + data.Nick, + data.Description, + data.Avatar, + data.Banner, + ); err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } else { return c.JSON(pub) diff --git a/pkg/internal/services/publishers.go b/pkg/internal/services/publishers.go index 55265ea..1ed8563 100644 --- a/pkg/internal/services/publishers.go +++ b/pkg/internal/services/publishers.go @@ -23,30 +23,20 @@ func GetPublisherByName(name string, userID uint) (models.Publisher, error) { return publisher, nil } -func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) { - var publisher models.Publisher - var count int64 - if err := database.C. - Model(&models.Publisher{}). - Where("account_id = ? AND type = ?", user.ID, models.PublisherTypePersonal). - Count(&count).Error; err != nil { - return publisher, fmt.Errorf("unable to count exsisting publisher: %v", err) - } - if count > 0 { - return publisher, fmt.Errorf("personal publisher already exists") - } - - publisher = models.Publisher{ +func CreatePersonalPublisher(user authm.Account, name, nick, desc, avatar, banner string) (models.Publisher, error) { + publisher := models.Publisher{ Type: models.PublisherTypePersonal, - Name: user.Name, - Nick: user.Nick, - Description: user.Description, + Name: name, + Nick: nick, + Description: desc, + Avatar: avatar, + Banner: banner, AccountID: &user.ID, } - if user.Avatar != nil { + if user.Avatar != nil && len(publisher.Avatar) == 0 { publisher.Avatar = *user.Avatar } - if user.Banner != nil { + if user.Banner != nil && len(publisher.Banner) == 0 { publisher.Banner = *user.Banner } @@ -56,31 +46,21 @@ func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) { return publisher, nil } -func CreateOrganizationPublisher(user authm.Account, realm authm.Realm) (models.Publisher, error) { - var publisher models.Publisher - var count int64 - if err := database.C. - Model(&models.Publisher{}). - Where("realm_id = ? AND type = ?", realm.ID, models.PublisherTypeOrganization). - Count(&count).Error; err != nil { - return publisher, fmt.Errorf("unable to count exsisting publisher: %v", err) - } - if count > 0 { - return publisher, fmt.Errorf("organization publisher already exists") - } - - publisher = models.Publisher{ +func CreateOrganizationPublisher(user authm.Account, realm authm.Realm, name, nick, desc, avatar, banner string) (models.Publisher, error) { + publisher := models.Publisher{ Type: models.PublisherTypeOrganization, - Name: realm.Alias, - Nick: realm.Name, - Description: realm.Description, + Name: name, + Nick: nick, + Description: desc, + Avatar: avatar, + Banner: banner, RealmID: &realm.ID, AccountID: &user.ID, } - if realm.Avatar != nil { + if realm.Avatar != nil && len(publisher.Avatar) == 0 { publisher.Avatar = *realm.Avatar } - if realm.Banner != nil { + if realm.Banner != nil && len(publisher.Banner) == 0 { publisher.Banner = *realm.Banner }