You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// InsertProfile 插入个人资料
|
|
func InsertProfile(ctx context.Context, db *gorm.DB, profile *Profile) (int64, error) {
|
|
result := db.WithContext(ctx).Create(profile)
|
|
if result.Error != nil {
|
|
return 0, result.Error
|
|
}
|
|
return profile.Id, nil
|
|
}
|
|
|
|
// FindOneProfile 根据ID查询个人资料
|
|
func FindOneProfile(ctx context.Context, db *gorm.DB, id int64) (*Profile, error) {
|
|
var profile Profile
|
|
result := db.WithContext(ctx).First(&profile, id)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
// FindProfileByUserId 根据用户ID查询个人资料
|
|
func FindProfileByUserId(ctx context.Context, db *gorm.DB, userId int64) (*Profile, error) {
|
|
var profile Profile
|
|
result := db.WithContext(ctx).Where("user_id = ?", userId).First(&profile)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
return &profile, nil
|
|
}
|
|
|
|
// UpdateProfile 更新个人资料
|
|
func UpdateProfile(ctx context.Context, db *gorm.DB, profile *Profile) error {
|
|
return db.WithContext(ctx).Save(profile).Error
|
|
}
|
|
|
|
// DeleteProfile 删除个人资料
|
|
func DeleteProfile(ctx context.Context, db *gorm.DB, id int64) error {
|
|
return db.WithContext(ctx).Delete(&Profile{}, id).Error
|
|
}
|
|
|