Can get today's daily sign record

This commit is contained in:
LittleSheep 2024-09-02 20:07:19 +08:00
parent bee5b676fa
commit bfddfa2201
4 changed files with 37 additions and 3 deletions

View File

@ -4,8 +4,10 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Daily signs"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix daily check issue">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/sign_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/sign_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/sign.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/sign.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/sign.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/sign.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -153,7 +155,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Default user group" />
<MESSAGE value=":bug: Fix permissions in groups" /> <MESSAGE value=":bug: Fix permissions in groups" />
<MESSAGE value=":wastebasket: Clean up code" /> <MESSAGE value=":wastebasket: Clean up code" />
<MESSAGE value=":sparkles: Use capital to deal with links" /> <MESSAGE value=":sparkles: Use capital to deal with links" />
@ -178,7 +179,8 @@
<MESSAGE value=":bug: Fix bot related key api issue" /> <MESSAGE value=":bug: Fix bot related key api issue" />
<MESSAGE value=":bug: Fix query statement column issue" /> <MESSAGE value=":bug: Fix query statement column issue" />
<MESSAGE value=":sparkles: Daily signs" /> <MESSAGE value=":sparkles: Daily signs" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Daily signs" /> <MESSAGE value=":bug: Fix daily check issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix daily check issue" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -12,6 +12,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
daily := api.Group("/daily").Name("Daily Sign API") daily := api.Group("/daily").Name("Daily Sign API")
{ {
daily.Get("/", listDailySignRecord) daily.Get("/", listDailySignRecord)
daily.Get("/today", getTodayDailySign)
daily.Post("/", doDailySign) daily.Post("/", doDailySign)
} }

View File

@ -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 { func doDailySign(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil { if err := exts.EnsureAuthenticated(c); err != nil {
return err return err

View File

@ -23,6 +23,16 @@ func CheckDailyCanSign(user models.Account) error {
return fmt.Errorf("daliy sign record exists") 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) { func DailySign(user models.Account) (models.SignRecord, error) {
tier := rand.Intn(5) tier := rand.Intn(5)
record := models.SignRecord{ record := models.SignRecord{
@ -35,6 +45,14 @@ func DailySign(user models.Account) (models.SignRecord, error) {
return record, fmt.Errorf("today already signed") 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 { if err := database.C.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do daliy sign: %v", err) return record, fmt.Errorf("unable do daliy sign: %v", err)
} }