-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
349 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
type BaseModel struct { | ||
ID uint `gorm:"primary_key" json:"id"` | ||
CreatedAt Datetime `json:"created_at"` | ||
UpdatedAt Datetime `json:"updated_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var DefaultTimeFormat = "2006-01-02 15:04:05" | ||
|
||
type Datetime sql.NullTime | ||
|
||
// Scan implements the Scanner interface. | ||
func (a *Datetime) Scan(value interface{}) error { | ||
return (*sql.NullTime)(a).Scan(value) | ||
} | ||
|
||
// Value implements the driver Valuer interface. | ||
func (a Datetime) Value() (driver.Value, error) { | ||
if !a.Valid { | ||
return nil, nil | ||
} | ||
|
||
return a.Time.Format(DefaultTimeFormat), nil | ||
} | ||
|
||
func (a Datetime) MarshalJSON() ([]byte, error) { | ||
if a.Valid { | ||
return []byte(fmt.Sprintf("\"%s\"", a.Time.Format(DefaultTimeFormat))), nil | ||
} | ||
|
||
return json.Marshal(nil) | ||
} | ||
|
||
func (a *Datetime) UnmarshalJSON(b []byte) error { | ||
s := strings.Trim(string(b), "\"") | ||
|
||
if s == "null" || s == "" { | ||
a.Valid = false | ||
a.Time = time.Time{} | ||
return nil | ||
} | ||
|
||
cst, err := time.LoadLocation("Asia/Shanghai") | ||
if err != nil { | ||
return fmt.Errorf("time.LoadLocation error: %s", err.Error()) | ||
} | ||
|
||
a.Time, err = time.ParseInLocation(DefaultTimeFormat, s, cst) | ||
if err != nil { | ||
// When time cannot be resolved using the default format, try RFC3339Nano | ||
if a.Time, err = time.ParseInLocation(time.RFC3339Nano, s, cst); err == nil { | ||
a.Time = a.Time.In(cst) | ||
} | ||
} | ||
|
||
a.Valid = true | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
package model | ||
|
||
import "gorm.io/gorm" | ||
|
||
type User struct { | ||
*gorm.Model | ||
BaseModel | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
Password string `json:"-"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.