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.ServersItemPatch).Methods("PATCH")
 | 
				
			||||||
	r.HandleFunc("/api/servers/{id}", routers.ServersItemDelete).Methods("DELETE")
 | 
						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/params/model", routers.ParamsModelsGet).Methods("GET")
 | 
				
			||||||
	r.HandleFunc("/api/account", routers.AccountGet).Methods("GET")
 | 
						r.HandleFunc("/api/account", routers.AccountGet).Methods("GET")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,23 +2,24 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Image struct {
 | 
					type Image struct {
 | 
				
			||||||
	ID                int     `json:"id" gorm:"primary_key"`
 | 
						ID                int       `json:"id" gorm:"primary_key"`
 | 
				
			||||||
	Name              string  `json:"name"`
 | 
						Name              string    `json:"name"`
 | 
				
			||||||
	Width             int     `json:"width"`
 | 
						Width             int       `json:"width"`
 | 
				
			||||||
	Height            int     `json:"height"`
 | 
						Height            int       `json:"height"`
 | 
				
			||||||
	Prompt            string  `json:"prompt"`
 | 
						Prompt            string    `json:"prompt"`
 | 
				
			||||||
	NegativePrompt    string  `json:"negative_prompt"`
 | 
						NegativePrompt    string    `json:"negative_prompt"`
 | 
				
			||||||
	NumInferenceSteps int     `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500)
 | 
						NumInferenceSteps int       `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500)
 | 
				
			||||||
	GuidanceScale     float32 `json:"guidance_scale"`      // Scale for classifier-free guidance (minimum: 1; maximum: 20)
 | 
						GuidanceScale     float32   `json:"guidance_scale"`      // Scale for classifier-free guidance (minimum: 1; maximum: 20)
 | 
				
			||||||
	Scheduler         string  `json:"scheduler"`           // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS)
 | 
						Scheduler         string    `json:"scheduler"`           // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS)
 | 
				
			||||||
	Seed              int     `json:"seed"`                // Random seed (minimum: 0; maximum: 2147483647)
 | 
						Seed              int       `json:"seed"`                // Random seed (minimum: 0; maximum: 2147483647)
 | 
				
			||||||
	FromImage         string  `json:"from_image"`          // Image to start from
 | 
						FromImage         string    `json:"from_image"`          // Image to start from
 | 
				
			||||||
	CreatedAt         string  `json:"created_at"`
 | 
						UserID            int       `json:"user_id"`
 | 
				
			||||||
	UpdatedAt         string  `json:"updated_at"`
 | 
						CreatedAt         time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UserID            int     `json:"user_id"`
 | 
						UpdatedAt         time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,22 +3,23 @@ package models
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Model struct {
 | 
					type Model 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"`                     // (lora|ckp|hyper|ti)
 | 
						Type         string    `json:"type"`                     // (lora|ckp|hyper|ti)
 | 
				
			||||||
	TriggerWords string `json:"trigger_words"`            // 觸發詞
 | 
						TriggerWords string    `json:"trigger_words"`            // 觸發詞
 | 
				
			||||||
	BaseModel    string `json:"base_model"`               // (SD1.5|SD2)
 | 
						BaseModel    string    `json:"base_model"`               // (SD1.5|SD2)
 | 
				
			||||||
	ModelPath    string `json:"model_path"`               // 模型路徑
 | 
						ModelPath    string    `json:"model_path"`               // 模型路徑
 | 
				
			||||||
	Status       string `json:"status" default:"initial"` // (initial|ready|waiting|running|success|error)
 | 
						Status       string    `json:"status" default:"initial"` // (initial|ready|waiting|running|success|error)
 | 
				
			||||||
	Progress     int    `json:"progress"`                 // (0-100)
 | 
						Progress     int       `json:"progress"`                 // (0-100)
 | 
				
			||||||
	Image        string `json:"image"`                    // 封面圖片實際地址
 | 
						Image        string    `json:"image"`                    // 封面圖片實際地址
 | 
				
			||||||
	Tags         string `json:"tags"`
 | 
						Tags         string    `json:"tags"`
 | 
				
			||||||
	CreatedAt    string `json:"created_at"`
 | 
						UserID       int       `json:"user_id"`
 | 
				
			||||||
	UpdatedAt    string `json:"updated_at"`
 | 
						CreatedAt    time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UserID       int    `json:"user_id"`
 | 
						UpdatedAt    time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,18 +2,19 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Server struct {
 | 
					type Server 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"` // (訓練|推理)
 | 
						Type      string    `json:"type"` // (訓練|推理)
 | 
				
			||||||
	IP        string `json:"ip"`
 | 
						IP        string    `json:"ip"`
 | 
				
			||||||
	Port      int    `json:"port"`
 | 
						Port      int       `json:"port"`
 | 
				
			||||||
	Username  string `json:"username"`
 | 
						Username  string    `json:"username"`
 | 
				
			||||||
	Password  string `json:"password"`
 | 
						Password  string    `json:"password"`
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,13 +2,14 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Tag struct {
 | 
					type Tag struct {
 | 
				
			||||||
	ID        int    `json:"id" gorm:"primary_key"`
 | 
						ID        int       `json:"id" gorm:"primary_key"`
 | 
				
			||||||
	Name      string `json:"name"`
 | 
						Name      string    `json:"name"`
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,16 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Task struct {
 | 
					type Task struct {
 | 
				
			||||||
	ID        int    `json:"id"`
 | 
						ID        int       `json:"id" gorm:"primary_key"`
 | 
				
			||||||
	Name      string `json:"name"`
 | 
						Name      string    `json:"name"`
 | 
				
			||||||
	Type      string `json:"type"`     // 任務類型(訓練|推理)
 | 
						Type      string    `json:"type"`     // 任務類型(訓練|推理)
 | 
				
			||||||
	Status    string `json:"status"`   // (initial|ready|waiting|running|success|error)
 | 
						Status    string    `json:"status"`   // (initial|ready|waiting|running|success|error)
 | 
				
			||||||
	Progress  int    `json:"progress"` // (0-100)
 | 
						Progress  int       `json:"progress"` // (0-100)
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						UserID    int       `json:"user_id"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UserID    int    `json:"user_id"`
 | 
						UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//// 推理任務
 | 
					//// 推理任務
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,16 +4,17 @@ import (
 | 
				
			|||||||
	"crypto/md5"
 | 
						"crypto/md5"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type User struct {
 | 
					type User struct {
 | 
				
			||||||
	ID        int    `json:"id" gorm:"primary_key"`
 | 
						ID        int       `json:"id"`
 | 
				
			||||||
	Name      string `json:"name"`
 | 
						Name      string    `json:"name"`
 | 
				
			||||||
	Email     string `json:"email"`
 | 
						Email     string    `json:"email"`
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						Password  string    `json:"-"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						Slat      string    `json:"-"`
 | 
				
			||||||
	Password  string `json:"-"`
 | 
						CreatedAt time.Time `json:"created_at"`
 | 
				
			||||||
	Slat      string `json:"-"`
 | 
						UpdatedAt time.Time `json:"updated_at"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,15 +3,16 @@ package models
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Account struct {
 | 
					type Account struct {
 | 
				
			||||||
	ID        int    `json:"id"`
 | 
						ID        int       `json:"id"`
 | 
				
			||||||
	Name      string `json:"name"`
 | 
						Name      string    `json:"name"`
 | 
				
			||||||
	Email     string `json:"email"`
 | 
						Email     string    `json:"email"`
 | 
				
			||||||
	SessionID string `json:"session_id"`
 | 
						SessionID string    `json:"session_id"`
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						CreatedAt time.Time `json:"created_at"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						UpdatedAt time.Time `json:"updated_at"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) {
 | 
					func AccountRead(w http.ResponseWriter, r *http.Request, cb func(account *Account)) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,15 +2,16 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"main/configs"
 | 
						"main/configs"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Session struct {
 | 
					type Session struct {
 | 
				
			||||||
	ID        string `json:"id" gorm:"primary_key"`
 | 
						ID        string    `json:"id" gorm:"primary_key"`
 | 
				
			||||||
	IP        string `json:"ip"`
 | 
						IP        string    `json:"ip"`
 | 
				
			||||||
	UserID    int    `json:"user_id"`
 | 
						UserID    int       `json:"user_id"`
 | 
				
			||||||
	UserAgent string `json:"user_agent"`
 | 
						UserAgent string    `json:"user_agent"`
 | 
				
			||||||
	CreatedAt string `json:"created_at"`
 | 
						CreatedAt time.Time `json:"created_at" gorm:"default:CURRENT_TIMESTAMP;autoCreateTime"`
 | 
				
			||||||
	UpdatedAt string `json:"updated_at"`
 | 
						UpdatedAt time.Time `json:"updated_at" gorm:"default:CURRENT_TIMESTAMP;autoUpdateTime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,17 +6,18 @@ import (
 | 
				
			|||||||
	"main/models"
 | 
						"main/models"
 | 
				
			||||||
	"main/utils"
 | 
						"main/utils"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 獲取當前賬戶信息(重寫, 爲輸出增加sid字段)
 | 
					// 獲取當前賬戶信息(重寫, 爲輸出增加sid字段)
 | 
				
			||||||
func AccountGet(w http.ResponseWriter, r *http.Request) {
 | 
					func AccountGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	var account struct {
 | 
						var account struct {
 | 
				
			||||||
		ID        int    `json:"id"`
 | 
							ID        int       `json:"id"`
 | 
				
			||||||
		Name      string `json:"name"`
 | 
							Name      string    `json:"name"`
 | 
				
			||||||
		Email     string `json:"email"`
 | 
							Email     string    `json:"email"`
 | 
				
			||||||
		SessionID string `json:"session_id"`
 | 
							SessionID string    `json:"session_id"`
 | 
				
			||||||
		CreatedAt string `json:"created_at"`
 | 
							CreatedAt time.Time `json:"created_at"`
 | 
				
			||||||
		UpdatedAt string `json:"updated_at"`
 | 
							UpdatedAt time.Time `json:"updated_at"`
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 獲取Cookie
 | 
						// 獲取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/models"
 | 
				
			||||||
	"main/utils"
 | 
						"main/utils"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gorilla/mux"
 | 
						"github.com/gorilla/mux"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -74,7 +73,6 @@ func ImagesItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	image.ID = utils.ParamInt(mux.Vars(r)["id"], 0)
 | 
						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 {
 | 
						if err := configs.ORMDB().Model(&image).Updates(image).Error; err != nil {
 | 
				
			||||||
		log.Println(err)
 | 
							log.Println(err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,7 +10,6 @@ import (
 | 
				
			|||||||
	"main/utils"
 | 
						"main/utils"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gorilla/mux"
 | 
						"github.com/gorilla/mux"
 | 
				
			||||||
	"github.com/gorilla/websocket"
 | 
						"github.com/gorilla/websocket"
 | 
				
			||||||
@@ -144,9 +143,6 @@ func ModelItemPatch(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		model.Image = model_new.Image
 | 
							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 {
 | 
						if err := configs.ORMDB().Save(&model).Error; err != nil {
 | 
				
			||||||
		log.Println(err)
 | 
							log.Println(err)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,6 @@ import (
 | 
				
			|||||||
	"main/models"
 | 
						"main/models"
 | 
				
			||||||
	"main/utils"
 | 
						"main/utils"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
	"github.com/gorilla/mux"
 | 
						"github.com/gorilla/mux"
 | 
				
			||||||
@@ -57,13 +56,13 @@ func UsersPost(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	// 創建用戶
 | 
						// 創建用戶
 | 
				
			||||||
	var slat string = uuid.New().String()
 | 
						var slat string = uuid.New().String()
 | 
				
			||||||
	var user models.User = models.User{
 | 
						var user models.User = models.User{
 | 
				
			||||||
		Name:      form.Name,
 | 
							Name:     form.Name,
 | 
				
			||||||
		Email:     form.Email,
 | 
							Email:    form.Email,
 | 
				
			||||||
		Password:  fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))),
 | 
							Password: fmt.Sprintf("%x", md5.Sum([]byte(form.Password+slat))),
 | 
				
			||||||
		Slat:      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 {
 | 
						if err := configs.ORMDB().Create(&user).Error; err != nil {
 | 
				
			||||||
		fmt.Println(err)
 | 
							fmt.Println(err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
@@ -84,21 +83,34 @@ func UsersItemGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// 更新用戶
 | 
					// 更新用戶
 | 
				
			||||||
func UsersItemPatch(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)
 | 
						body, err := ioutil.ReadAll(r.Body)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		fmt.Println(err)
 | 
							fmt.Println(err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer r.Body.Close()
 | 
						defer r.Body.Close()
 | 
				
			||||||
	if err = json.Unmarshal(body, &user); err != nil {
 | 
						if err = json.Unmarshal(body, &form); err != nil {
 | 
				
			||||||
		fmt.Println(err)
 | 
							fmt.Println(err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := configs.ORMDB().Save(&user).Error; err != nil {
 | 
						user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
 | 
				
			||||||
		fmt.Println(err)
 | 
						configs.ORMDB().First(&user)
 | 
				
			||||||
		return
 | 
						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.Header().Set("Content-Type", "application/json; charset=utf-8")
 | 
				
			||||||
	w.Write(utils.ToJSON(user))
 | 
						w.Write(utils.ToJSON(user))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user