gin funn and testing
This commit is contained in:
25
models/auth.go
Normal file
25
models/auth.go
Normal file
@ -0,0 +1,25 @@
|
||||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
|
||||
type Auth struct {
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// CheckAuth checks if authentication information exists
|
||||
func CheckAuth(username, password string) (bool, error) {
|
||||
var auth Auth
|
||||
err := db.Select("id").Where(Auth{Username: username, Password: password}).First(&auth).Error
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if auth.ID > 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
115
models/models.go
Normal file
115
models/models.go
Normal file
@ -0,0 +1,115 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
|
||||
"gin-server/lib/setting"
|
||||
"time"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
type Model struct {
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
CreatedOn int `json:"created_on"`
|
||||
ModifiedOn int `json:"modified_on"`
|
||||
DeletedOn int `json:"deleted_on"`
|
||||
}
|
||||
|
||||
// Setup initializes the database instance
|
||||
func Setup() {
|
||||
var err error
|
||||
db, err = gorm.Open(setting.DatabaseSetting.Type, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
||||
setting.DatabaseSetting.User,
|
||||
setting.DatabaseSetting.Password,
|
||||
setting.DatabaseSetting.Host,
|
||||
setting.DatabaseSetting.Name))
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("models.Setup err: %v", err)
|
||||
}
|
||||
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return setting.DatabaseSetting.TablePrefix + defaultTableName
|
||||
}
|
||||
|
||||
db.SingularTable(true)
|
||||
db.Callback().Create().Replace("gorm:update_time_stamp", updateTimeStampForCreateCallback)
|
||||
db.Callback().Update().Replace("gorm:update_time_stamp", updateTimeStampForUpdateCallback)
|
||||
db.Callback().Delete().Replace("gorm:delete", deleteCallback)
|
||||
db.DB().SetMaxIdleConns(10)
|
||||
db.DB().SetMaxOpenConns(100)
|
||||
db.AutoMigrate(&Auth{})
|
||||
}
|
||||
|
||||
// CloseDB closes database connection (unnecessary)
|
||||
func CloseDB() {
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
// updateTimeStampForCreateCallback will set `CreatedOn`, `ModifiedOn` when creating
|
||||
func updateTimeStampForCreateCallback(scope *gorm.Scope) {
|
||||
if !scope.HasError() {
|
||||
nowTime := time.Now().Unix()
|
||||
if createTimeField, ok := scope.FieldByName("CreatedOn"); ok {
|
||||
if createTimeField.IsBlank {
|
||||
createTimeField.Set(nowTime)
|
||||
}
|
||||
}
|
||||
|
||||
if modifyTimeField, ok := scope.FieldByName("ModifiedOn"); ok {
|
||||
if modifyTimeField.IsBlank {
|
||||
modifyTimeField.Set(nowTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updateTimeStampForUpdateCallback will set `ModifiedOn` when updating
|
||||
func updateTimeStampForUpdateCallback(scope *gorm.Scope) {
|
||||
if _, ok := scope.Get("gorm:update_column"); !ok {
|
||||
scope.SetColumn("ModifiedOn", time.Now().Unix())
|
||||
}
|
||||
}
|
||||
|
||||
// deleteCallback will set `DeletedOn` where deleting
|
||||
func deleteCallback(scope *gorm.Scope) {
|
||||
if !scope.HasError() {
|
||||
var extraOption string
|
||||
if str, ok := scope.Get("gorm:delete_option"); ok {
|
||||
extraOption = fmt.Sprint(str)
|
||||
}
|
||||
|
||||
deletedOnField, hasDeletedOnField := scope.FieldByName("DeletedOn")
|
||||
|
||||
if !scope.Search.Unscoped && hasDeletedOnField {
|
||||
scope.Raw(fmt.Sprintf(
|
||||
"UPDATE %v SET %v=%v%v%v",
|
||||
scope.QuotedTableName(),
|
||||
scope.Quote(deletedOnField.DBName),
|
||||
scope.AddToVars(time.Now().Unix()),
|
||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||
addExtraSpaceIfExist(extraOption),
|
||||
)).Exec()
|
||||
} else {
|
||||
scope.Raw(fmt.Sprintf(
|
||||
"DELETE FROM %v%v%v",
|
||||
scope.QuotedTableName(),
|
||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||
addExtraSpaceIfExist(extraOption),
|
||||
)).Exec()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// addExtraSpaceIfExist adds a separator
|
||||
func addExtraSpaceIfExist(str string) string {
|
||||
if str != "" {
|
||||
return " " + str
|
||||
}
|
||||
return ""
|
||||
}
|
Reference in New Issue
Block a user