Пример #1
0
        /// <summary>
        /// Adds a user to a selected course in the database and on object level
        /// </summary>
        /// <param name="course">Selected Course</param>
        /// <param name="user">User</param>
        /// <returns></returns>
        public static async Task AddUserToSelectedCourseAsync(SelectedCourse course, User user)
        {
            using (var db = new DatabaseContext(_filename, _basePath))
            {
                var selUser = new SelectedCourseUser()
                {
                    SelectedCourse = course, User = user
                };
                db.Entry(selUser).State = EntityState.Added;
                await db.SaveChangesAsync();

                course.Users.Add(selUser);
            }
        }
Пример #2
0
        /// <summary>
        /// This method removes a User from a Selected Course in the Database and the Objects
        /// </summary>
        /// <param name="selCourse">SelectedCourse</param>
        /// <param name="user">User</param>
        /// <returns></returns>
        public static async Task RemoveUserFromSelectedCourseAsync(SelectedCourse selCourse, SelectedCourseUser user)
        {
            using (var db = new DatabaseContext(_filename, _basePath))
            {
                await db.Database.ExecuteSqlCommandAsync($"DELETE FROM SelectedCourseUser WHERE SelectedCourseId={selCourse.Id} AND UserId = {user.User.Id} ");

                //Remove user from selected course and the connected course from the user
                selCourse.Users.RemoveAll(t => t.User.Id == user.User.Id);
                user.User.SelectedCourses.Remove(user);
            }
        }