From 99f8e4c8916364a14bf8bf6ec66e24f3961fdf8f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 1 Sep 2024 16:38:09 +0800 Subject: [PATCH] :sparkles: Daily signs --- .idea/dataSources.local.xml | 2 +- .idea/workspace.xml | 17 +++++---- pkg/internal/models/sign_record.go | 10 ++++++ pkg/internal/server/api/index.go | 6 ++++ pkg/internal/server/api/sign_api.go | 54 +++++++++++++++++++++++++++++ pkg/internal/services/sign.go | 40 +++++++++++++++++++++ 6 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 pkg/internal/models/sign_record.go create mode 100644 pkg/internal/server/api/sign_api.go create mode 100644 pkg/internal/services/sign.go diff --git a/.idea/dataSources.local.xml b/.idea/dataSources.local.xml index 4cd0223..331a5b8 100644 --- a/.idea/dataSources.local.xml +++ b/.idea/dataSources.local.xml @@ -1,6 +1,6 @@ - + " diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2658980..490c578 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,8 +4,13 @@ - - + + + + + + + - @@ -177,7 +181,8 @@ - true diff --git a/pkg/internal/models/sign_record.go b/pkg/internal/models/sign_record.go new file mode 100644 index 0000000..9afeaf2 --- /dev/null +++ b/pkg/internal/models/sign_record.go @@ -0,0 +1,10 @@ +package models + +type SignRecord struct { + BaseModel + + ResultTier int `json:"result_tier"` + ResultExperience int `json:"result_experience"` + Account Account `json:"account"` + AccountID uint `json:"account_id"` +} diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index db04fdc..a6c9831 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -9,6 +9,12 @@ func MapAPIs(app *fiber.App, baseURL string) { api := app.Group(baseURL).Name("API") { + daily := api.Group("/daily").Name("Daily Sign API") + { + daily.Get("/", listDailySignRecord) + daily.Post("/", doDailySign) + } + notify := api.Group("/notifications").Name("Notifications API") { notify.Get("/", getNotifications) diff --git a/pkg/internal/server/api/sign_api.go b/pkg/internal/server/api/sign_api.go new file mode 100644 index 0000000..c8765be --- /dev/null +++ b/pkg/internal/server/api/sign_api.go @@ -0,0 +1,54 @@ +package api + +import ( + "git.solsynth.dev/hydrogen/passport/pkg/internal/database" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" + "git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" + "git.solsynth.dev/hydrogen/passport/pkg/internal/services" + "github.com/gofiber/fiber/v2" +) + +func listDailySignRecord(c *fiber.Ctx) error { + take := c.QueryInt("take", 0) + offset := c.QueryInt("offset", 0) + + if err := exts.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + + var count int64 + if err := database.C. + Model(&models.Account{}). + Where("account_id = ?", user.ID). + Count(&count).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + var records []models.SignRecord + if err := database.C. + Where("account_id = ?", user.ID). + Limit(take).Offset(offset). + Order("created_at DESC"). + Find(&records).Error; err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(fiber.Map{ + "count": count, + "data": records, + }) +} + +func doDailySign(c *fiber.Ctx) error { + if err := exts.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + + if record, err := services.DailySign(user); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.JSON(record) + } +} diff --git a/pkg/internal/services/sign.go b/pkg/internal/services/sign.go new file mode 100644 index 0000000..9b3d824 --- /dev/null +++ b/pkg/internal/services/sign.go @@ -0,0 +1,40 @@ +package services + +import ( + "errors" + "fmt" + "git.solsynth.dev/hydrogen/passport/pkg/internal/database" + "git.solsynth.dev/hydrogen/passport/pkg/internal/models" + "gorm.io/gorm" + "math/rand" +) + +func CheckDailyCanSign(user models.Account) error { + var record models.SignRecord + if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID).First(&record).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil + } + return fmt.Errorf("unable check daliy sign record: %v", err) + } + return fmt.Errorf("daliy sign record exists") +} + +func DailySign(user models.Account) (models.SignRecord, error) { + tier := rand.Intn(5) + record := models.SignRecord{ + ResultTier: tier, + ResultExperience: rand.Intn(tier*100) + 100, + AccountID: user.ID, + } + + if err := CheckDailyCanSign(user); err != nil { + return record, fmt.Errorf("today already signed") + } + + if err := database.C.Save(&record).Error; err != nil { + return record, fmt.Errorf("unable do daliy sign: %v", err) + } + + return record, nil +}