diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index aac6aa9..81822e6 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,10 @@
-
+
+
+
@@ -153,7 +155,6 @@
-
@@ -178,7 +179,8 @@
-
+
+
true
diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go
index a6c9831..3fa6beb 100644
--- a/pkg/internal/server/api/index.go
+++ b/pkg/internal/server/api/index.go
@@ -12,6 +12,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
daily := api.Group("/daily").Name("Daily Sign API")
{
daily.Get("/", listDailySignRecord)
+ daily.Get("/today", getTodayDailySign)
daily.Post("/", doDailySign)
}
diff --git a/pkg/internal/server/api/sign_api.go b/pkg/internal/server/api/sign_api.go
index c8765be..dcb9d03 100644
--- a/pkg/internal/server/api/sign_api.go
+++ b/pkg/internal/server/api/sign_api.go
@@ -40,6 +40,19 @@ func listDailySignRecord(c *fiber.Ctx) error {
})
}
+func getTodayDailySign(c *fiber.Ctx) error {
+ if err := exts.EnsureAuthenticated(c); err != nil {
+ return err
+ }
+ user := c.Locals("user").(models.Account)
+
+ if record, err := services.GetTodayDailySign(user); err != nil {
+ return fiber.NewError(fiber.StatusNotFound, err.Error())
+ } else {
+ return c.JSON(record)
+ }
+}
+
func doDailySign(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
diff --git a/pkg/internal/services/sign.go b/pkg/internal/services/sign.go
index 8db8ac9..aeb9069 100644
--- a/pkg/internal/services/sign.go
+++ b/pkg/internal/services/sign.go
@@ -23,6 +23,16 @@ func CheckDailyCanSign(user models.Account) error {
return fmt.Errorf("daliy sign record exists")
}
+func GetTodayDailySign(user models.Account) (models.SignRecord, error) {
+ probe := time.Now().Format("YYYY-MM-DD")
+
+ var record models.SignRecord
+ if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
+ return record, fmt.Errorf("unable get daliy sign record: %v", err)
+ }
+ return record, nil
+}
+
func DailySign(user models.Account) (models.SignRecord, error) {
tier := rand.Intn(5)
record := models.SignRecord{
@@ -35,6 +45,14 @@ func DailySign(user models.Account) (models.SignRecord, error) {
return record, fmt.Errorf("today already signed")
}
+ var profile models.AccountProfile
+ if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
+ return record, fmt.Errorf("unable get account profile: %v", err)
+ } else {
+ profile.Experience += uint64(record.ResultExperience)
+ database.C.Save(&profile)
+ }
+
if err := database.C.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do daliy sign: %v", err)
}