예제 #1
0
        /// <summary>
        /// Method to create new user account
        /// Validates user does not exist based on email address
        /// </summary>
        /// <param name="coreUser"></param>
        /// <returns>Completed Task if new user account is created</returns>
        public async Task <long> CreateNewUserAccount(CoreUser coreUser)
        {
            try
            {
                //pull any user that exists with email address provided
                var existingUser = await _userRepository.GetUserByEmail(coreUser.Email);
            }
            catch (Exception)
            {
                //map from core to db user
                var dbUser = EfUserMapper.CoreToDbUser(coreUser);
                dbUser.Created = DateTime.Now;
                dbUser.Updated = DateTime.Now;

                //encrypt password
                var hashCode = _passwordService.CreatePasswordHash(coreUser.Password);
                dbUser.Password = hashCode;

                //create new user account with repository method
                var userId = await _userRepository.CreateNewUserAccount(dbUser);

                return(userId);
            }

            //validate that user is null
            throw new Exception("User with associated email exists.");
        }
예제 #2
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");
            }
        }
예제 #3
0
        /// <summary>
        /// Service method to pull single user by username
        /// </summary>
        /// <param name="username">Username as string</param>
        /// <returns>Domain Model User</returns>
        public async Task <UserModel> GetUserByUsername(string username)
        {
            //validate parameter is not null or empty
            if (username == null || username == "")
            {
                throw new ArgumentException("Username not provided");
            }

            //create new domain user
            UserModel user = new UserModel();

            //pull user from db
            var dbuser = await _userRepository.GetUserByUserName(username);

            //validate db call returned user
            if (dbuser == null)
            {
                throw new Exception("User not found");
            }

            //convert db user to domain user
            user = EfUserMapper.DbEntityToCoreModel(dbuser);

            if (dbuser.PhotoId != null && dbuser.PhotoId > 0)
            {
                var photo = await _stockPhoto.GetPhotoById((long)dbuser.PhotoId);

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

            return(user);
        }
예제 #4
0
        /// <summary>
        /// Method to get Core User Object by User ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Core User</returns>

        public async Task <CoreUser> GetUserById(long id)
        {
            var dbUser = await _userRepository.GetUserByUserId(id);

            if (dbUser == null)
            {
                throw new Exception("User does not exist");
            }

            CoreUser coreUser = EfUserMapper.DbToCoreUser(dbUser);

            return(coreUser);
        }
예제 #5
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");
            }
        }
예제 #6
0
        /// <summary>
        /// Method to pull user object by email
        /// </summary>
        /// <param name="email"></param>
        /// <returns>Core User</returns>
        public async Task <CoreUser> GetUserByEmail(string email)
        {
            //pull user object
            var dbUser = await _userRepository.GetUserByEmail(email);

            //validate user exists
            if (dbUser == null)
            {
                throw new Exception("User does not exist");
            }

            //map db user to core user
            CoreUser coreUser = EfUserMapper.DbToCoreUser(dbUser);

            return(coreUser);
        }
예제 #7
0
        /// <summary>
        /// Service method to log a user in
        /// </summary>
        /// <param name="userName">Username</param>
        /// <param name="password">Password</param>
        /// <returns>username</returns>
        public async Task <UserModel> Login(string userName, string password)
        {
            //validate inputs
            if (userName == null || password == null)
            {
                throw new Exception("Invalid input");
            }

            //pull user account
            var user = await _userRepository.GetUserByUserName(userName);

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

            //if user exists validate passwords match
            if (user.Password != password)
            {
                throw new Exception("Invalid password");
            }

            //if passwords match, update user status
            await _userRepository.UpdateUserStatus(user.Id, true);

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

            if (user.PhotoId != null && user.PhotoId > 0)
            {
                var picture = await _stockPhoto.GetPhotoById((long)user.PhotoId);

                domainUser.Photo = new DomainPhoto()
                {
                    Id = picture.Id, URL = picture.Source.Medium
                };
            }

            return(domainUser);
        }
예제 #8
0
        /// <summary>
        /// Service method to pull users by location
        /// </summary>
        /// <param name="location">Location as string</param>
        /// <returns>List of Domain User Models</returns>
        public async Task <List <UserModel> > GetUsersByLocation(string location)
        {
            //validate location input
            if (string.IsNullOrEmpty(location))
            {
                throw new ArgumentException("Location not provided");
            }
            //create empty list of domain users
            List <UserModel> usersByLocation = new List <UserModel>();

            var dbUsersList = await _userRepository.GetUsersByLocation(location);

            //validate db users list is not empty
            if (dbUsersList == null || dbUsersList.Count < 0)
            {
                throw new Exception("Users not found");
            }

            //map db users to domain list
            foreach (var dbUser in dbUsersList)
            {
                var user = EfUserMapper.DbEntityToCoreModel(dbUser);

                if (dbUser.PhotoId != null && dbUser.PhotoId > 0)
                {
                    var picture = await _stockPhoto.GetPhotoById((long)dbUser.PhotoId);

                    user.Photo = new DomainPhoto()
                    {
                        Id = picture.Id, URL = picture.Source.Medium
                    };
                }

                usersByLocation.Add(user);
            }

            return(usersByLocation);
        }
예제 #9
0
        /// <summary>
        /// Service method to pull users from database by User Id
        /// </summary>
        /// <param name="userIds">List of User Ids</param>
        /// <returns>List of Domain Model Users</returns>
        public async Task <List <UserModel> > GetUsersByUserId(List <long> userIds)
        {
            //create empty list of domain users
            List <UserModel> allUsers = new List <UserModel>();

            //pull all users from db
            var dbUsers = await _userRepository.GetUsersByUserId(userIds);

            if (dbUsers != null)
            {
                //map each db user to Domain Model and add to domain users list
                foreach (var dbUser in dbUsers)
                {
                    var user = EfUserMapper.DbEntityToCoreModel(dbUser);
                    if (dbUser.PhotoId != null && dbUser.PhotoId > 0)
                    {
                        var picture = await _stockPhoto.GetPhotoById((long)dbUser.PhotoId);

                        user.Photo = new DomainPhoto()
                        {
                            Id = picture.Id, URL = picture.Source.Medium
                        };
                    }

                    allUsers.Add(user);
                }
            }
            else
            {
                throw new Exception("No Users found");
            }


            //return domain users list
            return(allUsers);
        }