recommender/internal/service/application/applications.go
2024-11-07 18:09:13 +08:00

46 lines
1.2 KiB
Go

package application
import (
"context"
"leafdev.top/Ecosystem/recommender/internal/entity"
"leafdev.top/Ecosystem/recommender/internal/schema"
)
func (s *Service) CreateApplication(ctx context.Context, name string, userId schema.UserId) (*entity.Application, error) {
application := &entity.Application{
Name: name,
UserId: userId,
}
err := s.dao.Application.WithContext(ctx).Create(application)
if err != nil {
return nil, err
}
return application, nil
}
func (s *Service) GetApplicationById(ctx context.Context, id schema.EntityId) (*entity.Application, error) {
return s.dao.Application.WithContext(ctx).Where(s.dao.Application.Id.Eq(id.Uint())).First()
}
// ListApplications 列出所有应用
func (s *Service) ListApplications(ctx context.Context, userId *schema.UserId) ([]*entity.Application, error) {
var q = s.dao.WithContext(ctx).Application
if userId != nil {
return q.Where(s.dao.Application.UserId.Eq(userId.String())).Find()
}
return q.Find()
}
// DeleteApplication 删除应用
func (s *Service) DeleteApplication(ctx context.Context, id schema.EntityId) error {
// TODO: 删除之前,需要删除关联的部分
_, err := s.dao.WithContext(ctx).Application.Where(s.dao.Application.Id.Eq(id.Uint())).Delete()
return err
}