✨ Can get today's daily sign record
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user