draft grpc
This commit is contained in:
91
internal/api/preferences/service.go
Normal file
91
internal/api/preferences/service.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package preferences
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"stream.api/internal/database/model"
|
||||
"stream.api/pkg/logger"
|
||||
)
|
||||
|
||||
type UpdateInput struct {
|
||||
EmailNotifications *bool
|
||||
PushNotifications *bool
|
||||
MarketingNotifications *bool
|
||||
TelegramNotifications *bool
|
||||
Autoplay *bool
|
||||
Loop *bool
|
||||
Muted *bool
|
||||
ShowControls *bool
|
||||
Pip *bool
|
||||
Airplay *bool
|
||||
Chromecast *bool
|
||||
Language *string
|
||||
Locale *string
|
||||
}
|
||||
|
||||
func LoadUserPreferences(ctx context.Context, db *gorm.DB, userID string) (*model.UserPreference, error) {
|
||||
return model.FindOrCreateUserPreference(ctx, db, userID)
|
||||
}
|
||||
|
||||
func UpdateUserPreferences(ctx context.Context, db *gorm.DB, l logger.Logger, userID string, req UpdateInput) (*model.UserPreference, error) {
|
||||
pref, err := model.FindOrCreateUserPreference(ctx, db, userID)
|
||||
if err != nil {
|
||||
l.Error("Failed to load preferences", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.EmailNotifications != nil {
|
||||
pref.EmailNotifications = model.BoolPtr(*req.EmailNotifications)
|
||||
}
|
||||
if req.PushNotifications != nil {
|
||||
pref.PushNotifications = model.BoolPtr(*req.PushNotifications)
|
||||
}
|
||||
if req.MarketingNotifications != nil {
|
||||
pref.MarketingNotifications = *req.MarketingNotifications
|
||||
}
|
||||
if req.TelegramNotifications != nil {
|
||||
pref.TelegramNotifications = *req.TelegramNotifications
|
||||
}
|
||||
if req.Autoplay != nil {
|
||||
pref.Autoplay = *req.Autoplay
|
||||
}
|
||||
if req.Loop != nil {
|
||||
pref.Loop = *req.Loop
|
||||
}
|
||||
if req.Muted != nil {
|
||||
pref.Muted = *req.Muted
|
||||
}
|
||||
if req.ShowControls != nil {
|
||||
pref.ShowControls = model.BoolPtr(*req.ShowControls)
|
||||
}
|
||||
if req.Pip != nil {
|
||||
pref.Pip = model.BoolPtr(*req.Pip)
|
||||
}
|
||||
if req.Airplay != nil {
|
||||
pref.Airplay = model.BoolPtr(*req.Airplay)
|
||||
}
|
||||
if req.Chromecast != nil {
|
||||
pref.Chromecast = model.BoolPtr(*req.Chromecast)
|
||||
}
|
||||
if req.Language != nil {
|
||||
pref.Language = model.StringPtr(strings.TrimSpace(*req.Language))
|
||||
}
|
||||
if req.Locale != nil {
|
||||
pref.Locale = model.StringPtr(strings.TrimSpace(*req.Locale))
|
||||
}
|
||||
if strings.TrimSpace(model.StringValue(pref.Language)) == "" {
|
||||
pref.Language = model.StringPtr("en")
|
||||
}
|
||||
if strings.TrimSpace(model.StringValue(pref.Locale)) == "" {
|
||||
pref.Locale = model.StringPtr(model.StringValue(pref.Language))
|
||||
}
|
||||
|
||||
if err := db.WithContext(ctx).Save(pref).Error; err != nil {
|
||||
l.Error("Failed to save preferences", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pref, nil
|
||||
}
|
||||
Reference in New Issue
Block a user