public IHttpActionResult GetStuffCourses(int UserId)
        {
            List <AspStuffCourses> obj = new List <AspStuffCourses>();

            if (_context.StuffCourses.Any())
            {
                var allStuffCourses = from c in _context.StuffCourses
                                      where c.UserId == UserId
                                      select new AspStuffCoursesDto
                {
                    Id         = c.Id,
                    CourseId   = c.CourseId,
                    CourseName = c.AspCourse.CourseName,
                    CourseTit  = c.AspCourse.CourseTitle,
                    DepId      = c.DepId,
                    DepName    = c.AspCourse.AspDepartment.Name
                };
                foreach (var row in allStuffCourses)
                {
                    AspStuffCourses _obj = new AspStuffCourses();
                    _obj.Id         = row.Id;
                    _obj.CourseId   = row.CourseId;
                    _obj.CourseName = row.CourseName;
                    _obj.CourseTit  = row.CourseTit;
                    _obj.DepId      = row.DepId;
                    _obj.DeptName   = row.DepName;
                    obj.Add(_obj);
                }
                return(Json(obj));
            }
            else
            {
                return(Json(obj));
            }
        }
Exemplo n.º 2
0
        public ActionResult Save(AspStuffCourseVM stuffCourseVM)
        {
            if (CheckCookies() == true)
            {
                var firstname = stuffCourseVM.AspUser.FirstName = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).FirstName;
                var lastname  = stuffCourseVM.AspUser.LastName = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).LastName;
                var userId    = stuffCourseVM.AspUser.UserId = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).UserId;
                var roleId    = stuffCourseVM.AspUser.AspRoleId = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).AspRoleId;
                var email     = stuffCourseVM.AspUser.Email = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).Email;
                var authkey   = stuffCourseVM.AspUser.AuthKey = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).AuthKey;
                var password  = stuffCourseVM.AspUser.Password = _context.Users.Single(c => c.UserId == stuffCourseVM.AspUser.UserId).Password;
                // ------------------------------------------------------------------------------------------------------------------------------------
                var             coursename      = stuffCourseVM.AspCourse.CourseName = _context.Courses.Single(c => c.CourseId == stuffCourseVM.AspCourse.CourseId).CourseName;
                var             coursetit       = stuffCourseVM.AspCourse.CourseTitle = _context.Courses.Single(c => c.CourseId == stuffCourseVM.AspCourse.CourseId).CourseTitle;
                var             courseid        = stuffCourseVM.AspCourse.CourseId = _context.Courses.Single(c => c.CourseId == stuffCourseVM.AspCourse.CourseId).CourseId;
                var             depid           = stuffCourseVM.AspCourse.AspDepartmentId = _context.Courses.Single(c => c.CourseId == stuffCourseVM.AspCourse.CourseId).AspDepartmentId;
                AspStuffCourses newStuffCourses = new AspStuffCourses
                {
                    CourseName = coursename,
                    CourseTit  = coursetit,
                    CourseId   = courseid,
                    DepId      = depid,

                    FirstName = firstname,
                    LastName  = lastname,
                    UserId    = userId,
                    RoleId    = roleId,
                    Email     = email,
                    AuthKey   = authkey.ToString(),
                    Password  = password,
                };
                if (_context.StuffCourses.Any(c => c.UserId == stuffCourseVM.AspUser.UserId && c.CourseId == stuffCourseVM.AspCourse.CourseId))
                {
                    return(RedirectToAction("Index"));
                }
                _context.StuffCourses.Add(newStuffCourses);
                //if(stuffCourseVM.AspCourse.CourseId == 0 && stuffCourseVM.AspUser.UserId == 0)
                //{

                //}
                //else
                //{
                //    var stuffCourseInDb = _context.StuffCourses.Single(c => c.CourseId == stuffCourseVM.AspCourse.CourseId && c.UserId == stuffCourseVM.AspUser.UserId);
                //    stuffCourseInDb.FirstName = firstname;
                //    stuffCourseInDb.LastName = lastname;
                //    stuffCourseInDb.Email = email;
                //    stuffCourseInDb.AuthKey = authkey.ToString();
                //    stuffCourseInDb.Password = password;
                //    stuffCourseInDb.CourseName = coursename;
                //    stuffCourseInDb.CourseTit = coursetit;
                //    stuffCourseInDb.RoleId = roleId;
                //    stuffCourseInDb.UserId = userId;
                //    stuffCourseInDb.CourseId = courseid;
                //    stuffCourseInDb.DepId = depid;
                //}

                _context.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Logout", "User"));
            }
        }