Add user db search

- Search user by email

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-19 18:10:04 +08:00
parent dc128c0392
commit 0fb5c8b758
5 changed files with 65 additions and 0 deletions

39
data/data.go Normal file
View 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
View 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
View File

@@ -17,6 +17,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/goccy/go-json v0.10.5 // 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/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect

2
go.sum
View File

@@ -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/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
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/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=

View File

@@ -2,6 +2,7 @@ package main
import (
"nixcn-cms/config"
"nixcn-cms/data"
"nixcn-cms/logger"
"nixcn-cms/server"
)
@@ -9,5 +10,6 @@ import (
func main() {
config.Init()
logger.Init()
data.Init()
server.Start()
}