user UpdatedAt
This commit is contained in:
106
routers/dataset.go
Normal file
106
routers/dataset.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"main/configs"
|
||||
"main/models"
|
||||
"main/utils"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type Dataset struct {
|
||||
ID int `json:"id" gorm:"primary_key"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Info string `json:"info"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
configs.ORMDB().AutoMigrate(&Dataset{})
|
||||
}
|
||||
|
||||
func DatasetsGet(w http.ResponseWriter, r *http.Request) {
|
||||
var listview models.ListView
|
||||
listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1)
|
||||
listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10)
|
||||
var dataset_list []Dataset
|
||||
db := configs.ORMDB()
|
||||
db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&dataset_list)
|
||||
for _, dataset := range dataset_list {
|
||||
listview.List = append(listview.List, dataset)
|
||||
}
|
||||
db.Model(&Dataset{}).Count(&listview.Total)
|
||||
listview.Next = listview.Page*listview.PageSize < int(listview.Total)
|
||||
listview.WriteJSON(w)
|
||||
}
|
||||
|
||||
func DatasetsPost(w http.ResponseWriter, r *http.Request) {
|
||||
dataset := Dataset{}
|
||||
if err := configs.ORMDB().Create(&dataset).Error; err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("500 - Internal Server Error"))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(dataset))
|
||||
}
|
||||
|
||||
// 獲取數據集
|
||||
func DatasetsItemGet(w http.ResponseWriter, r *http.Request) {
|
||||
dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)}
|
||||
if err := configs.ORMDB().Find(&dataset).Error; err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("404 - Not Found"))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(dataset))
|
||||
}
|
||||
|
||||
func DatasetsItemPatch(w http.ResponseWriter, r *http.Request) {
|
||||
dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)}
|
||||
// 取出更新数据
|
||||
var dataset_new Dataset
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("500 - Internal Server Error"))
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if err = json.Unmarshal(body, &dataset_new); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("500 - Internal Server Error"))
|
||||
return
|
||||
}
|
||||
|
||||
// 合併字段
|
||||
if dataset_new.Name != "" {
|
||||
dataset.Name = dataset_new.Name
|
||||
}
|
||||
|
||||
// 執行更新
|
||||
if err := configs.ORMDB().Model(&dataset).Updates(dataset_new).Error; err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("404 - Not Found"))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Write(utils.ToJSON(dataset))
|
||||
}
|
||||
|
||||
func DatasetsItemDelete(w http.ResponseWriter, r *http.Request) {
|
||||
dataset := Dataset{ID: utils.ParamInt(mux.Vars(r)["dataset_id"], 0)}
|
||||
if err := configs.ORMDB().Delete(&dataset).Error; err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("404 - Not Found"))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
Reference in New Issue
Block a user