Interactive/pkg/server/users_api.go

77 lines
2.1 KiB
Go
Raw Normal View History

2024-02-01 16:53:22 +00:00
package server
import (
2024-03-20 12:57:21 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/database"
"git.solsynth.dev/hydrogen/interactive/pkg/models"
"git.solsynth.dev/hydrogen/interactive/pkg/services"
2024-02-01 16:53:22 +00:00
"github.com/gofiber/fiber/v2"
)
func getUserinfo(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
2024-02-03 07:20:32 +00:00
2024-02-03 11:22:50 +00:00
func getOthersInfo(c *fiber.Ctx) error {
2024-02-05 07:51:31 +00:00
accountId := c.Params("accountId")
2024-02-03 11:22:50 +00:00
var data models.Account
if err := database.C.
2024-02-05 07:51:31 +00:00
Where(&models.Account{Name: accountId}).
2024-02-03 11:22:50 +00:00
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
2024-02-03 12:44:16 +00:00
func getAccountFollowed(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
accountId, _ := c.ParamsInt("accountId", 0)
var data models.Account
if err := database.C.
Where(&models.Account{BaseModel: models.BaseModel{ID: uint(accountId)}}).
First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
_, status := services.GetAccountFollowed(user, data)
return c.JSON(fiber.Map{
"is_followed": status,
})
}
2024-02-03 07:20:32 +00:00
func doFollowAccount(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
id, _ := c.ParamsInt("accountId", 0)
var account models.Account
if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: uint(id)},
}).First(&account).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if _, ok := services.GetAccountFollowed(user, account); ok {
if err := services.UnfollowAccount(user.ID, account.ID); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusNoContent)
} else {
if err := services.FollowAccount(user.ID, account.ID); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusCreated)
}
}