WIP: Full restruct, seprate service and api
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
76
data/user.go
76
data/user.go
@@ -33,10 +33,50 @@ type UserSearchDoc struct {
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
func (self *User) GetByEmail(ctx context.Context, email string) (*User, error) {
|
||||
func (self *User) SetEmail(s string) *User {
|
||||
self.Email = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetUsername(s string) *User {
|
||||
self.Username = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetNickname(s string) *User {
|
||||
self.Nickname = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetSubtitle(s string) *User {
|
||||
self.Subtitle = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetAvatar(s string) *User {
|
||||
self.Avatar = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetBio(s string) *User {
|
||||
self.Bio = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetPermissionLevel(s uint) *User {
|
||||
self.PermissionLevel = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) SetAllowPublic(s bool) *User {
|
||||
self.AllowPublic = s
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) GetByEmail(ctx *context.Context, email *string) (*User, error) {
|
||||
var user User
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
err := Database.WithContext(*ctx).
|
||||
Where("email = ?", email).
|
||||
First(&user).Error
|
||||
|
||||
@@ -50,10 +90,10 @@ func (self *User) GetByEmail(ctx context.Context, email string) (*User, error) {
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (self *User) GetByUserId(ctx context.Context, userId uuid.UUID) (*User, error) {
|
||||
func (self *User) GetByUserId(ctx *context.Context, userId *uuid.UUID) (*User, error) {
|
||||
var user User
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
err := Database.WithContext(*ctx).
|
||||
Where("user_id = ?", userId).
|
||||
First(&user).Error
|
||||
|
||||
@@ -67,12 +107,12 @@ func (self *User) GetByUserId(ctx context.Context, userId uuid.UUID) (*User, err
|
||||
return &user, err
|
||||
}
|
||||
|
||||
func (self *User) Create(ctx context.Context) error {
|
||||
func (self *User) Create(ctx *context.Context) error {
|
||||
self.UUID = uuid.New()
|
||||
self.UserId = uuid.New()
|
||||
|
||||
// DB transaction only
|
||||
if err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := Database.WithContext(*ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -90,8 +130,8 @@ func (self *User) Create(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *User) UpdateByUserID(ctx context.Context, userId uuid.UUID) error {
|
||||
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
func (self *User) UpdateByUserID(ctx *context.Context, userId *uuid.UUID) error {
|
||||
return Database.WithContext(*ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&User{}).Where("user_id = ?", userId).Updates(&self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -99,22 +139,22 @@ func (self *User) UpdateByUserID(ctx context.Context, userId uuid.UUID) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (self *User) GetFullTable(ctx context.Context) (*[]User, error) {
|
||||
func (self *User) GetFullTable(ctx *context.Context) (*[]User, error) {
|
||||
var users []User
|
||||
err := Database.WithContext(ctx).Find(&users).Error
|
||||
err := Database.WithContext(*ctx).Find(&users).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &users, nil
|
||||
}
|
||||
|
||||
func (self *User) FastListUsers(ctx context.Context, limit, offset int64) (*[]UserSearchDoc, error) {
|
||||
func (self *User) FastListUsers(ctx *context.Context, limit, offset *int64) (*[]UserSearchDoc, error) {
|
||||
index := MeiliSearch.Index("user")
|
||||
|
||||
// Fast read from MeiliSearch, no DB involved
|
||||
result, err := index.SearchWithContext(ctx, "", &meilisearch.SearchRequest{
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
result, err := index.SearchWithContext(*ctx, "", &meilisearch.SearchRequest{
|
||||
Limit: *limit,
|
||||
Offset: *offset,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -128,7 +168,7 @@ func (self *User) FastListUsers(ctx context.Context, limit, offset int64) (*[]Us
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
func (self *User) UpdateSearchIndex(ctx context.Context) error {
|
||||
func (self *User) UpdateSearchIndex(ctx *context.Context) error {
|
||||
doc := UserSearchDoc{
|
||||
UserId: self.UserId.String(),
|
||||
Email: self.Email,
|
||||
@@ -145,7 +185,7 @@ func (self *User) UpdateSearchIndex(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if _, err := index.UpdateDocumentsWithContext(
|
||||
ctx,
|
||||
*ctx,
|
||||
[]UserSearchDoc{doc},
|
||||
opts,
|
||||
); err != nil {
|
||||
@@ -155,8 +195,8 @@ func (self *User) UpdateSearchIndex(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *User) DeleteSearchIndex(ctx context.Context) error {
|
||||
func (self *User) DeleteSearchIndex(ctx *context.Context) error {
|
||||
index := MeiliSearch.Index("user")
|
||||
_, err := index.DeleteDocumentWithContext(ctx, self.UserId.String(), nil)
|
||||
_, err := index.DeleteDocumentWithContext(*ctx, self.UserId.String(), nil)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user