refactor: Update service and repository methods to use context
- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles. - Updated handlers to utilize context in method calls, improving error handling and performance. - Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"carrotskin/internal/model"
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -16,48 +17,48 @@ func NewClientRepository(db *gorm.DB) ClientRepository {
|
||||
return &clientRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *clientRepository) Create(client *model.Client) error {
|
||||
return r.db.Create(client).Error
|
||||
func (r *clientRepository) Create(ctx context.Context, client *model.Client) error {
|
||||
return r.db.WithContext(ctx).Create(client).Error
|
||||
}
|
||||
|
||||
func (r *clientRepository) FindByClientToken(clientToken string) (*model.Client, error) {
|
||||
func (r *clientRepository) FindByClientToken(ctx context.Context, clientToken string) (*model.Client, error) {
|
||||
var client model.Client
|
||||
err := r.db.Where("client_token = ?", clientToken).First(&client).Error
|
||||
err := r.db.WithContext(ctx).Where("client_token = ?", clientToken).First(&client).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *clientRepository) FindByUUID(uuid string) (*model.Client, error) {
|
||||
func (r *clientRepository) FindByUUID(ctx context.Context, uuid string) (*model.Client, error) {
|
||||
var client model.Client
|
||||
err := r.db.Where("uuid = ?", uuid).First(&client).Error
|
||||
err := r.db.WithContext(ctx).Where("uuid = ?", uuid).First(&client).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
func (r *clientRepository) FindByUserID(userID int64) ([]*model.Client, error) {
|
||||
func (r *clientRepository) FindByUserID(ctx context.Context, userID int64) ([]*model.Client, error) {
|
||||
var clients []*model.Client
|
||||
err := r.db.Where("user_id = ?", userID).Find(&clients).Error
|
||||
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&clients).Error
|
||||
return clients, err
|
||||
}
|
||||
|
||||
func (r *clientRepository) Update(client *model.Client) error {
|
||||
return r.db.Save(client).Error
|
||||
func (r *clientRepository) Update(ctx context.Context, client *model.Client) error {
|
||||
return r.db.WithContext(ctx).Save(client).Error
|
||||
}
|
||||
|
||||
func (r *clientRepository) IncrementVersion(clientUUID string) error {
|
||||
return r.db.Model(&model.Client{}).
|
||||
func (r *clientRepository) IncrementVersion(ctx context.Context, clientUUID string) error {
|
||||
return r.db.WithContext(ctx).Model(&model.Client{}).
|
||||
Where("uuid = ?", clientUUID).
|
||||
Update("version", gorm.Expr("version + 1")).Error
|
||||
}
|
||||
|
||||
func (r *clientRepository) DeleteByClientToken(clientToken string) error {
|
||||
return r.db.Where("client_token = ?", clientToken).Delete(&model.Client{}).Error
|
||||
func (r *clientRepository) DeleteByClientToken(ctx context.Context, clientToken string) error {
|
||||
return r.db.WithContext(ctx).Where("client_token = ?", clientToken).Delete(&model.Client{}).Error
|
||||
}
|
||||
|
||||
func (r *clientRepository) DeleteByUserID(userID int64) error {
|
||||
return r.db.Where("user_id = ?", userID).Delete(&model.Client{}).Error
|
||||
func (r *clientRepository) DeleteByUserID(ctx context.Context, userID int64) error {
|
||||
return r.db.WithContext(ctx).Where("user_id = ?", userID).Delete(&model.Client{}).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user