Ejemplo n.º 1
0
        public async Task <TypedResult <UserOutDto> > CreateUser(UpdateUserDtoIn updateUserIn)
        {
            try
            {
                var newUser = new UserEntity()
                {
                    UserId      = Guid.NewGuid(),
                    Email       = updateUserIn.Email,
                    ReportingTo = updateUserIn.ReportingTo,
                    Name        = new NameEntity()
                    {
                        FirstName          = updateUserIn.Name.FirstName,
                        LastName           = updateUserIn.Name.LastName,
                        MiddleName         = updateUserIn.Name.MiddleName,
                        Salutation         = updateUserIn.Name.Salutation,
                        PreferredFirstName = updateUserIn.Name.PreferredFirstName
                    },
                    Roles  = updateUserIn.Roles.Select(x => x.ToString()),
                    Status = updateUserIn.Status
                };

                await UserCollection.InsertOneAsync(newUser);

                return(new SuccessfulTypedResult <UserOutDto>(UserOutDto.EntityToOutDto(newUser)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <UserOutDto>(e));
            }
        }
Ejemplo n.º 2
0
        public async Task <TypedResult <UserOutDto> > Create(NewUserInDto newUserIn)
        {
            try
            {
                var existingUserByEmail = (await UsersCollection.FindAsync(u => u.Email == newUserIn.Email)).FirstOrDefault();
                if (existingUserByEmail != null)
                {
                    return(new BadRequestTypedResult <UserOutDto>("User with given email already exists."));
                }

                var newUser = new UserEntity()
                {
                    UserId    = Guid.NewGuid(),
                    FirstName = newUserIn.FirstName,
                    LastName  = newUserIn.LastName,
                    Email     = newUserIn.Email
                };

                await UsersCollection.InsertOneAsync(newUser);

                return(new SuccessfulTypedResult <UserOutDto>(UserOutDto.EntityToOutDto(newUser)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <UserOutDto>(e));
            }
        }
Ejemplo n.º 3
0
        public async Task <TypedResult <UserOutDto> > UpdateUser(Guid userId, UpdateUserDtoIn newUserId)
        {
            try
            {
                var foundUser = (await UserCollection.FindAsync(c => c.UserId == userId)).FirstOrDefault();
                if (foundUser == null)
                {
                    return(new NotFoundTypedResult <UserOutDto>());
                }

                foundUser.Name = new NameEntity()
                {
                    FirstName          = newUserId.Name.FirstName,
                    LastName           = newUserId.Name.LastName,
                    MiddleName         = newUserId.Name.MiddleName,
                    Salutation         = newUserId.Name.Salutation,
                    PreferredFirstName = newUserId.Name.PreferredFirstName
                };
                foundUser.Email       = newUserId.Email;
                foundUser.ReportingTo = newUserId.ReportingTo;
                foundUser.Status      = newUserId.Status;
                foundUser.Roles       = newUserId.Roles.Select(x => x.ToString());

                await UserCollection.ReplaceOneAsync(u => u.UserId == userId, foundUser, new ReplaceOptions()
                {
                    IsUpsert = false
                });

                return(new SuccessfulTypedResult <UserOutDto>(UserOutDto.EntityToOutDto(foundUser)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <UserOutDto>(e));
            }
        }
Ejemplo n.º 4
0
 public static GroupUser AsDbModel(this UserOutDto user)
 => new GroupUser
 {
     Id       = user.Id,
     Email    = user.Email,
     Username = user.Username
 };
Ejemplo n.º 5
0
        public async Task <bool> UpdateUser(UserOutDto user)
        {
            var userToUpdate = await _unitOfWork.UserRepo.GetAsync(user.UserId);

            userToUpdate.Name  = user.Name;
            userToUpdate.Email = user.Email;
            return(await _unitOfWork.UserRepo.SaveAsync() > 0);
        }
Ejemplo n.º 6
0
        public async Task <UserOutDto> AddGroupMember(
            [Service] IGroupService service,
            string groupId,
            UserOutDto member)
        {
            await service.AddMember(groupId, member);

            return(member);
        }
Ejemplo n.º 7
0
        public async Task AddMember(string groupId, UserOutDto member)
        {
            var group = (await groupRepository.GetGroup(groupId)).AsOutModel();

            if (group.Users.Select(x => x.Id).Contains(member.Id))
            {
                throw new ArgumentException($"member {member.Id} already exists in {groupId}");
            }

            group.Users.Add(member);

            await groupRepository.Upsert(group.AsDbModel());
        }
Ejemplo n.º 8
0
        public async Task <TypedResult <UserOutDto> > GetUser(Guid userId)
        {
            try
            {
                var foundUser = (await UserCollection.FindAsync(c => c.UserId == userId)).FirstOrDefault();
                if (foundUser == null)
                {
                    return(new NotFoundTypedResult <UserOutDto>());
                }

                return(new SuccessfulTypedResult <UserOutDto>(UserOutDto.EntityToOutDto(foundUser)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <UserOutDto>(e));
            }
        }
        public ActionResult <IEnumerable <UserOutDto> > Get()
        {
            IEnumerable <UserOutDto> result;

            if (HttpContext.User.IsInRole("Administrators"))
            {
                result = Map(_userRepository.All());
            }
            else if (HttpContext.User.IsInRole("Users"))
            {
                result = Map(_userRepository.All(), true);
            }
            else
            {
                result = new UserOutDto[] { };
            }

            return(Ok(result));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> GetSingleUser(Guid userId)
        {
            var getUserResult = await UserService.Get(userId);

            switch (getUserResult)
            {
            case FailedTypedResult <UserEntity> failedTypedResult:
                return(StatusCode(StatusCodes.Status500InternalServerError, failedTypedResult.Error));

            case NotFoundTypedResult <UserEntity> _:
                return(NotFound());

            case SuccessfulTypedResult <UserEntity> successfulTypedResult:
                return(Ok(UserOutDto.EntityToOutDto(successfulTypedResult.Value)));

            default:
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  new ArgumentOutOfRangeException(nameof(getUserResult))));
            }
        }
Ejemplo n.º 11
0
        public async Task <TypedResult <UserOutDto> > Update(Guid userId, UpdateUserDto updatedUserIn)
        {
            try
            {
                var foundUser = (await UsersCollection.FindAsync(u => u.UserId == userId)).FirstOrDefault();
                if (foundUser == null)
                {
                    return(new NotFoundTypedResult <UserOutDto>());
                }

                var updates = new List <UpdateDefinition <UserEntity> >();
                if (!string.IsNullOrWhiteSpace(updatedUserIn.FirstName))
                {
                    updates.Add(Builders <UserEntity> .Update.Set(u => u.FirstName, updatedUserIn.FirstName));
                }

                if (!string.IsNullOrWhiteSpace(updatedUserIn.LastName))
                {
                    updates.Add(Builders <UserEntity> .Update.Set(u => u.LastName, updatedUserIn.LastName));
                }

                if (!string.IsNullOrWhiteSpace(updatedUserIn.Email))
                {
                    updates.Add(Builders <UserEntity> .Update.Set(u => u.Email, updatedUserIn.Email));
                }

                var updatedUser = await UsersCollection.FindOneAndUpdateAsync <UserEntity>(u => u.UserId == userId,
                                                                                           Builders <UserEntity> .Update.Combine(updates), GetEntityAfterUpdateOption <UserEntity>());

                return(new SuccessfulTypedResult <UserOutDto>(UserOutDto.EntityToOutDto(updatedUser)));
            }
            catch (Exception e)
            {
                return(new FailedTypedResult <UserOutDto>(e));
            }
        }