Interactive/pkg/server/moments_api.go

147 lines
4.2 KiB
Go
Raw Normal View History

2024-02-02 15:42:42 +00:00
package server
import (
2024-02-03 16:24:04 +00:00
"strings"
2024-02-03 17:08:31 +00:00
"time"
2024-02-03 16:24:04 +00:00
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-02 15:42:42 +00:00
"github.com/gofiber/fiber/v2"
2024-02-02 16:50:23 +00:00
"github.com/google/uuid"
2024-02-02 15:42:42 +00:00
)
func contextMoment() *services.PostTypeContext {
return &services.PostTypeContext{
Tx: database.C,
TableName: "moments",
ColumnName: "moment",
CanReply: false,
CanRepost: true,
2024-02-03 11:22:50 +00:00
}
2024-03-02 17:23:11 +00:00
}
2024-02-02 15:42:42 +00:00
2024-03-02 17:23:11 +00:00
func createMoment(c *fiber.Ctx) error {
2024-02-02 15:42:42 +00:00
user := c.Locals("principal").(models.Account)
var data struct {
2024-03-07 14:41:00 +00:00
Alias string `json:"alias" form:"alias"`
2024-03-10 15:35:38 +00:00
Content string `json:"content" form:"content" validate:"required,max=1024"`
2024-03-07 14:41:00 +00:00
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
Categories []models.Category `json:"categories" form:"categories"`
Attachments []models.Attachment `json:"attachments" form:"attachments"`
PublishedAt *time.Time `json:"published_at" form:"published_at"`
RealmID *uint `json:"realm_id" form:"realm_id"`
RepostTo uint `json:"repost_to" form:"repost_to"`
2024-02-02 15:42:42 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
2024-02-02 16:50:23 +00:00
} else if len(data.Alias) == 0 {
data.Alias = strings.ReplaceAll(uuid.NewString(), "-", "")
2024-02-02 15:42:42 +00:00
}
2024-03-03 14:57:17 +00:00
item := &models.Moment{
2024-03-02 17:23:11 +00:00
PostBase: models.PostBase{
Alias: data.Alias,
PublishedAt: data.PublishedAt,
AuthorID: user.ID,
},
2024-03-10 10:38:42 +00:00
Hashtags: data.Hashtags,
Categories: data.Categories,
Attachments: data.Attachments,
Content: data.Content,
RealmID: data.RealmID,
2024-03-02 17:23:11 +00:00
}
2024-02-03 16:24:04 +00:00
var relatedCount int64
if data.RepostTo > 0 {
2024-03-02 17:23:11 +00:00
if err := database.C.Where("id = ?", data.RepostTo).
Model(&models.Moment{}).Count(&relatedCount).Error; err != nil {
2024-02-03 16:24:04 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else if relatedCount <= 0 {
return fiber.NewError(fiber.StatusNotFound, "related post was not found")
} else {
2024-03-02 17:23:11 +00:00
item.RepostID = &data.RepostTo
2024-02-03 16:24:04 +00:00
}
}
2024-02-05 11:25:56 +00:00
var realm *models.Realm
if data.RealmID != nil {
if err := database.C.Where(&models.Realm{
BaseModel: models.BaseModel{ID: *data.RealmID},
}).First(&realm).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
}
item, err := services.NewPost(item)
2024-02-03 17:08:31 +00:00
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
2024-03-02 17:23:11 +00:00
return c.JSON(item)
2024-02-03 17:08:31 +00:00
}
2024-03-02 17:23:11 +00:00
func editMoment(c *fiber.Ctx) error {
2024-02-03 17:08:31 +00:00
user := c.Locals("principal").(models.Account)
2024-03-02 17:23:11 +00:00
id, _ := c.ParamsInt("momentId", 0)
2024-02-03 17:08:31 +00:00
var data struct {
2024-03-07 14:41:00 +00:00
Alias string `json:"alias" form:"alias" validate:"required"`
2024-03-10 15:35:38 +00:00
Content string `json:"content" form:"content" validate:"required,max=1024"`
2024-03-07 14:41:00 +00:00
PublishedAt *time.Time `json:"published_at" form:"published_at"`
Hashtags []models.Tag `json:"hashtags" form:"hashtags"`
Categories []models.Category `json:"categories" form:"categories"`
Attachments []models.Attachment `json:"attachments" form:"attachments"`
2024-02-03 17:08:31 +00:00
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
var item *models.Moment
if err := database.C.Where(models.Comment{
PostBase: models.PostBase{
BaseModel: models.BaseModel{ID: uint(id)},
AuthorID: user.ID,
},
}).First(&item).Error; err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-03-02 17:23:11 +00:00
item.Alias = data.Alias
item.Content = data.Content
item.PublishedAt = data.PublishedAt
item.Hashtags = data.Hashtags
item.Categories = data.Categories
item.Attachments = data.Attachments
if item, err := services.EditPost(item); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(item)
}
2024-02-02 16:50:23 +00:00
}
2024-02-03 17:08:31 +00:00
2024-03-02 17:23:11 +00:00
func deleteMoment(c *fiber.Ctx) error {
2024-02-03 17:08:31 +00:00
user := c.Locals("principal").(models.Account)
2024-03-02 17:23:11 +00:00
id, _ := c.ParamsInt("momentId", 0)
2024-02-03 17:08:31 +00:00
var item *models.Moment
if err := database.C.Where(models.Comment{
PostBase: models.PostBase{
BaseModel: models.BaseModel{ID: uint(id)},
AuthorID: user.ID,
},
}).First(&item).Error; err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := services.DeletePost(item); err != nil {
2024-02-03 17:08:31 +00:00
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}