數據集
This commit is contained in:
@@ -13,9 +13,10 @@ TEST:
|
|||||||
- [x] 登錄賬戶(創建會話)
|
- [x] 登錄賬戶(創建會話)
|
||||||
- [x] 註銷登錄(刪除指定會話)
|
- [x] 註銷登錄(刪除指定會話)
|
||||||
- [x] GET [/api/dataset](/api/dataset) 數據集列表
|
- [x] GET [/api/dataset](/api/dataset) 數據集列表
|
||||||
- [ ] 創建數據集
|
- [x] 創建數據集
|
||||||
- [ ] 刪除數據集
|
- [x] 刪除數據集
|
||||||
- [ ] 修改數據集
|
- [x] 修改數據集
|
||||||
|
- [x] 查詢數據集
|
||||||
- [x] GET [/api/servers](/api/servers) 服務器列表
|
- [x] GET [/api/servers](/api/servers) 服務器列表
|
||||||
- [x] GET [/api/models](/api/models) 模型列表
|
- [x] GET [/api/models](/api/models) 模型列表
|
||||||
- [x] GET [/api/images](/api/images) 圖片列表
|
- [x] GET [/api/images](/api/images) 圖片列表
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"main/configs"
|
"main/configs"
|
||||||
@@ -12,14 +13,24 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"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 {
|
type Dataset struct {
|
||||||
ID int `json:"id" gorm:"primary_key"`
|
ID int `json:"id" gorm:"primary_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type string `json:"type"`
|
|
||||||
Info string `json:"info"`
|
Info string `json:"info"`
|
||||||
|
Images ImageList `json:"images"`
|
||||||
UserID int `json:"user_id"`
|
UserID int `json:"user_id"`
|
||||||
CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||||
UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -35,6 +46,9 @@ func DatasetsGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
db := configs.ORMDB()
|
db := configs.ORMDB()
|
||||||
db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&dataset_list)
|
db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&dataset_list)
|
||||||
for _, dataset := range dataset_list {
|
for _, dataset := range dataset_list {
|
||||||
|
if dataset.Images == nil {
|
||||||
|
dataset.Images = ImageList{}
|
||||||
|
}
|
||||||
listview.List = append(listview.List, dataset)
|
listview.List = append(listview.List, dataset)
|
||||||
}
|
}
|
||||||
db.Model(&Dataset{}).Count(&listview.Total)
|
db.Model(&Dataset{}).Count(&listview.Total)
|
||||||
@@ -42,7 +56,7 @@ func DatasetsGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
listview.WriteJSON(w)
|
listview.WriteJSON(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新增數據集
|
// 創建數據集
|
||||||
func DatasetsPost(w http.ResponseWriter, r *http.Request) {
|
func DatasetsPost(w http.ResponseWriter, r *http.Request) {
|
||||||
models.AccountRead(w, r, func(account *models.Account) {
|
models.AccountRead(w, r, func(account *models.Account) {
|
||||||
var dataset Dataset
|
var dataset Dataset
|
||||||
@@ -58,6 +72,9 @@ func DatasetsPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte("500 - Internal Server Error"))
|
w.Write([]byte("500 - Internal Server Error"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if dataset.Images == nil {
|
||||||
|
dataset.Images = ImageList{}
|
||||||
|
}
|
||||||
dataset.UserID = account.ID
|
dataset.UserID = account.ID
|
||||||
if err := configs.ORMDB().Create(&dataset).Error; err != nil {
|
if err := configs.ORMDB().Create(&dataset).Error; err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
Reference in New Issue
Block a user