예제 #1
0
        /// <summary>
        /// Service method to update individual user profile information
        /// </summary>
        /// <param name="updatedUser">User Model</param>
        /// <returns>Completed Task</returns>

        public async Task UpdateUserProfile(UserModel updatedUser)
        {
            //validate updatedUser
            var validUser = ValidateUser(updatedUser);

            //if updatedUser is valid
            if (validUser)
            {
                //pull user account
                var user = await _userRepository.GetUserByUserName(updatedUser.Username);

                //validate user
                if (user == null)
                {
                    throw new Exception("Invalid User");
                }

                updatedUser.Photo = new DomainPhoto();

                //map updatedUser to user
                user = EfUserMapper.CoreModelToDbEntity(updatedUser);

                //submit updated information to repository
                await _userRepository.UpdateUserProfile(user);
            }
            else
            {
                throw new Exception("Invalid User");
            }
        }
예제 #2
0
        /// <summary>
        /// Service method to Create new Core User
        /// </summary>
        /// <param name="newUser">User Model</param>
        /// <returns>User Id </returns>
        public async Task <UserModel> CreateNewUser(UserModel newUser)
        {
            //validate new user
            bool validUser = ValidateUser(newUser);

            if (validUser)
            {
                //if user properties are valid
                try
                {
                    //check to see if user by user name exists
                    var existingUser = await _userRepository.GetUserByUserName(newUser.Username);

                    if (existingUser == null)
                    {
                        throw new Exception("User does not exist");
                    }
                }
                catch
                {
                    //if user doesn't exist, create new user
                    var createdUser = await _userRepository.CreateNewUser(EfUserMapper.CoreModelToDbEntity(newUser));

                    //map db user to domain user
                    var newDomainUser = EfUserMapper.DbEntityToCoreModel(createdUser);

                    //map photo id to photo for domain user
                    if (createdUser.PhotoId != null && createdUser.PhotoId > 0)
                    {
                        var photo = await _stockPhoto.GetPhotoById((long)createdUser.PhotoId);

                        newDomainUser.Photo = new DomainPhoto()
                        {
                            Id = photo.Id, URL = photo.Source.Medium
                        };
                    }

                    //return newly created user
                    return(newDomainUser);
                }

                // if user exists, throw exception
                throw new Exception("Username already exists");
            }
            else
            {
                //if input isn't valid, throw exception
                throw new Exception("Invalid User");
            }
        }