Files
gochat/internal/service/service.go.bak
T
2026-06-04 15:44:48 +08:00

107 lines
3.1 KiB
Plaintext

package service
import (
"context"
"github.com/gochat/gochat/internal/app"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"gorm.io/gorm"
)
// AccountService provides business logic for account operations.
type AccountService struct {
app *app.App
}
func NewAccountService(application *app.App) *AccountService {
return &AccountService{app: application}
}
func (s *AccountService) GetByID(ctx context.Context, id uint) (*model.Account, error) {
var account model.Account
if err := s.app.DB().WithContext(ctx).First(&account, id).Error; err != nil {
return nil, err
}
return &account, nil
}
func (s *AccountService) Create(ctx context.Context, account *model.Account) error {
return s.app.DB().WithContext(ctx).Create(account).Error
}
func (s *AccountService) List(ctx context.Context, offset, limit int) ([]model.Account, int64, error) {
var accounts []model.Account
var count int64
db := s.app.DB().WithContext(ctx).Model(&model.Account{})
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Find(&accounts).Error; err != nil {
return nil, 0, err
}
return accounts, count, nil
}
// ConversationService provides business logic for conversation operations.
type ConversationService struct {
app *app.App
}
func NewConversationService(application *app.App) *ConversationService {
return &ConversationService{app: application}
}
func (s *ConversationService) GetByID(ctx context.Context, id uint) (*model.Conversation, error) {
var conv model.Conversation
if err := s.app.DB().WithContext(ctx).First(&conv, id).Error; err != nil {
return nil, err
}
return &conv, nil
}
func (s *ConversationService) Create(ctx context.Context, conv *model.Conversation) error {
return s.app.DB().WithContext(ctx).Create(conv).Error
}
func (s *ConversationService) ListByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.Conversation, int64, error) {
var convs []model.Conversation
var count int64
db := s.app.DB().WithContext(ctx).Model(&model.Conversation{}).Where("account_id = ?", accountID)
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Find(&convs).Error; err != nil {
return nil, 0, err
}
return convs, count, nil
}
// GenericRepository is a convenience wrapper around BaseRepository with typed models.
type GenericRepository[T any] struct {
db *gorm.DB
}
func NewGenericRepository[T any](db *gorm.DB) *GenericRepository[T] {
return &GenericRepository[T]{db: db}
}
func (r *GenericRepository[T]) Create(ctx context.Context, entity *T) error {
return r.db.WithContext(ctx).Create(entity).Error
}
func (r *GenericRepository[T]) GetByID(ctx context.Context, id uint) (*T, error) {
var entity T
if err := r.db.WithContext(ctx).First(&entity, id).Error; err != nil {
return nil, err
}
return &entity, nil
}
func (r *GenericRepository[T]) List(ctx context.Context, offset, limit int) ([]T, error) {
var entities []T
if err := r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&entities).Error; err != nil {
return nil, err
}
return entities, nil
}
func (r *GenericRepository[T]) DB() *gorm.DB {
return r.db
}