From e946608376e04788551393c61f998af168df3fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Wed, 17 May 2023 00:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B8=E6=93=9A=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++--- routers/datasets.go | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5637595..fdf7af9 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ TEST: - [x] 登錄賬戶(創建會話) - [x] 註銷登錄(刪除指定會話) - [x] GET [/api/dataset](/api/dataset) 數據集列表 - - [ ] 創建數據集 - - [ ] 刪除數據集 - - [ ] 修改數據集 + - [x] 創建數據集 + - [x] 刪除數據集 + - [x] 修改數據集 + - [x] 查詢數據集 - [x] GET [/api/servers](/api/servers) 服務器列表 - [x] GET [/api/models](/api/models) 模型列表 - [x] GET [/api/images](/api/images) 圖片列表 diff --git a/routers/datasets.go b/routers/datasets.go index 598e2f2..ccd15ba 100644 --- a/routers/datasets.go +++ b/routers/datasets.go @@ -1,6 +1,7 @@ package routers import ( + "database/sql/driver" "encoding/json" "io/ioutil" "main/configs" @@ -12,14 +13,24 @@ import ( "github.com/gorilla/mux" ) +type ImageList []string + +func (list *ImageList) Scan(value interface{}) error { + return json.Unmarshal(value.([]byte), list) +} +func (list ImageList) Value() (driver.Value, error) { + return json.Marshal(list) +} + +// 數據集 type Dataset struct { ID int `json:"id" gorm:"primary_key"` Name string `json:"name"` - Type string `json:"type"` Info string `json:"info"` + Images ImageList `json:"images"` UserID int `json:"user_id"` - CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"` - UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"` + CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` } func init() { @@ -35,6 +46,9 @@ func DatasetsGet(w http.ResponseWriter, r *http.Request) { db := configs.ORMDB() db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&dataset_list) for _, dataset := range dataset_list { + if dataset.Images == nil { + dataset.Images = ImageList{} + } listview.List = append(listview.List, dataset) } db.Model(&Dataset{}).Count(&listview.Total) @@ -42,7 +56,7 @@ func DatasetsGet(w http.ResponseWriter, r *http.Request) { listview.WriteJSON(w) } -// 新增數據集 +// 創建數據集 func DatasetsPost(w http.ResponseWriter, r *http.Request) { models.AccountRead(w, r, func(account *models.Account) { var dataset Dataset @@ -58,6 +72,9 @@ func DatasetsPost(w http.ResponseWriter, r *http.Request) { w.Write([]byte("500 - Internal Server Error")) return } + if dataset.Images == nil { + dataset.Images = ImageList{} + } dataset.UserID = account.ID if err := configs.ORMDB().Create(&dataset).Error; err != nil { w.WriteHeader(http.StatusInternalServerError)