public OperationResult <int> Create(UserCreateDto dto)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            if (string.IsNullOrWhiteSpace(dto.FirstName))
            {
                return(new OperationResult <int>
                {
                    Success = false,
                    Message = "First name is empty"
                });
            }

            if (dto.FirstName.Length > 200)
            {
                return(new OperationResult <int>
                {
                    Success = false,
                    Message = "First name is too long"
                });
            }

            if (string.IsNullOrWhiteSpace(dto.LastName))
            {
                return(new OperationResult <int>
                {
                    Success = false,
                    Message = "Last name is empty"
                });
            }

            if (dto.LastName.Length > 200)
            {
                return(new OperationResult <int>
                {
                    Success = false,
                    Message = "Last name is too long"
                });
            }

            var entity = new UserEntity
            {
                Created   = DateTime.UtcNow,
                BirthDate = dto.BirthDate,
                FirstName = dto.FirstName,
                LastName  = dto.LastName,
                IsActive  = true
            };

            var id = _repository.Create(entity);

            return(new OperationResult <int>
            {
                Success = true,
                Data = id
            });
        }
Example #2
0
        public int Create(User user)
        {
            user.Password = HashHelper.GetHash(user.Password);

            _userRepository.Create(user);

            return(_userRepository.SaveChanges());
        }
Example #3
0
        public void AcceptInvitationForNewUser(ShareInvitation shareInvitation, User newUser)
        {
            var shareInvitations = _shareInvitationRepository.GetAll()
                                   .Where(x => x.Email == shareInvitation.Email.ToLower() && x.InvitationStatus == InvitationStatusEnum.Invited).ToList();

            newUser.ServiceDescription_Users = new List <ServiceDescription_User>();

            foreach (var invitation in shareInvitations)
            {
                //Update the invitation status to Accepted
                invitation.InvitationStatus = InvitationStatusEnum.Accepted;

                //Update the entity with the new invitation status in the EF context
                _shareInvitationRepository.Update(invitation);

                var idServiceDescription = invitation.IdServiceDescription;

                //Generate the service description for the (invited) shared user
                var serviceDescription_User = new ServiceDescription_User
                {
                    IdServiceDescription = idServiceDescription
                };

                //Add the link between the (invited) shared user and the service description
                newUser.ServiceDescription_Users.Add(serviceDescription_User);

                var ontologiesUsedByServiceDescription = _serviceDescription_OntologyRepository.GetAll().Where(x => x.IdServiceDescription == idServiceDescription);
                var ontologiesSharedByOpenedInProject  = _shareInvitation_OntologyRepository.GetAll().Where(x => x.IdShareInvitation == invitation.Id);
                newUser.Ontology_Users = new List <Ontology_User>();

                foreach (var idOntology in ontologiesUsedByServiceDescription.Select(x => x.IdOntology).ToList())
                {
                    if (!newUser.Ontology_Users.Any(x => x.IdOntology == idOntology))
                    {
                        newUser.Ontology_Users.Add(new Ontology_User {
                            IdOntology = idOntology
                        });
                    }
                }

                foreach (var idOntology in ontologiesSharedByOpenedInProject.Select(x => x.IdOntology).ToList())
                {
                    if (!newUser.Ontology_Users.Any(x => x.IdOntology == idOntology))
                    {
                        newUser.Ontology_Users.Add(new Ontology_User {
                            IdOntology = idOntology
                        });
                    }
                }
            }

            //Hash the password for the invited user
            newUser.Password = HashHelper.GetHash(newUser.Password);

            //Create the the (invited) shared user in the EF context
            _userRepository.Create(newUser);

            //Save the (invited) shared user in the database via the EF context
            _userRepository.SaveChanges();

            //Save the updated invitation, with new status, in the database via the EF context
            _shareInvitationRepository.SaveChanges();
        }