2024-11-09 16:11:00 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"leafdev.top/Ecosystem/recommender/internal/entity"
|
|
|
|
)
|
|
|
|
|
2024-11-09 19:49:53 +00:00
|
|
|
func (s *Service) GetExternalUser(c context.Context, externalUserId string, applicationEntity *entity.Application) (*entity.ExternalUser, error) {
|
|
|
|
iu, err := s.dao.WithContext(c).ExternalUser.
|
|
|
|
Where(s.dao.ExternalUser.ApplicationId.Eq(applicationEntity.Id.Uint())).
|
|
|
|
Where(s.dao.ExternalUser.ExternalId.Eq(externalUserId)).First()
|
|
|
|
return iu, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) UserExists(c context.Context, externalUserId string, applicationEntity *entity.Application) (bool, error) {
|
|
|
|
iu, err := s.dao.WithContext(c).ExternalUser.
|
|
|
|
Where(s.dao.ExternalUser.ApplicationId.Eq(applicationEntity.Id.Uint())).
|
|
|
|
Where(s.dao.ExternalUser.ExternalId.Eq(externalUserId)).Count()
|
|
|
|
return iu > 0, err
|
|
|
|
}
|
|
|
|
|
2024-11-09 16:11:00 +00:00
|
|
|
func (s *Service) GetOrCreateExternalUser(c context.Context, externalUserId string, applicationEntity *entity.Application) (*entity.ExternalUser, error) {
|
|
|
|
//Where(s.dao.UserTagScore.ExternalUserId.Eq(externalUserEntity.Id.Uint())).
|
|
|
|
count, err := s.dao.WithContext(c).ExternalUser.
|
|
|
|
Where(s.dao.ExternalUser.ApplicationId.Eq(applicationEntity.Id.Uint())).
|
|
|
|
Where(s.dao.ExternalUser.ExternalId.Eq(externalUserId)).Count()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
eu, err := s.dao.WithContext(c).ExternalUser.
|
|
|
|
Where(s.dao.ExternalUser.ApplicationId.Eq(applicationEntity.Id.Uint())).
|
|
|
|
Where(s.dao.ExternalUser.ExternalId.Eq(externalUserId)).First()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return eu, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
eu := &entity.ExternalUser{
|
|
|
|
ApplicationId: applicationEntity.Id,
|
|
|
|
ExternalId: externalUserId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.dao.WithContext(c).ExternalUser.Create(eu)
|
|
|
|
|
|
|
|
return eu, err
|
|
|
|
}
|