Exemple #1
0
        /// <summary>
        /// This method is used to make user register.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>register response string.</returns>
        public async Task <RegisterResponse> RegisterUser(RegisterRequest request)
        {
            var existingUser = await _userManager.GetUserByEmail(request.Email);

            if (existingUser != null)
            {
                return(new RegisterResponse()
                {
                    StatusCode = StatusCode.AlreadyExists,
                    ErrorMessage = "Email already exist",
                });
            }
            if (!request.IsValid())
            {
                return(new RegisterResponse()
                {
                    StatusCode = StatusCode.InvalidArgument,
                    ErrorMessage = "Empty argument provided",
                });
            }

            var newUser = new User
            {
                Email          = request.Email, Name = request.Name, Friends = new List <string>(),
                FriendRequests = new List <string>(), FriendRequestsSent = new List <string>(),
            };

            newUser.SetPassword(request.Password);
            var added = await _userManager.InsertUser(newUser);

            if (!added)
            {
                return(new RegisterResponse()
                {
                    StatusCode = StatusCode.Internal,
                    ErrorMessage = "Internal error! Couldn't insert user"
                });
            }

            return(new RegisterResponse()
            {
                Token = newUser.GetToken(),
                StatusCode = StatusCode.Ok,
            });
        }
Exemple #2
0
        public async Task <UpdateUserResponse> UpdateUser(UpdateUserRequest request, string context)
        {
            var currentUser = await _userManager.GetUser(context);

            if (currentUser == null)
            {
                return(new UpdateUserResponse()
                {
                    StatusCode = StatusCode.Unauthenticated,
                    ErrorMessage = "Unauthenticated user",
                });
            }

            if (!request.IsValid())
            {
                return(new UpdateUserResponse()
                {
                    StatusCode = StatusCode.InvalidArgument,
                    ErrorMessage = "Empty argument provided",
                });
            }

            var unavailableEmail = await _userManager.GetUserByEmail(request.Email);

            if (unavailableEmail != null)
            {
                return(new UpdateUserResponse()
                {
                    StatusCode = StatusCode.AlreadyExists,
                    ErrorMessage = "This Email already exists",
                });
            }

            User user = new User()
            {
                Id                 = currentUser.Id,
                Name               = request.Name,
                Email              = request.Email,
                Friends            = currentUser.Friends,
                FriendRequests     = currentUser.FriendRequests,
                FriendRequestsSent = currentUser.FriendRequestsSent,
            };

            user.SetPassword(request.Password);

            var update = await _userManager.UpdateUser(currentUser.Id, user);

            if (!update)
            {
                return(new UpdateUserResponse()
                {
                    StatusCode = StatusCode.Internal,
                    ErrorMessage = "Internal Error! Couldn't update user",
                });
            }
            return(new UpdateUserResponse()
            {
                Name = user.Name,
                Email = user.Email,
                StatusCode = StatusCode.Ok,
            });
        }