Customize publisher meta when creating

👔 Now user and org can have multiple publishers
This commit is contained in:
LittleSheep 2024-12-01 12:53:46 +08:00
parent 09335ea99f
commit 9dd03e0734
2 changed files with 53 additions and 41 deletions

View File

@ -67,7 +67,26 @@ func createPersonalPublisher(c *fiber.Ctx) error {
} }
user := c.Locals("user").(authm.Account) 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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.JSON(pub) return c.JSON(pub)
@ -81,7 +100,12 @@ func createOrganizationPublisher(c *fiber.Ctx) error {
user := c.Locals("user").(authm.Account) user := c.Locals("user").(authm.Account)
var data struct { 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 { 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") 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()) return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else { } else {
return c.JSON(pub) return c.JSON(pub)

View File

@ -23,30 +23,20 @@ func GetPublisherByName(name string, userID uint) (models.Publisher, error) {
return publisher, nil return publisher, nil
} }
func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) { func CreatePersonalPublisher(user authm.Account, name, nick, desc, avatar, banner string) (models.Publisher, error) {
var publisher models.Publisher 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{
Type: models.PublisherTypePersonal, Type: models.PublisherTypePersonal,
Name: user.Name, Name: name,
Nick: user.Nick, Nick: nick,
Description: user.Description, Description: desc,
Avatar: avatar,
Banner: banner,
AccountID: &user.ID, AccountID: &user.ID,
} }
if user.Avatar != nil { if user.Avatar != nil && len(publisher.Avatar) == 0 {
publisher.Avatar = *user.Avatar publisher.Avatar = *user.Avatar
} }
if user.Banner != nil { if user.Banner != nil && len(publisher.Banner) == 0 {
publisher.Banner = *user.Banner publisher.Banner = *user.Banner
} }
@ -56,31 +46,21 @@ func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) {
return publisher, nil return publisher, nil
} }
func CreateOrganizationPublisher(user authm.Account, realm authm.Realm) (models.Publisher, error) { func CreateOrganizationPublisher(user authm.Account, realm authm.Realm, name, nick, desc, avatar, banner string) (models.Publisher, error) {
var publisher models.Publisher 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{
Type: models.PublisherTypeOrganization, Type: models.PublisherTypeOrganization,
Name: realm.Alias, Name: name,
Nick: realm.Name, Nick: nick,
Description: realm.Description, Description: desc,
Avatar: avatar,
Banner: banner,
RealmID: &realm.ID, RealmID: &realm.ID,
AccountID: &user.ID, AccountID: &user.ID,
} }
if realm.Avatar != nil { if realm.Avatar != nil && len(publisher.Avatar) == 0 {
publisher.Avatar = *realm.Avatar publisher.Avatar = *realm.Avatar
} }
if realm.Banner != nil { if realm.Banner != nil && len(publisher.Banner) == 0 {
publisher.Banner = *realm.Banner publisher.Banner = *realm.Banner
} }