✨ Wearing badge
This commit is contained in:
parent
378c60cef8
commit
4616f7cc93
@ -7,5 +7,6 @@ type Badge struct {
|
|||||||
|
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Metadata datatypes.JSONMap `json:"metadata"`
|
Metadata datatypes.JSONMap `json:"metadata"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
AccountID uint `json:"account_id"`
|
AccountID uint `json:"account_id"`
|
||||||
}
|
}
|
||||||
|
@ -13,3 +13,23 @@ func GrantBadge(user models.Account, badge models.Badge) error {
|
|||||||
func RevokeBadge(badge models.Badge) error {
|
func RevokeBadge(badge models.Badge) error {
|
||||||
return database.C.Delete(&badge).Error
|
return database.C.Delete(&badge).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ActiveBadge(badge models.Badge) error {
|
||||||
|
accountId := badge.AccountID
|
||||||
|
tx := database.C.Begin()
|
||||||
|
|
||||||
|
if err := tx.Model(&badge).Where("account_id = ?", accountId).Update("is_active", false).Error; err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tx.Model(&badge).Where("id = ?", badge.ID).Update("is_active", true).Error; err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit().Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
28
pkg/internal/web/api/badges_api.go
Normal file
28
pkg/internal/web/api/badges_api.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func activeUserBadge(c *fiber.Ctx) error {
|
||||||
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
|
badgeId, _ := c.ParamsInt("badgeId", 0)
|
||||||
|
|
||||||
|
var badge models.Badge
|
||||||
|
if err := database.C.Where("id = ? AND account_id = ?", badgeId, user.ID).First(&badge).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := services.ActiveBadge(badge); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
}
|
@ -37,6 +37,11 @@ func MapControllers(app *fiber.App, baseURL string) {
|
|||||||
preferences.Put("/notifications", updateNotificationPreference)
|
preferences.Put("/notifications", updateNotificationPreference)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
badges := api.Group("/badges").Name("Badges")
|
||||||
|
{
|
||||||
|
badges.Post("/:badgeId/active", activeUserBadge)
|
||||||
|
}
|
||||||
|
|
||||||
reports := api.Group("/reports").Name("Reports API")
|
reports := api.Group("/reports").Name("Reports API")
|
||||||
{
|
{
|
||||||
abuse := reports.Group("/abuse").Name("Abuse Reports")
|
abuse := reports.Group("/abuse").Name("Abuse Reports")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user