|
@@ -0,0 +1,154 @@
|
|
|
|
+package orm
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "net/http"
|
|
|
|
+
|
|
|
|
+ "github.com/jinzhu/gorm"
|
|
|
|
+
|
|
|
|
+ "gogs.carducci-dante.gov.it/karmen/core/renderer"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type FilePath struct {
|
|
|
|
+ gorm.Model
|
|
|
|
+
|
|
|
|
+ Path string
|
|
|
|
+ DocumentID uint
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Document struct {
|
|
|
|
+ gorm.Model
|
|
|
|
+
|
|
|
|
+ Name string
|
|
|
|
+ SurveyUrl string
|
|
|
|
+ ReferencePath string
|
|
|
|
+ TemplatePath string
|
|
|
|
+
|
|
|
|
+ Files []*FilePath
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type DocumentForUpdate struct {
|
|
|
|
+ Document Document
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type DocumentForAdd struct{}
|
|
|
|
+
|
|
|
|
+func (s *Document) GetID() uint { return s.ID }
|
|
|
|
+
|
|
|
|
+func GetDocument(args map[string]string) (interface{}, error) {
|
|
|
|
+ var document Document
|
|
|
|
+ if err := DB().First(&document, args["id"]).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return &document, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDocumentAll(args map[string]string) (interface{}, error) {
|
|
|
|
+ var document Document
|
|
|
|
+
|
|
|
|
+ id := args["id"]
|
|
|
|
+
|
|
|
|
+ if err := DB().Where("id = ?", id).Find(&document).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return &document, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDocuments(args map[string]string) (interface{}, error) {
|
|
|
|
+ var documents []*Document
|
|
|
|
+ if err := DB().Order("name").Find(&documents).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return documents, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDocumentsAll(args map[string]string) (interface{}, error) {
|
|
|
|
+ var documents []*Document
|
|
|
|
+ if err := DB().Order("name").Find(&documents).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return documents, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func SaveDocument(document interface{}) (interface{}, error) {
|
|
|
|
+ if err := DB().Save(document).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return document, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func UpdateDocument(args map[string]string, r *http.Request) (IDer, error) {
|
|
|
|
+ document, err := GetDocument(args)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = renderer.Decode(document, r)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ _, err = SaveDocument(document)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ document, err = GetDocument(args)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return document.(*Document), nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func AddDocument(args map[string]string, r *http.Request) (IDer, error) {
|
|
|
|
+ document := new(Document)
|
|
|
|
+ err := renderer.Decode(document, r)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ document, err = CreateDocument(document)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return document, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func CreateDocument(document *Document) (*Document, error) {
|
|
|
|
+ if err := DB().Create(document).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return document, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func DeleteDocument(args map[string]string, r *http.Request) (IDer, error) {
|
|
|
|
+ document, err := GetDocument(args)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ if err := DB().Unscoped().Delete(document.(*Document)).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ return document.(*Document), nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDocumentForUpdate(args map[string]string) (interface{}, error) {
|
|
|
|
+ var data DocumentForUpdate
|
|
|
|
+
|
|
|
|
+ id := args["id"]
|
|
|
|
+
|
|
|
|
+ if err := DB().First(&data.Document, id).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return data, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDocumentForAdd(args map[string]string) (interface{}, error) {
|
|
|
|
+ var data DocumentForAdd
|
|
|
|
+
|
|
|
|
+ return data, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (t *Document) RestAPIPath() string {
|
|
|
|
+ return "documents"
|
|
|
|
+}
|