🎉 Initial Commit
This commit is contained in:
29
pkg/internal/server/api/index.go
Normal file
29
pkg/internal/server/api/index.go
Normal file
@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func MapAPIs(app *fiber.App, baseURL string) {
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
products := api.Group("/products")
|
||||
{
|
||||
products.Get("/", listProduct)
|
||||
products.Get("/created", listCreatedProduct)
|
||||
products.Get("/:productId", getProduct)
|
||||
products.Post("/", createProduct)
|
||||
products.Put("/:productId", updateProduct)
|
||||
products.Delete("/:productId", deleteProduct)
|
||||
|
||||
releases := products.Group("/:productId/releases")
|
||||
{
|
||||
releases.Get("/", listRelease)
|
||||
releases.Get("/:releaseId", getRelease)
|
||||
releases.Post("/", createRelease)
|
||||
releases.Put("/:releaseId", updateRelease)
|
||||
releases.Delete("/:releaseId", deleteRelease)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
175
pkg/internal/server/api/products_api.go
Normal file
175
pkg/internal/server/api/products_api.go
Normal file
@ -0,0 +1,175 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/models"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func listProduct(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
count, err := services.CountProduct()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListProduct(take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func listCreatedProduct(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
|
||||
count, err := services.CountCreatedProduct(user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListCreatedProduct(user.ID, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func getProduct(c *fiber.Ctx) error {
|
||||
alias := c.Params("productId")
|
||||
|
||||
var item models.Product
|
||||
if numericId, err := strconv.Atoi(alias); err == nil {
|
||||
item, err = services.GetProduct(uint(numericId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
} else {
|
||||
item, err = services.GetProduct(uint(numericId))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
func createProduct(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Name string `json:"name" validate:"required,max=256"`
|
||||
Description string `json:"description" validate:"max=4096"`
|
||||
Introduction string `json:"introduction"`
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product := models.Product{
|
||||
Name: data.Name,
|
||||
Alias: data.Alias,
|
||||
Description: data.Description,
|
||||
Tags: data.Tags,
|
||||
Meta: models.ProductMeta{
|
||||
Introduction: data.Introduction,
|
||||
Attachments: data.Attachments,
|
||||
},
|
||||
AccountID: user.ID,
|
||||
}
|
||||
|
||||
if product, err := services.NewProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(product)
|
||||
}
|
||||
}
|
||||
|
||||
func updateProduct(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name" validate:"required,max=256"`
|
||||
Description string `json:"description" validate:"max=4096"`
|
||||
Introduction string `json:"introduction"`
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Previews []string `json:"previews"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
product.Icon = data.Icon
|
||||
product.Name = data.Name
|
||||
product.Description = data.Description
|
||||
product.Tags = data.Tags
|
||||
product.Previews = data.Previews
|
||||
product.Meta.Introduction = data.Introduction
|
||||
product.Meta.Attachments = data.Attachments
|
||||
|
||||
if product, err := services.UpdateProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(product)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteProduct(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
product, err := services.GetProductWithUser(uint(id), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if _, err := services.DeleteProduct(product); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
167
pkg/internal/server/api/releases_api.go
Normal file
167
pkg/internal/server/api/releases_api.go
Normal file
@ -0,0 +1,167 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/models"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/matrix/nucleus/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func listRelease(c *fiber.Ctx) error {
|
||||
take := c.QueryInt("take", 0)
|
||||
offset := c.QueryInt("offset", 0)
|
||||
id, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
count, err := services.CountRelease(id)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
items, err := services.ListRelease(id, take, offset)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"count": count,
|
||||
"data": items,
|
||||
})
|
||||
}
|
||||
|
||||
func getRelease(c *fiber.Ctx) error {
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
if item, err := services.GetReleaseWithProduct(uint(id), uint(productId)); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
return c.JSON(item)
|
||||
}
|
||||
}
|
||||
|
||||
func createRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
|
||||
var data struct {
|
||||
Version string `json:"version" validate:"required"`
|
||||
Type int `json:"type" validate:"required"`
|
||||
Channel string `json:"channel" validate:"required"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description string `json:"description" validate:"required,max=4096"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Assets map[string]any `json:"assets" validate:"required"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release := models.ProductRelease{
|
||||
Version: data.Version,
|
||||
Type: models.ProductReleaseType(data.Type),
|
||||
Channel: data.Channel,
|
||||
Assets: datatypes.NewJSONType(data.Assets),
|
||||
ProductID: product.ID,
|
||||
Meta: models.ProductReleaseMeta{
|
||||
Title: data.Title,
|
||||
Description: data.Description,
|
||||
Content: data.Content,
|
||||
Attachments: data.Attachments,
|
||||
},
|
||||
}
|
||||
|
||||
if release, err := services.NewRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(release)
|
||||
}
|
||||
}
|
||||
|
||||
func updateRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
var data struct {
|
||||
Version string `json:"version" validate:"required"`
|
||||
Type int `json:"type" validate:"required"`
|
||||
Channel string `json:"channel" validate:"required"`
|
||||
Title string `json:"title" validate:"required,max=1024"`
|
||||
Description string `json:"description" validate:"required,max=4096"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Assets map[string]any `json:"assets" validate:"required"`
|
||||
Attachments []string `json:"attachments"`
|
||||
}
|
||||
|
||||
if err := exts.BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release, err := services.GetReleaseWithProduct(uint(id), product.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
release.Version = data.Version
|
||||
release.Type = models.ProductReleaseType(data.Type)
|
||||
release.Channel = data.Channel
|
||||
release.Assets = datatypes.NewJSONType(data.Assets)
|
||||
release.Meta.Title = data.Title
|
||||
release.Meta.Description = data.Description
|
||||
release.Meta.Content = data.Content
|
||||
release.Meta.Attachments = data.Attachments
|
||||
|
||||
if release, err := services.UpdateRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.JSON(release)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteRelease(c *fiber.Ctx) error {
|
||||
if err := sec.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("nex_user").(*sec.UserInfo)
|
||||
|
||||
productId, _ := c.ParamsInt("productId", 0)
|
||||
id, _ := c.ParamsInt("releaseId", 0)
|
||||
|
||||
product, err := services.GetProductWithUser(uint(productId), user.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
release, err := services.GetReleaseWithProduct(uint(id), product.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if _, err := services.DeleteRelease(release); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user