user UpdatedAt
This commit is contained in:
		
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@@ -97,6 +97,12 @@ func main() {
 | 
			
		||||
	r.HandleFunc("/api/servers/{id}", routers.ServersItemPatch).Methods("PATCH")
 | 
			
		||||
	r.HandleFunc("/api/servers/{id}", routers.ServersItemDelete).Methods("DELETE")
 | 
			
		||||
 | 
			
		||||
	r.HandleFunc("/api/datasets", routers.DatasetsGet).Methods("GET")
 | 
			
		||||
	r.HandleFunc("/api/datasets", routers.DatasetsPost).Methods("POST")
 | 
			
		||||
	r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemGet).Methods("GET")
 | 
			
		||||
	r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemPatch).Methods("PATCH")
 | 
			
		||||
	r.HandleFunc("/api/datasets/{id}", routers.DatasetsItemDelete).Methods("DELETE")
 | 
			
		||||
 | 
			
		||||
	r.HandleFunc("/api/params/model", routers.ParamsModelsGet).Methods("GET")
 | 
			
		||||
	r.HandleFunc("/api/account", routers.AccountGet).Methods("GET")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Image struct {
 | 
			
		||||
@@ -16,9 +17,9 @@ type Image struct {
 | 
			
		||||
	Scheduler         string    `json:"scheduler"`           // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS)
 | 
			
		||||
	Seed              int       `json:"seed"`                // Random seed (minimum: 0; maximum: 2147483647)
 | 
			
		||||
	FromImage         string    `json:"from_image"`          // Image to start from
 | 
			
		||||
	CreatedAt         string  `json:"created_at"`
 | 
			
		||||
	UpdatedAt         string  `json:"updated_at"`
 | 
			
		||||
	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"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Model struct {
 | 
			
		||||
@@ -16,9 +17,9 @@ type Model struct {
 | 
			
		||||
	Progress     int       `json:"progress"`                 // (0-100)
 | 
			
		||||
	Image        string    `json:"image"`                    // 封面圖片實際地址
 | 
			
		||||
	Tags         string    `json:"tags"`
 | 
			
		||||
	CreatedAt    string `json:"created_at"`
 | 
			
		||||
	UpdatedAt    string `json:"updated_at"`
 | 
			
		||||
	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"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Server struct {
 | 
			
		||||
@@ -12,8 +13,8 @@ type Server struct {
 | 
			
		||||
	Port      int       `json:"port"`
 | 
			
		||||
	Username  string    `json:"username"`
 | 
			
		||||
	Password  string    `json:"password"`
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
			
		||||
	UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,13 +2,14 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Tag struct {
 | 
			
		||||
	ID        int       `json:"id" gorm:"primary_key"`
 | 
			
		||||
	Name      string    `json:"name"`
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
			
		||||
	UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,16 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import "time"
 | 
			
		||||
 | 
			
		||||
type Task struct {
 | 
			
		||||
	ID        int    `json:"id"`
 | 
			
		||||
	ID        int       `json:"id" gorm:"primary_key"`
 | 
			
		||||
	Name      string    `json:"name"`
 | 
			
		||||
	Type      string    `json:"type"`     // 任務類型(訓練|推理)
 | 
			
		||||
	Status    string    `json:"status"`   // (initial|ready|waiting|running|success|error)
 | 
			
		||||
	Progress  int       `json:"progress"` // (0-100)
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	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"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//// 推理任務
 | 
			
		||||
 
 | 
			
		||||
@@ -4,16 +4,17 @@ import (
 | 
			
		||||
	"crypto/md5"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type User struct {
 | 
			
		||||
	ID        int    `json:"id" gorm:"primary_key"`
 | 
			
		||||
	ID        int       `json:"id"`
 | 
			
		||||
	Name      string    `json:"name"`
 | 
			
		||||
	Email     string    `json:"email"`
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	Password  string    `json:"-"`
 | 
			
		||||
	Slat      string    `json:"-"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at"`
 | 
			
		||||
	UpdatedAt time.Time `json:"updated_at"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Account struct {
 | 
			
		||||
@@ -10,8 +11,8 @@ type Account struct {
 | 
			
		||||
	Name      string    `json:"name"`
 | 
			
		||||
	Email     string    `json:"email"`
 | 
			
		||||
	SessionID string    `json:"session_id"`
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at"`
 | 
			
		||||
	UpdatedAt time.Time `json:"updated_at"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Session struct {
 | 
			
		||||
@@ -9,8 +10,8 @@ type Session struct {
 | 
			
		||||
	IP        string    `json:"ip"`
 | 
			
		||||
	UserID    int       `json:"user_id"`
 | 
			
		||||
	UserAgent string    `json:"user_agent"`
 | 
			
		||||
	CreatedAt string `json:"created_at"`
 | 
			
		||||
	UpdatedAt string `json:"updated_at"`
 | 
			
		||||
	CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
			
		||||
	UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"main/models"
 | 
			
		||||
	"main/utils"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 獲取當前賬戶信息(重寫, 爲輸出增加sid字段)
 | 
			
		||||
@@ -15,8 +16,8 @@ func AccountGet(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		Name      string    `json:"name"`
 | 
			
		||||
		Email     string    `json:"email"`
 | 
			
		||||
		SessionID string    `json:"session_id"`
 | 
			
		||||
		CreatedAt string `json:"created_at"`
 | 
			
		||||
		UpdatedAt string `json:"updated_at"`
 | 
			
		||||
		CreatedAt time.Time `json:"created_at"`
 | 
			
		||||
		UpdatedAt time.Time `json:"updated_at"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 獲取Cookie
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										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)
 | 
			
		||||
}
 | 
			
		||||
@@ -8,7 +8,6 @@ import (
 | 
			
		||||
	"main/models"
 | 
			
		||||
	"main/utils"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/gorilla/mux"
 | 
			
		||||
)
 | 
			
		||||
@@ -74,7 +73,6 @@ func ImagesItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	image.ID = utils.ParamInt(mux.Vars(r)["id"], 0)
 | 
			
		||||
	image.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
 | 
			
		||||
	if err := configs.ORMDB().Model(&image).Updates(image).Error; err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,6 @@ import (
 | 
			
		||||
	"main/utils"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/gorilla/mux"
 | 
			
		||||
	"github.com/gorilla/websocket"
 | 
			
		||||
@@ -144,9 +143,6 @@ func ModelItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		model.Image = model_new.Image
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 更新時間
 | 
			
		||||
	model.UpdatedAt = time.Now().Format("2006-01-02 15:04:05")
 | 
			
		||||
 | 
			
		||||
	// 執行更新
 | 
			
		||||
	if err := configs.ORMDB().Save(&model).Error; err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,6 @@ import (
 | 
			
		||||
	"main/models"
 | 
			
		||||
	"main/utils"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"github.com/gorilla/mux"
 | 
			
		||||
@@ -61,9 +60,9 @@ func UsersPost(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		Email:    form.Email,
 | 
			
		||||
		Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))),
 | 
			
		||||
		Slat:     slat,
 | 
			
		||||
		CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
 | 
			
		||||
		UpdatedAt: time.Now().Format("2006-01-02 15:04:05"),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 寫入數據庫
 | 
			
		||||
	if err := configs.ORMDB().Create(&user).Error; err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
@@ -84,21 +83,34 @@ func UsersItemGet(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
// 更新用戶
 | 
			
		||||
func UsersItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
			
		||||
	var form struct {
 | 
			
		||||
		Name     string `json:"name"`
 | 
			
		||||
		Email    string `json:"email"`
 | 
			
		||||
		Password string `json:"password"`
 | 
			
		||||
	}
 | 
			
		||||
	body, err := ioutil.ReadAll(r.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	defer r.Body.Close()
 | 
			
		||||
	if err = json.Unmarshal(body, &user); err != nil {
 | 
			
		||||
	if err = json.Unmarshal(body, &form); err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if err := configs.ORMDB().Save(&user).Error; err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
			
		||||
	configs.ORMDB().First(&user)
 | 
			
		||||
	if form.Name != "" {
 | 
			
		||||
		user.Name = form.Name
 | 
			
		||||
	}
 | 
			
		||||
	if form.Email != "" {
 | 
			
		||||
		user.Email = form.Email
 | 
			
		||||
	}
 | 
			
		||||
	if form.Password != "" {
 | 
			
		||||
		user.Slat = uuid.New().String()
 | 
			
		||||
		user.Password = fmt.Sprintf("%x", md5.Sum([]byte(form.Password+user.Slat)))
 | 
			
		||||
	}
 | 
			
		||||
	configs.ORMDB().Save(&user)
 | 
			
		||||
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
			
		||||
	w.Write(utils.ToJSON(user))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user