Esempio n. 1
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);
        }
Esempio n. 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");
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }