Exemple #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            // @await Html.PartialAsync("_LoginPartial")
            //int code = User.Identity.GetHashCode();
            //Review.Score = code;

            // Need to get username as string somehow from AccountController

            // 1How to look up Review.StudentID from authenticated user?
            //2 How to relate user's StudentID to Student table?
            string uname;
            {
                System.IO.StreamReader file = new System.IO.StreamReader(@"uname.txt");
                uname = file.ReadLine();
                file.Close();
            }
            int studentID = 0;
            //FILEIO
            int n = _context.Students.Count();

            {
                List <Student> students = _context.Students.ToList();
                for (int i = 0; i < students.Count; i++)
                {
                    if (students[i].LastName == uname)
                    {
                        studentID = students[i].StudentID;
                        break;
                    }
                }
            }
            if (studentID == 0)
            {
                Student stu = new Student();
                //stu.StudentID = i+1;
                stu.FirstMidName = uname;
                stu.LastName     = uname;
                _context.Students.Add(stu);
                _context.SaveChanges();

                List <Student> students = _context.Students.ToList();
                studentID = students[0].StudentID;
                for (int i = 0; i < students.Count; i++)
                {
                    if (students[i].LastName == uname)
                    {
                        studentID = students[i].StudentID;
                        break;
                    }
                }
            }
            // return RedirectToPage("./Index");

            Review.StudentID = studentID;
            _context.Reviews.Add(Review);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public static void Initialize(SDSUContext context)
        {
            context.Database.EnsureCreated();

            // Look for any students.
            if (context.Students.Any())
            {
                return;   // DB has been seeded
            }

            var students = new Student[]
            {
                new Student {
                    FirstMidName = "Carson", LastName = "Alexander"
                },
                new Student {
                    FirstMidName = "Meredith", LastName = "Alonso"
                },
                new Student {
                    FirstMidName = "Arturo", LastName = "Anand"
                },
                new Student {
                    FirstMidName = "Gytis", LastName = "Barzdukas"
                },
                new Student {
                    FirstMidName = "Yan", LastName = "Li"
                },
                new Student {
                    FirstMidName = "Peggy", LastName = "Justice"
                },
                new Student {
                    FirstMidName = "Laura", LastName = "Norman"
                },
                new Student {
                    FirstMidName = "Nino", LastName = "Olivetto"
                }
            };

            foreach (Student s in students)
            {
                context.Students.Add(s);
            }
            context.SaveChanges();

            var classes = new Class[]
            {
                new Class {
                    ClassID = 1050, Title = "Chemistry"
                },
                new Class {
                    ClassID = 4022, Title = "Microeconomics"
                },
                new Class {
                    ClassID = 4041, Title = "Macroeconomics"
                },
                new Class {
                    ClassID = 1045, Title = "Calculus"
                },
                new Class {
                    ClassID = 3141, Title = "Trigonometry"
                },
                new Class {
                    ClassID = 2021, Title = "Composition"
                },
                new Class {
                    ClassID = 2042, Title = "Literature"
                }
            };

            foreach (Class c in classes)
            {
                context.Classes.Add(c);
            }
            context.SaveChanges();

            var reviews = new Review[]
            {
                new Review {
                    StudentID = 1, ClassID = 1050, Score = 2, Text = "word1"
                },
                new Review {
                    StudentID = 1, ClassID = 4022, Score = 3, Text = "word1"
                },
                new Review {
                    StudentID = 1, ClassID = 4041, Score = 4, Text = "word1"
                },
                new Review {
                    StudentID = 2, ClassID = 1045, Score = 5, Text = "word1"
                },
                new Review {
                    StudentID = 2, ClassID = 3141, Score = 2, Text = "word1"
                },
                new Review {
                    StudentID = 2, ClassID = 2021, Score = 3, Text = "word1"
                },
                new Review {
                    StudentID = 3, ClassID = 1050, Score = 4, Text = "word1"
                },
                new Review {
                    StudentID = 4, ClassID = 1050, Score = 5, Text = "word1"
                },
                new Review {
                    StudentID = 4, ClassID = 4022, Score = 2, Text = "word1"
                },
                new Review {
                    StudentID = 5, ClassID = 4041, Score = 3, Text = "word1"
                },
                new Review {
                    StudentID = 6, ClassID = 1045, Score = 4, Text = "word1"
                },
                new Review {
                    StudentID = 7, ClassID = 3141, Score = 5, Text = "word1"
                },
            };

            foreach (Review e in reviews)
            {
                context.Reviews.Add(e);
            }
            context.SaveChanges();
        }