✨ Account page (aka detailed version description)
This commit is contained in:
parent
5979fd5b2c
commit
0fac34edfb
@ -8,6 +8,20 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func listUserBadge(c *fiber.Ctx) error {
|
||||||
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
|
var badges []models.Badge
|
||||||
|
if err := database.C.Where("account_id = ?", user.ID).Find(&badges).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(badges)
|
||||||
|
}
|
||||||
|
|
||||||
func activeUserBadge(c *fiber.Ctx) error {
|
func activeUserBadge(c *fiber.Ctx) error {
|
||||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -39,6 +39,7 @@ func MapControllers(app *fiber.App, baseURL string) {
|
|||||||
|
|
||||||
badges := api.Group("/badges").Name("Badges")
|
badges := api.Group("/badges").Name("Badges")
|
||||||
{
|
{
|
||||||
|
badges.Get("/me", listUserBadge)
|
||||||
badges.Post("/:badgeId/active", activeUserBadge)
|
badges.Post("/:badgeId/active", activeUserBadge)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +80,9 @@ func MapControllers(app *fiber.App, baseURL string) {
|
|||||||
me.Put("/status", editStatus)
|
me.Put("/status", editStatus)
|
||||||
me.Delete("/status", clearStatus)
|
me.Delete("/status", clearStatus)
|
||||||
|
|
||||||
|
me.Get("/pages", getOwnAccountPage)
|
||||||
|
me.Put("/pages", updateAccountPage)
|
||||||
|
|
||||||
contacts := me.Group("/contacts").Name("Contacts")
|
contacts := me.Group("/contacts").Name("Contacts")
|
||||||
{
|
{
|
||||||
contacts.Get("/", listContact)
|
contacts.Get("/", listContact)
|
||||||
@ -122,6 +126,7 @@ func MapControllers(app *fiber.App, baseURL string) {
|
|||||||
{
|
{
|
||||||
directory.Get("/", getOtherUserinfo)
|
directory.Get("/", getOtherUserinfo)
|
||||||
directory.Get("/status", getStatus)
|
directory.Get("/status", getStatus)
|
||||||
|
directory.Get("/pages", getAccountPage)
|
||||||
|
|
||||||
directory.Get("/check-in", listOtherUserCheckInRecord)
|
directory.Get("/check-in", listOtherUserCheckInRecord)
|
||||||
}
|
}
|
||||||
|
78
pkg/internal/web/api/pages_api.go
Normal file
78
pkg/internal/web/api/pages_api.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||||
|
"git.solsynth.dev/hypernet/passport/pkg/internal/web/exts"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getAccountPage(c *fiber.Ctx) error {
|
||||||
|
alias := c.Params("alias")
|
||||||
|
|
||||||
|
tx := database.C.Where("name = ?", alias)
|
||||||
|
|
||||||
|
numericId, err := strconv.Atoi(alias)
|
||||||
|
if err == nil {
|
||||||
|
tx = tx.Or("id = ?", numericId)
|
||||||
|
}
|
||||||
|
|
||||||
|
var account models.Account
|
||||||
|
if err := tx.First(&account).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var page models.AccountPage
|
||||||
|
if err := database.C.Where("account_id = ?", account.ID).First(&page).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(page)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOwnAccountPage(c *fiber.Ctx) error {
|
||||||
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
|
var page models.AccountPage
|
||||||
|
if err := database.C.Where("account_id = ?", user.ID).First(&page).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(page)
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateAccountPage(c *fiber.Ctx) error {
|
||||||
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
user := c.Locals("user").(models.Account)
|
||||||
|
|
||||||
|
var data struct {
|
||||||
|
Content string `json:"content" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var page models.AccountPage
|
||||||
|
if err := database.C.Where("account_id = ?", user.ID).First(&page).Error; err != nil {
|
||||||
|
page = models.AccountPage{
|
||||||
|
AccountID: user.ID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
page.Content = data.Content
|
||||||
|
|
||||||
|
if err := database.C.Save(&page).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(page)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user