initial commit 2
This commit is contained in:
149
application/applicationservice/user.go
Normal file
149
application/applicationservice/user.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package applicationservice
|
||||
|
||||
import (
|
||||
"bitbucket.org/nemt/nemt-portal-api/application/entitymapping"
|
||||
"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
|
||||
"bitbucket.org/nemt/nemt-portal-api/domain/entity"
|
||||
"bitbucket.org/nemt/nemt-portal-api/domain/service"
|
||||
"bitbucket.org/nemt/nemt-portal-api/infra/errors"
|
||||
)
|
||||
|
||||
// userService holds methods to user application service
|
||||
type userService struct {
|
||||
svc *service.Service
|
||||
mapEntity *entitymapping.Mapper
|
||||
}
|
||||
|
||||
// newUserService returns a userService instance
|
||||
func newUserService(svc *service.Service, mapper *entitymapping.Mapper) *userService {
|
||||
return &userService{
|
||||
svc: svc,
|
||||
mapEntity: mapper,
|
||||
}
|
||||
}
|
||||
|
||||
// GetAll returns a list of users
|
||||
func (s *userService) GetAll(quantity int64, page int64) (retVal []viewmodel.User, err error) {
|
||||
users, err := s.svc.Users.GetAll()
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
return s.mapEntity.User.ToUserModelSlice(users), nil
|
||||
}
|
||||
|
||||
// GetByID returns a specific user by its ID
|
||||
func (s *userService) GetByID(id int64) (retVal viewmodel.User, err error) {
|
||||
user, err := s.svc.Users.GetByID(id)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModel(user), nil
|
||||
}
|
||||
|
||||
// GetByID returns a specific user by its ID
|
||||
func (s *userService) GetByUUID(uuid string, profile string) (retVal viewmodel.User, err error) {
|
||||
user, err := s.svc.Users.GetByUUID(uuid, profile)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModel(user), nil
|
||||
}
|
||||
|
||||
// Login returns a specific user by email and pass
|
||||
func (s *userService) FullLogin(loginType string, key string, pass string, profile string) (retVal viewmodel.User, err error) {
|
||||
user, err := s.svc.Users.FullLogin(loginType, key, pass, profile)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModel(user), nil
|
||||
}
|
||||
|
||||
// Login returns a specific user by email and pass
|
||||
func (s *userService) Login(email string, pass string) (retVal viewmodel.User, err error) {
|
||||
user, err := s.svc.Users.Login(email, pass)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModel(user), nil
|
||||
}
|
||||
|
||||
func (s *userService) Create(user viewmodel.User) (retVal viewmodel.User, err error) {
|
||||
entity := s.mapEntity.User.ToUserEntity(user)
|
||||
|
||||
for i, _ := range entity.Organizations {
|
||||
entity.Organizations[i], err = s.svc.Organization.GetByUUID(entity.Organizations[i].UUID)
|
||||
if err != nil {
|
||||
return retVal, err
|
||||
}
|
||||
}
|
||||
|
||||
entity, err = s.svc.Users.Create(entity)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModel(entity), nil
|
||||
}
|
||||
|
||||
func (s *userService) CreateBulk(users []viewmodel.User) (retVal []viewmodel.User, err error) {
|
||||
entities := s.mapEntity.User.ToUserEntitySlice(users)
|
||||
organizations := make([]entity.Organization, 0)
|
||||
for i, _ := range entities {
|
||||
if i == 0 {
|
||||
for o, _ := range entities[i].Organizations {
|
||||
org, err := s.svc.Organization.GetByUUID(entities[i].Organizations[o].UUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
organizations = append(organizations, org)
|
||||
}
|
||||
}
|
||||
entities[i].Organizations = organizations
|
||||
}
|
||||
|
||||
entities, err = s.svc.Users.CreateBulk(entities)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToUserModelSlice(entities), nil
|
||||
}
|
||||
|
||||
// GetUsersByProfile returns a list of users by profile
|
||||
func (s *userService) GetUsersByProfile(profile string) (retVal []viewmodel.User, err error) {
|
||||
users, err := s.svc.Users.GetUsersByProfile(profile)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
list := s.mapEntity.User.ToUserModelSlice(users)
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *userService) RemoveAddress(addressUUID string) error {
|
||||
return s.svc.Users.RemoveAddress(addressUUID)
|
||||
}
|
||||
|
||||
func (s *userService) SaveAddress(address viewmodel.Address) (retVal viewmodel.Address, err error) {
|
||||
entity := s.mapEntity.Address.ToAddressEntity(address)
|
||||
entity, err = s.svc.Users.SaveAddress(entity)
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.Address.ToAddressModel(entity), err
|
||||
}
|
||||
|
||||
func (s *userService) GetContactType() (retVal []viewmodel.ContactType, err error) {
|
||||
entity, err := s.svc.Users.GetContactType()
|
||||
if err != nil {
|
||||
return retVal, errors.Wrap(err)
|
||||
}
|
||||
|
||||
return s.mapEntity.User.ToContactTypeModelSlice(entity), nil
|
||||
}
|
||||
Reference in New Issue
Block a user