數據集

This commit is contained in:
2023-05-17 00:55:22 +08:00
parent 9aea72a47c
commit e946608376
2 changed files with 25 additions and 7 deletions

View File

@@ -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)