✨ Daily signs
This commit is contained in:
@ -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)
|
||||
|
54
pkg/internal/server/api/sign_api.go
Normal file
54
pkg/internal/server/api/sign_api.go
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user