Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy feature to dev #5

Merged
merged 6 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/definition/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func GetDefinition(contentType string, language ...string) (ContentType, error)
if len(language) > 0 {
languageStr = language[0]
}

definition, ok := contentTypeDefinition[languageStr]
if !ok {
log.Println("Language " + languageStr + " doesn't exist. use default.")
Expand Down
14 changes: 14 additions & 0 deletions core/handler/content_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,17 @@ func SaveDraft(ctx context.Context, userId int, data string, contentType string,
)
return newVersion, nil
}

// Copy data
func Copy(ctx context.Context, content contenttype.ContentTyper, ctype string, userId, parentInt int) error {
inputs := InputMap{}
for _, field := range content.Definition().FieldMap {
identifier := field.Identifier
inputs[identifier] = content.Value(identifier)
}
_, errors := Create(ctx, userId, ctype, inputs, parentInt)
if errors != nil {
return fmt.Errorf("Got error copy content: %w", errors)
}
return nil
}
63 changes: 63 additions & 0 deletions rest/content_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package rest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -269,6 +270,66 @@ func Delete(w http.ResponseWriter, r *http.Request) {
WriteResponse(true, w)
}

//copy old data
func Copy(w http.ResponseWriter, r *http.Request) {
userID := CheckUserID(r.Context(), w)
if userID == 0 {
return
}

params := mux.Vars(r)
id := params["id"]
parent := params["parent"]

parentInt, err := strconv.Atoi(parent)
if err != nil {
HandleError(errors.New("parent id should be integer."), w)
return
}

// To number
idInt, err := strconv.Atoi(id)
if err != nil {
HandleError(errors.New("id should be integer."), w)
return
}
ctype := params["contenttype"]

if ctype == "" {
content, err := query.FetchByID(r.Context(), idInt)
if err != nil {
HandleError(fmt.Errorf("Failed to get content via content id: %w", err), w)
return
}
if content.GetCID() == 0 {
HandleError(fmt.Errorf("Got empty : %w", err), w)
return
}
err = handler.Copy(r.Context(), content, ctype, userID, parentInt)
if err != nil {
HandleError(err, w)
return
}
} else {
content, err := query.FetchByCID(r.Context(), ctype, idInt)
if err != nil {
HandleError(fmt.Errorf("Failed to get content via content id: %w", err), w)
return
}
if content.GetCID() == 0 {
HandleError(fmt.Errorf("Got empty content: %w", err), w)
return
}
err = handler.Copy(r.Context(), content, ctype, userID, parentInt)
if err != nil {
HandleError(err, w)
return
}
}

WriteResponse(true, w)
}

func init() {
RegisterRoute("/content/create/{contenttype}/{parent:[0-9]+}", Create, "POST")
RegisterRoute("/content/create/{contenttype}", Create, "POST")
Expand All @@ -278,4 +339,6 @@ func init() {
RegisterRoute("/content/delete", Delete)
RegisterRoute("/content/setpriority", SetPriority)
RegisterRoute("/content/savedraft/{id:[0-9]+}/{type}", SaveDraft, "POST")
RegisterRoute("/content/Copy/{id:[0-9]+}/{parent:[0-9]+}", Copy, "POST")
RegisterRoute("/content/Copy/{contenttype}/{id:[0-9]+}/{parent:[0-9]+}", Copy, "POST")
}