Пример #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,
            });
        }