init
This commit is contained in:
33
backend/app/services/user.go
Normal file
33
backend/app/services/user.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type user struct{}
|
||||
|
||||
func (t *user) FindByUsername(ctx context.Context, username string) (*models.User, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
model, err := query.Where(tbl.Username.Eq(username)).First()
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FindByusername failed, %s", username)
|
||||
}
|
||||
return model, nil
|
||||
}
|
||||
|
||||
func (t *user) Create(ctx context.Context, user *models.User) (*models.User, error) {
|
||||
if err := user.EncryptPassword(ctx); err != nil {
|
||||
return nil, errors.Wrap(err, "encrypt user password failed")
|
||||
}
|
||||
|
||||
if err := user.Create(ctx); err != nil {
|
||||
return nil, errors.Wrapf(err, "Create user failed, %s", user.Username)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user