2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-02-01 07:08:40 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-06-22 05:04:21 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
2024-02-01 07:08:40 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-04-20 11:04:33 +00:00
|
|
|
func getTickets(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-02-01 07:08:40 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
var count int64
|
2024-04-21 04:20:06 +00:00
|
|
|
var tickets []models.AuthTicket
|
2024-02-01 07:08:40 +00:00
|
|
|
if err := database.C.
|
2024-04-20 11:04:33 +00:00
|
|
|
Where(&models.AuthTicket{AccountID: user.ID}).
|
|
|
|
Model(&models.AuthTicket{}).
|
2024-02-01 07:08:40 +00:00
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.
|
2024-02-12 04:31:18 +00:00
|
|
|
Order("created_at desc").
|
2024-04-20 11:04:33 +00:00
|
|
|
Where(&models.AuthTicket{AccountID: user.ID}).
|
2024-02-01 07:08:40 +00:00
|
|
|
Limit(take).
|
|
|
|
Offset(offset).
|
2024-04-21 04:20:06 +00:00
|
|
|
Find(&tickets).Error; err != nil {
|
2024-02-01 07:08:40 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
2024-04-21 04:20:06 +00:00
|
|
|
"data": tickets,
|
2024-02-01 07:08:40 +00:00
|
|
|
})
|
|
|
|
}
|
2024-06-30 09:01:39 +00:00
|
|
|
|
|
|
|
func killTicket(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("ticketId", 0)
|
|
|
|
|
|
|
|
if err := database.C.Delete(&models.AuthTicket{}, &models.AuthTicket{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|