mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
Add link support to resource creation and editing, including validation
This commit is contained in:
@@ -24,7 +24,7 @@ func updateSiteMapAndRss(baseURL string) {
|
||||
}
|
||||
|
||||
func handleCreateResource(c fiber.Ctx) error {
|
||||
var params service.ResourceCreateParams
|
||||
var params service.ResourceParams
|
||||
body := c.Body()
|
||||
err := json.Unmarshal(body, ¶ms)
|
||||
if err != nil {
|
||||
@@ -229,7 +229,7 @@ func handleUpdateResource(c fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid resource ID")
|
||||
}
|
||||
var params service.ResourceCreateParams
|
||||
var params service.ResourceParams
|
||||
body := c.Body()
|
||||
err = json.Unmarshal(body, ¶ms)
|
||||
if err != nil {
|
||||
|
@@ -10,6 +10,7 @@ type Resource struct {
|
||||
gorm.Model
|
||||
Title string
|
||||
AlternativeTitles []string `gorm:"serializer:json"`
|
||||
Links []Link `gorm:"serializer:json"`
|
||||
Article string
|
||||
Images []Image `gorm:"many2many:resource_images;"`
|
||||
Tags []Tag `gorm:"many2many:resource_tags;"`
|
||||
@@ -20,6 +21,11 @@ type Resource struct {
|
||||
Downloads uint
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
URL string `json:"url"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
|
||||
type ResourceView struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
@@ -33,6 +39,7 @@ type ResourceDetailView struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
AlternativeTitles []string `json:"alternativeTitles"`
|
||||
Links []Link `json:"links"`
|
||||
Article string `json:"article"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Tags []TagView `json:"tags"`
|
||||
@@ -84,6 +91,7 @@ func (r *Resource) ToDetailView() ResourceDetailView {
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
AlternativeTitles: r.AlternativeTitles,
|
||||
Links: r.Links,
|
||||
Article: r.Article,
|
||||
CreatedAt: r.CreatedAt,
|
||||
Tags: tags,
|
||||
|
@@ -12,15 +12,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ResourceCreateParams struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
AlternativeTitles []string `json:"alternative_titles"`
|
||||
Tags []uint `json:"tags"`
|
||||
Article string `json:"article"`
|
||||
Images []uint `json:"images"`
|
||||
type ResourceParams struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
AlternativeTitles []string `json:"alternative_titles"`
|
||||
Links []model.Link `json:"links"`
|
||||
Tags []uint `json:"tags"`
|
||||
Article string `json:"article"`
|
||||
Images []uint `json:"images"`
|
||||
}
|
||||
|
||||
func CreateResource(uid uint, params *ResourceCreateParams) (uint, error) {
|
||||
func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
canUpload, err := checkUserCanUpload(uid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -49,6 +50,7 @@ func CreateResource(uid uint, params *ResourceCreateParams) (uint, error) {
|
||||
Title: params.Title,
|
||||
AlternativeTitles: params.AlternativeTitles,
|
||||
Article: params.Article,
|
||||
Links: params.Links,
|
||||
Images: images,
|
||||
Tags: tags,
|
||||
UserID: uid,
|
||||
@@ -213,7 +215,7 @@ func GetResourcesWithUser(username string, page int) ([]model.ResourceView, int,
|
||||
return views, totalPages, nil
|
||||
}
|
||||
|
||||
func EditResource(uid, rid uint, params *ResourceCreateParams) error {
|
||||
func EditResource(uid, rid uint, params *ResourceParams) error {
|
||||
isAdmin, err := checkUserCanUpload(uid)
|
||||
if err != nil {
|
||||
log.Error("checkUserCanUpload error: ", err)
|
||||
@@ -230,6 +232,7 @@ func EditResource(uid, rid uint, params *ResourceCreateParams) error {
|
||||
r.Title = params.Title
|
||||
r.AlternativeTitles = params.AlternativeTitles
|
||||
r.Article = params.Article
|
||||
r.Links = params.Links
|
||||
|
||||
images := make([]model.Image, len(params.Images))
|
||||
for i, id := range params.Images {
|
||||
|
Reference in New Issue
Block a user