/// <summary>
        /// Update the user setting profile given the identity user's username.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userSetting"></param>
        /// <param name="newsCategoryNames"></param>
        public void Update(string userName, UserSetting userSetting, ICollection <string> newsCategoryNames)
        {
            // Find the user setting
            var oldUserSetting = Read(userName);

            // When the setting is found
            if (oldUserSetting != null)
            {
                // And when at least one categories are provided
                if (newsCategoryNames != null)
                {
                    // Iterate over each news category
                    foreach (var news in newsCategoryNames)
                    {
                        // Create the news category
                        var userNewsCategory = new UserSettingNewsCategory
                        {
                            Id           = 0,
                            NewsCategory = _applicationdb.NewsCategories.FirstOrDefault(n => n.Name == news),
                            UserSetting  = userSetting
                        };
                        oldUserSetting.UserSettingNewsCategories.Add(userNewsCategory);
                    }
                }
                // Update all old settings that can be changed
                oldUserSetting.FirstName = userSetting.FirstName;
                oldUserSetting.LastName  = userSetting.LastName;
                _applicationdb.SaveChanges();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Delete a news source given a source ID
        /// And a user setting object.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="user"></param>
        public void DeleteNewsSource(int id, UserSetting user)
        {
            // Search for the user, and eager load the associations
            var        userAssoc       = Read(user.Id);
            NewsSource connectedSource = null;
            UserSettingNewsCategory connectedCategory = null;

            // Iterate over each category
            foreach (var category in userAssoc.UserSettingNewsCategories)
            {
                // Find the news category
                connectedCategory = category;
                var firstNewsSource = category.NewsSources.Where(s => s.Id == id);

                // When the news source is found
                if (firstNewsSource != null && firstNewsSource.Count() >= 1)
                {
                    // Add it to the list of sources that have been found connected to the user's category
                    connectedSource = firstNewsSource.First();
                }
            }

            // When the user, source, and category are identified to exist
            if (connectedSource != null && connectedCategory != null && userAssoc != null)
            {
                // Delete the news source
                _db.NewsSources.Remove(connectedSource);
                connectedCategory.NewsSources.Remove(connectedSource);
                _db.SaveChanges();
            }
        }
        /// <summary>
        /// Read in an identity user username and find the
        /// identity user object from the username.
        /// Then, find the associated user setting and return
        /// it along with all associations.
        /// The first time this is called, the identity user
        /// does not have an associated user setting, so it is created.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>UserSetting</returns>
        public UserSetting Read(string userName)
        {
            UserSetting  user         = null;
            IdentityUser identityUser = _applicationdb.Users.FirstOrDefault(u => u.UserName == userName);

            // When the identity user is not found
            if (identityUser != null)
            {
                // Get the user setting
                user = _applicationdb.UserSettings.Include(u => u.UserSettingNewsCategories).FirstOrDefault(u => u.IdentityUserId == identityUser.Id);
                // If the user is not found
                if (user == null)
                {
                    // Create the user profile settings
                    user = new UserSetting
                    {
                        Id             = 0,
                        IdentityUserId = identityUser.Id,
                        User           = identityUser,
                        FirstName      = "",
                        LastName       = "",
                        Country        = "us",
                        Language       = "en"
                    };

                    // Create the default general news category
                    var userNewsCategory = new UserSettingNewsCategory
                    {
                        Id           = 0,
                        NewsCategory = _applicationdb.NewsCategories.Include(u => u.UserSettingNewsCategories).FirstOrDefault(n => n.Name == "general"),
                        UserSetting  = user
                    };

                    // Add the news category
                    user.UserSettingNewsCategories.Add(userNewsCategory);

                    // Save all the additions
                    _applicationdb.UserSettings.Add(user);
                    _applicationdb.SaveChanges();
                }
            }

            // When the user is found
            if (user != null)
            {
                // Iterate over each news settings
                foreach (var settingNews in user.UserSettingNewsCategories)
                {
                    // Load all associations
                    _applicationdb.Entry(settingNews).Reference("NewsCategory").Load();
                    _applicationdb.Entry(settingNews).Collection("NewsSources").Load();
                }
            }

            return(user);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a news source given a source name and a category
        /// </summary>
        /// <param name="name"></param>
        /// <param name="category"></param>
        public void CreateNewsSource(string name, UserSettingNewsCategory category)
        {
            // Instantiate a new news source
            var source = new NewsSource
            {
                Id       = 0,
                SourceId = name,
                UserSettingNewsCategory   = category,
                UserSettingNewsCategoryId = category.Id
            };

            // Add the source to the news sources
            _db.NewsSources.Add(source);
            // Also add the news source to the category
            category.NewsSources.Add(source);
            // Save the changes
            _db.SaveChanges();
        }