Daily signs

This commit is contained in:
LittleSheep 2024-09-01 16:38:09 +08:00
parent f240226563
commit 99f8e4c891
6 changed files with 122 additions and 7 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="dataSourceStorageLocal" created-in="GO-242.20224.424"> <component name="dataSourceStorageLocal" created-in="GO-242.21829.165">
<data-source name="hy_passport@localhost" uuid="74bcf3ef-a2b9-435b-b9e5-f32902a33b25"> <data-source name="hy_passport@localhost" uuid="74bcf3ef-a2b9-435b-b9e5-f32902a33b25">
<database-info product="PostgreSQL" version="16.3 (Homebrew)" jdbc-version="4.2" driver-name="PostgreSQL JDBC Driver" driver-version="42.6.0" dbms="POSTGRES" exact-version="16.3" exact-driver-version="42.6"> <database-info product="PostgreSQL" version="16.3 (Homebrew)" jdbc-version="4.2" driver-name="PostgreSQL JDBC Driver" driver-version="42.6.0" dbms="POSTGRES" exact-version="16.3" exact-driver-version="42.6">
<identifier-quote-string>&quot;</identifier-quote-string> <identifier-quote-string>&quot;</identifier-quote-string>

View File

@ -4,8 +4,13 @@
<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=":bug: Fix bot related key api issue"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix query statement column issue">
<change beforePath="$PROJECT_DIR$/pkg/internal/services/bot_token.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/bot_token.go" afterDir="false" /> <change afterPath="$PROJECT_DIR$/pkg/internal/models/sign_record.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pkg/internal/server/api/sign_api.go" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pkg/internal/services/sign.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/dataSources.local.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources.local.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" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -111,8 +116,8 @@
<component name="SharedIndexes"> <component name="SharedIndexes">
<attachedChunks> <attachedChunks>
<set> <set>
<option value="bundled-gosdk-5df93f7ad4aa-dfc284eb1eb8-org.jetbrains.plugins.go.sharedIndexes.bundled-GO-242.20224.424" /> <option value="bundled-gosdk-5df93f7ad4aa-df9ad98b711f-org.jetbrains.plugins.go.sharedIndexes.bundled-GO-242.21829.165" />
<option value="bundled-js-predefined-d6986cc7102b-410509235cf1-JavaScript-GO-242.20224.424" /> <option value="bundled-js-predefined-d6986cc7102b-7c0b70fcd90d-JavaScript-GO-242.21829.165" />
</set> </set>
</attachedChunks> </attachedChunks>
</component> </component>
@ -152,7 +157,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":bug: Fix push notification to wrong person" />
<MESSAGE value=":sparkles: Account groups" /> <MESSAGE value=":sparkles: Account groups" />
<MESSAGE value=":sparkles: Default user group" /> <MESSAGE value=":sparkles: Default user group" />
<MESSAGE value=":bug: Fix permissions in groups" /> <MESSAGE value=":bug: Fix permissions in groups" />
@ -177,7 +181,8 @@
<MESSAGE value=":sparkles: Preload api key's ticket" /> <MESSAGE value=":sparkles: Preload api key's ticket" />
<MESSAGE value=":bug: Fix preloading issue" /> <MESSAGE value=":bug: Fix preloading issue" />
<MESSAGE value=":bug: Fix bot related key api issue" /> <MESSAGE value=":bug: Fix bot related key api issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix bot related key api issue" /> <MESSAGE value=":bug: Fix query statement column issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix query statement column issue" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -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"`
}

View File

@ -9,6 +9,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
api := app.Group(baseURL).Name("API") 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 := api.Group("/notifications").Name("Notifications API")
{ {
notify.Get("/", getNotifications) notify.Get("/", getNotifications)

View File

@ -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)
}
}

View File

@ -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
}