forked from nixcn/nixcn-cms
Add user db search
- Search user by email Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
39
data/data.go
Normal file
39
data/data.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"nixcn-cms/config"
|
||||||
|
"nixcn-cms/data/drivers"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Database *drivers.DBClient
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
// Init database
|
||||||
|
dbType := config.Get("database.type").(string)
|
||||||
|
exDSN := drivers.ExternalDSN{
|
||||||
|
Host: config.Get("database.host").(string),
|
||||||
|
Name: config.Get("database.name").(string),
|
||||||
|
Username: config.Get("database.username").(string),
|
||||||
|
Password: config.Get("database.password").(string),
|
||||||
|
}
|
||||||
|
|
||||||
|
if dbType != "postgres" {
|
||||||
|
log.Fatal("[Database] Only support postgras db!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conect to db
|
||||||
|
db, err := drivers.Postgres(exDSN)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("[Database] Error connecting to db!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto migrate
|
||||||
|
err = db.DB.AutoMigrate(&User{})
|
||||||
|
if err != nil {
|
||||||
|
log.Error("[Database] Error migrating database: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Database = db
|
||||||
|
}
|
||||||
21
data/user.go
Normal file
21
data/user.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package data
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Id uint `json:"id" gorm:"primarykey;autoincrement"`
|
||||||
|
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueindex;not null"`
|
||||||
|
UserId string `json:"user_id" gorm:"size:8;uniqueindex;not null"`
|
||||||
|
Email string `json:"email" gorm:"uniqueindex;not null"`
|
||||||
|
Nickname string `json:"nickname" gorm:"not null"`
|
||||||
|
Type string `json:"type" gorm:"not null"`
|
||||||
|
Subtitle string `json:"subtitle" gorm:"not null"`
|
||||||
|
Avatar string `json:"avatar" gorm:"not null"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *User) GetByEmail(email string) error {
|
||||||
|
if err := Database.Where("email = ?", email).First(&self).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@@ -17,6 +17,7 @@ require (
|
|||||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.5 // indirect
|
github.com/goccy/go-json v0.10.5 // indirect
|
||||||
github.com/goccy/go-yaml v1.19.1 // indirect
|
github.com/goccy/go-yaml v1.19.1 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
github.com/jackc/pgx/v5 v5.6.0 // indirect
|
github.com/jackc/pgx/v5 v5.6.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -29,6 +29,8 @@ github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU
|
|||||||
github.com/goccy/go-yaml v1.19.1 h1:3rG3+v8pkhRqoQ/88NYNMHYVGYztCOCIZ7UQhu7H+NE=
|
github.com/goccy/go-yaml v1.19.1 h1:3rG3+v8pkhRqoQ/88NYNMHYVGYztCOCIZ7UQhu7H+NE=
|
||||||
github.com/goccy/go-yaml v1.19.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
github.com/goccy/go-yaml v1.19.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/config"
|
"nixcn-cms/config"
|
||||||
|
"nixcn-cms/data"
|
||||||
"nixcn-cms/logger"
|
"nixcn-cms/logger"
|
||||||
"nixcn-cms/server"
|
"nixcn-cms/server"
|
||||||
)
|
)
|
||||||
@@ -9,5 +10,6 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
config.Init()
|
config.Init()
|
||||||
logger.Init()
|
logger.Init()
|
||||||
|
data.Init()
|
||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user