Exemplo n.º 1
0
        public async Task <IActionResult> OnGet()
        {
            // Repositorys
            var classRepository = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
            var @class          = (await classRepository.Select(@class: new Models.Class()
            {
                Guid = ClassGuid
            })).FirstOrDefault();

            if (string.IsNullOrEmpty(@class.SubjectChineseName) &&
                string.IsNullOrEmpty(@class.Teachers) &&
                string.IsNullOrEmpty(@class.ClassShortName))
            {
                ModelState.AddModelError("Error", "查無該課程資料,請依照流程填寫");
                return(Page());
            }
            else
            {
                Comment = new Models.Comment()
                {
                    /* Fixed value */
                    SubjectChineseName = @class.SubjectChineseName,
                    Teachers           = @class.Teachers,
                    ClassShortName     = @class.ClassShortName,
                    ClassGuid          = @class.Guid,
                    /* Default value */
                    RollCallFrequency = 2m, // [0, 1, 2, 3, 4]
                    Grade             = 3
                };
                return(Page());
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPost()
        {
            // Repositorys
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);
            var comment           = (await commentRepository.Select(comment: new Models.Comment()
            {
                Guid = CommentGuid
            })).FirstOrDefault();

            if (comment == null)
            {
                TempData["Error"] = $"查無評論 { CommentGuid } 的課程資訊";
                return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
            }
            else
            {
                var student = JsonConvert.DeserializeObject <Models.Student>(
                    User.Claims.First(claim => claim.Type == "Information").Value);
                if (comment.Creator == student.Username)
                {
                    // Check comment's class info
                    var classRepository = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                    var classes         = (await classRepository.Select(@class: new Models.Class()
                    {
                        Guid = comment.ClassGuid
                    })).ToList();

                    // Insert class info to comment
                    if (classes.Count > 0)
                    {
                        Comment.SubjectChineseName = classes.First().SubjectChineseName;
                        Comment.Teachers           = classes.First().Teachers;
                        Comment.ClassShortName     = classes.First().ClassShortName;
                        Comment.RecordTime         = DateTime.Now;
                    }

                    // Insert comment value
                    Comment.Creator = student.Username;
                    Comment.Guid    = CommentGuid;

                    // Update comment
                    if (await commentRepository.Update(comment: Comment) > 0)
                    {
                        TempData["Success"] = "修改評論成功";
                        return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "修改評論失敗");
                        return(Page());
                    }
                }
                else
                {
                    TempData["Error"] = "僅評論者本人可修改此評論";
                    return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
                }
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnGetAsync()
        {
            if (string.IsNullOrEmpty(CommentGuid))
            {
                return(RedirectToPage("/Courses/Index"));
            }
            else
            {
                // Message by redirect
                if (TempData.ContainsKey("Success"))
                {
                    ModelState.AddModelError("Success", TempData["Success"].ToString());
                }
                if (TempData.ContainsKey("Error"))
                {
                    ModelState.AddModelError("Error", TempData["Success"].ToString());
                }

                // Repositorys
                var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

                var comments = (await commentRepository.Select(comment: new Models.Comment()
                {
                    Guid = CommentGuid
                })).ToList();
                if (comments.Count <= 0)
                {
                    Comment = new Models.Comment();
                    ModelState.AddModelError("Error", "此評論不存在或已被移除");
                }
                else
                {
                    Comment   = comments.FirstOrDefault();
                    IsCreator = Comment.Creator.Equals(
                        JsonConvert.DeserializeObject <Models.Student>(
                            User.Claims.First(claim => claim.Type == "Information").Value).Username);
                    var classes = (await classRepository.Select(@class: new Models.Class()
                    {
                        Guid = Comment.ClassGuid
                    })).ToList();
                    if (classes.Count <= 0)
                    {
                        ModelState.AddModelError("Error", $"查無評論 { Comment.Guid } 之課程資訊");
                    }
                    Class = classes.FirstOrDefault();
                }

                return(Page());
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync()
        {
            var student = JsonConvert.DeserializeObject <Models.Student>(
                User.Claims.First(claim => claim.Type == "Information").Value);
            var kuasAp = new Services.KUASAPService();

            kuasAp.GetCourseListAndSchedule(student: student, year: SysValue.Split(',')[0], semester: SysValue.Split(',')[1]);

            if (Class.SubjectChineseName.Contains("跨外校"))
            {
                TempData["Error"] = "跨校選課不得撰寫課程評論";
                return(RedirectToPage($"/Courses/Create/Select", new { SysValue = SysValue }));
            }
            else if (!kuasAp.CourseList.Any(course =>
                                            course.SubjectChineseName == Class.SubjectChineseName &&
                                            course.Teachers == Class.Teachers &&
                                            course.ClassShortName == Class.ClassShortName))
            {
                TempData["Error"] = $"學期中並未包含 { Class.SubjectChineseName } 課程";
                return(RedirectToPage($"/Courses/Create/Select", new { SysValue = SysValue }));
            }
            else
            {
                var classRepository = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                var @class          = (await classRepository.Select(@class: new Models.Class()
                {
                    SubjectChineseName = Class.SubjectChineseName,
                    Teachers = Class.Teachers,
                    ClassShortName = Class.ClassShortName
                })).FirstOrDefault();
                if (@class != null)
                {
                    return(RedirectToPage("/Courses/Create/Content", new { ClassGuid = @class.Guid }));
                }
                else
                {
                    Class.Guid = Guid.NewGuid().ToString();
                    if (await classRepository.Insert(@class: Class) > 0)
                    {
                        return(RedirectToPage("/Courses/Create/Content", new { ClassGuid = Class.Guid }));
                    }
                    else
                    {
                        TempData["Error"] = "課程加入失敗";
                        return(RedirectToPage($"/Courses/Create/Select", new { SysValue = SysValue }));
                    }
                }
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnGet()
        {
            // Repositorys
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);
            var comment           = (await commentRepository.Select(comment: new Models.Comment()
            {
                Guid = CommentGuid
            })).FirstOrDefault();

            if (comment == null)
            {
                TempData["Error"] = $"查無評論 { CommentGuid } 的課程資訊";
                return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
            }
            else
            {
                var student = JsonConvert.DeserializeObject <Models.Student>(
                    User.Claims.First(claim => claim.Type == "Information").Value);
                if (comment.Creator == student.Username)
                {
                    // Check comment's class info
                    var classRepository = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                    var classes         = (await classRepository.Select(@class: new Models.Class()
                    {
                        Guid = comment.ClassGuid
                    })).ToList();
                    if (classes.Count <= 0)
                    {
                        TempData["Error"] = $"查無評論 { CommentGuid } 的課程資訊";
                        return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
                    }
                    else
                    {
                        comment.SubjectChineseName = classes.First().SubjectChineseName;
                        comment.Teachers           = classes.First().Teachers;
                        comment.ClassShortName     = classes.First().ClassShortName;
                        Comment = comment;
                        return(Page());
                    }
                }
                else
                {
                    TempData["Error"] = "僅評論者本人可修改此評論";
                    return(RedirectToPage("/Courses/Details", new { CommentGuid = CommentGuid }));
                }
            }
        }
Exemplo n.º 6
0
        public async Task OnGetAsync()
        {
            // Message by redirect
            if (TempData.ContainsKey("Success"))
            {
                ModelState.AddModelError("Success", TempData["Success"].ToString());
            }
            if (TempData.ContainsKey("Error"))
            {
                ModelState.AddModelError("Error", TempData["Success"].ToString());
            }

            var student = JsonConvert.DeserializeObject <Models.Student>(
                User.Claims.First(claim => claim.Type == "Information").Value);
            var kuasAp = new Services.KUASAPService();

            // Repositorys
            var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

            Comments = (await commentRepository.Select(comment: new Models.Comment()
            {
                Creator = student.Username
            })).ToList();
            foreach (var comment in Comments)
            {
                var classes = (await classRepository.Select(@class: new Models.Class()
                {
                    Guid = comment.ClassGuid
                })).ToList();
                if (classes.Count > 0)
                {
                    comment.SubjectChineseName = classes.First().SubjectChineseName;
                    comment.Teachers           = classes.First().Teachers;
                    comment.ClassShortName     = classes.First().ClassShortName;
                }
            }

            // If empty, display alert
            if (Comments.Count <= 0)
            {
                ModelState.AddModelError("Warning", "尚未填寫任何學生教學評量");
            }
        }
Exemplo n.º 7
0
        public async Task OnGetAsync()
        {
            // 若搜尋條件為 null (直接點擊分析) 則預設為關鍵字搜尋
            bool paramsNull = new string[] { SubjectChineseName, Teachers, ClassShortName }.All(str => str == null);

            ByKeyword = paramsNull ? true : ByKeyword;

            // Repositorys
            var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

            // 查詢指定課程的相關評論
            Comments = new List <Models.Comment>();
            foreach (var @class in (await classRepository.Select(
                                        @class: new Models.Class()
            {
                SubjectChineseName = SubjectChineseName, Teachers = Teachers, ClassShortName = ClassShortName
            },
                                        sqlLike: ByKeyword)))
            {
                var comments = (await commentRepository.Select(comment: new Models.Comment()
                {
                    ClassGuid = @class.Guid
                })).ToList();
                comments.ForEach(comment =>
                {
                    comment.SubjectChineseName = @class.SubjectChineseName;
                    comment.Teachers           = @class.Teachers;
                    comment.ClassShortName     = @class.ClassShortName;
                });
                Comments.AddRange(comments);
            }
            // Sort
            Comments = Comments.OrderByDescending(comment => comment.RecordTime).ToList();
            // Set now username
            var student = JsonConvert.DeserializeObject <Models.Student>(
                User.Claims.First(claim => claim.Type == "Information").Value);

            // 若無回應物件
            if (Comments.Count == 0)
            {
                ModelState.AddModelError("Error", "無查詢結果");
            }
        }
Exemplo n.º 8
0
        public async Task OnGetAsync()
        {
            // Default parameters
            if (Pages <= 0)
            {
                Pages = 1;
            }
            if (string.IsNullOrEmpty(OrderBy))
            {
                OrderBy = "DESC";
            }

            // Repositorys
            var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

            // 取得所有課程之評論
            Comments = new List <Models.Comment>();
            foreach (var @class in (await classRepository.Select(@class: null)).ToList())
            {
                List <Models.Comment> classComments = (await commentRepository.Select(comment: new Models.Comment()
                {
                    ClassGuid = @class.Guid
                })).ToList();
                if (classComments.Count > 0)
                {
                    Comments.Add(new Models.Comment()
                    {
                        SubjectChineseName = @class.SubjectChineseName,
                        Teachers           = @class.Teachers,
                        ClassShortName     = @class.ClassShortName,
                        Grade  = classComments.Sum(comment => comment.Grade) / classComments.Count, /* Grade 表示平均評價 */
                        Remark = classComments.Sum(comment => comment.Grade).ToString("f2")         /* 借用 Remark 欄位來表示總評價 */
                    });
                }
            }

            // 正向排序或反向排序 && 排序欄位 (預設以 DESC 處理)
            Comments = OrderBy == "ASC" ?
                       Comments.OrderBy(comment => comment.Grade).ThenByDescending(comment => decimal.Parse(comment.Remark)).ThenBy(comment => comment.ClassShortName).ThenBy(comment => comment.Teachers).ThenBy(comment => comment.SubjectChineseName).ToList() :
                       Comments.OrderByDescending(comment => comment.Grade).ThenByDescending(comment => decimal.Parse(comment.Remark)).ThenBy(comment => comment.ClassShortName).ThenBy(comment => comment.Teachers).ThenBy(comment => comment.SubjectChineseName).ToList();

            // 表示每頁顯示課程數
            int coursePerPage = 12;

            // 表示最後頁碼
            LastPage = (int)Math.Ceiling((decimal)Comments.Count / coursePerPage);
            // 檢查頁面是否存在
            if (Pages < 1 || ((Pages - 1) * coursePerPage > (Comments.Count - 1)))
            {
                Comments = Comments.GetRange((Pages - 1) * coursePerPage, coursePerPage);
            }
            else
            {
                // 取得回傳的評論並加入至集合
                if (Comments.Count >= Pages * coursePerPage)
                {
                    Comments = Comments.GetRange((Pages - 1) * coursePerPage, coursePerPage);
                }
                else
                {
                    Comments = Comments.GetRange((Pages - 1) * coursePerPage, Comments.Count - (Pages - 1) * coursePerPage);
                }
            }
        }
Exemplo n.º 9
0
        public async Task OnGetAsync()
        {
            // Message by redirect
            if (TempData.ContainsKey("Error"))
            {
                ModelState.AddModelError("Error", TempData["Error"].ToString());
            }

            var student = JsonConvert.DeserializeObject <Models.Student>(
                User.Claims.First(claim => claim.Type == "Information").Value);
            var kuasAp = new Services.KUASAPService();

            // Set SysValue default value
            if (string.IsNullOrEmpty(SysValue))
            {
                SysValue = $"{ student.SysYear },{ student.SysSemester }";
            }

            // DropDownList items
            SysValues = new List <SelectListItem>();
            kuasAp.GetOptionValueList(student: student).ForEach(sysItem =>
                                                                SysValues.Add(new SelectListItem()
            {
                Text     = sysItem.SysText,
                Value    = $"{ sysItem.SysYear },{ sysItem.SysSemester }",
                Selected = string.IsNullOrEmpty(SysValue) ?
                           (sysItem.SysYear == student.SysYear && sysItem.SysSemester == student.SysSemester) :
                           (sysItem.SysYear == SysValue.Split(',')[0] && sysItem.SysSemester == SysValue.Split(',')[1])
            }));

            // Course list
            kuasAp.GetCourseListAndSchedule(
                student: student,
                year: SysValue.Split(',')[0],
                semester: SysValue.Split(',')[1]);
            CourseList = new List <Models.Class>(kuasAp.CourseList);
            Schedule   = new List <List <Models.Class> >(kuasAp.Schedule);

            // If empty, display alert
            if (CourseList.Count <= 0)
            {
                ModelState.AddModelError("Warning", $"查無 { SysValues.First(item => item.Selected).Text } 之課程資訊");
            }
            else
            {
                // 跨校選課不得撰寫評論
                CourseList.ForEach(course => course.DoneComment = course.ClassShortName.Contains("跨外校"));
                Schedule.ForEach(courses => courses.ForEach(course => course.DoneComment = string.IsNullOrEmpty(course.ClassShortName) ? course.DoneComment : course.ClassShortName.Contains("跨外校")));

                // 尋找是否存在已經寫過的評論
                // Repositorys
                var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);
                // // 搜尋使用者的評論
                var comments = (await commentRepository.Select(comment: new Models.Comment()
                {
                    Creator = student.Username
                })).ToList();
                // // 巡迴所有評論
                foreach (var comment in comments)
                {
                    // 取得評論之課程資料
                    Models.Class @class = (await classRepository.Select(@class: new Models.Class()
                    {
                        Guid = comment.ClassGuid
                    })).ToList().First();
                    // 選課清單
                    CourseList.ForEach(course =>
                                       course.DoneComment = course.DoneComment ||
                                                            (course.SubjectChineseName == @class.SubjectChineseName && course.Teachers == @class.Teachers && course.ClassShortName == @class.ClassShortName));
                    // 課表
                    Schedule.ForEach(courses => courses.ForEach(course =>
                                                                course.DoneComment = course.DoneComment ||
                                                                                     (course.SubjectChineseName == @class.SubjectChineseName && course.Teachers == @class.Teachers && course.ClassShortName == @class.ClassShortName)));
                }
            }
        }
Exemplo n.º 10
0
        public async Task OnGetAsync()
        {
            // User device type
            DeviceType = Request.Headers["User-Agent"].ToString().GetDeviceType();

            // 若搜尋條件為 null (直接點擊分析) 則預設為關鍵字搜尋
            bool paramsNull = new string[] { SubjectChineseName, ClassShortName, Teachers }.All(str => str == null);

            ByKeyword = paramsNull ? true : ByKeyword;

            // Repositorys
            var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
            var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

            // 所有相關評論
            var comments = new List <Models.Comment>();

            // 取得指定過濾條件的相關評論
            if (!paramsNull)
            {
                foreach (var @class in (await classRepository.Select(@class: new Models.Class()
                {
                    SubjectChineseName = SubjectChineseName, Teachers = Teachers, Classroom = ClassShortName
                }, sqlLike: ByKeyword)))
                {
                    comments.AddRange((await commentRepository.Select(comment: new Models.Comment()
                    {
                        ClassGuid = @class.Guid
                    })));
                }
            }
            else if (ByKeyword)
            {
                foreach (var @class in (await classRepository.Select(@class: null)))
                {
                    comments.AddRange((await commentRepository.Select(comment: new Models.Comment()
                    {
                        ClassGuid = @class.Guid
                    })));
                }
            }

            // 若無回應物件
            if (comments.Count == 0)
            {
                Overview = new Models.Overview()
                {
                    ClassInfo       = new Models.Class(),
                    CommentCount    = 0m,
                    TeachingMethods = new Models.TeachingMethods(),
                    RollCallMethods = new Models.RollCallMethods(),
                    ClassworkExam   = new Models.ClassworkExam(),
                    Grades          = new Models.Grades()
                };
                ModelState.AddModelError("Error", "無查詢結果");
            }
            else
            {
                Overview = new Models.Overview()
                {
                    ClassInfo = new Models.Class()
                    {
                        SubjectChineseName = SubjectChineseName,
                        Teachers           = Teachers,
                        ClassShortName     = ClassShortName,
                    },
                    CommentCount = comments.Count,
                    /* 以下百分比小數取到第二位 (Math.Floor(X * 10000) * 100) */
                    TeachingMethods = new Models.TeachingMethods()
                    {
                        IsBlackboard        = comments.Count(comment => comment.IsBlackboard),
                        IsBlackboardPercent = Math.Floor((decimal)comments.Count(comment => comment.IsBlackboard) / comments.Count * 10000) / 100,
                        IsBook               = comments.Count(comment => comment.IsBook),
                        IsBookPercent        = Math.Floor((decimal)comments.Count(comment => comment.IsBook) / comments.Count * 10000) / 100,
                        IsPPT                = comments.Count(comment => comment.IsPPT),
                        IsPPTPercent         = Math.Floor((decimal)comments.Count(comment => comment.IsPPT) / comments.Count * 10000) / 100,
                        IsBroadcast          = comments.Count(comment => comment.IsBroadcast),
                        IsBroadcastPercent   = Math.Floor((decimal)comments.Count(comment => comment.IsBroadcast) / comments.Count * 10000) / 100,
                        IsBuild              = comments.Count(comment => comment.IsBuild),
                        IsBuildPercent       = Math.Floor((decimal)comments.Count(comment => comment.IsBuild) / comments.Count * 10000) / 100,
                        IsInteractive        = comments.Count(comment => comment.IsInteractive),
                        IsInteractivePercent = Math.Floor((decimal)comments.Count(comment => comment.IsInteractive) / comments.Count * 10000) / 100
                    },
                    RollCallMethods = new Models.RollCallMethods()
                    {
                        RollCallFrequency        = comments.Sum(comment => comment.RollCallFrequency) / comments.Count,
                        RollCallFrequencyPercent = Math.Floor(comments.Sum(comment => comment.RollCallFrequency) / (comments.Count * 5) * 100 + (decimal)0.5),
                        ByInPerson           = comments.Count(comment => comment.ByInPerson),
                        ByInPersonPercent    = Math.Floor((decimal)comments.Count(comment => comment.ByInPerson) / comments.Count * 10000) / 100,
                        BySignInSheet        = comments.Count(comment => comment.BySignInSheet),
                        BySignInSheetPercent = Math.Floor((decimal)comments.Count(comment => comment.BySignInSheet) / comments.Count * 10000) / 100,
                        ByOnline             = comments.Count(comment => comment.ByOnline),
                        ByOnlinePercent      = Math.Floor((decimal)comments.Count(comment => comment.ByOnline) / comments.Count * 10000) / 100,
                        ByClasswork          = comments.Count(comment => comment.ByClasswork),
                        ByClassworkPercent   = Math.Floor((decimal)comments.Count(comment => comment.ByClasswork) / comments.Count * 10000) / 100,
                        ByTest        = comments.Count(comment => comment.ByTest),
                        ByTestPercent = Math.Floor((decimal)comments.Count(comment => comment.ByTest) / comments.Count * 10000) / 100
                    },
                    ClassworkExam = new Models.ClassworkExam()
                    {
                        HaveClasswork          = comments.Count(comment => comment.HaveClasswork),
                        HaveClassworkPercent   = Math.Floor((decimal)comments.Count(comment => comment.HaveClasswork) / comments.Count * 10000) / 100,
                        HaveTest               = comments.Count(comment => comment.HaveTest),
                        HaveTestPercent        = Math.Floor((decimal)comments.Count(comment => comment.HaveTest) / comments.Count * 10000) / 100,
                        HaveMidtermExam        = comments.Count(comment => comment.HaveMidtermExam),
                        HaveMidtermExamPercent = Math.Floor((decimal)comments.Count(comment => comment.HaveMidtermExam) / comments.Count * 10000) / 100,
                        HaveFinalExam          = comments.Count(comment => comment.HaveFinalExam),
                        HaveFinalExamPercent   = Math.Floor((decimal)comments.Count(comment => comment.HaveFinalExam) / comments.Count * 10000) / 100
                    },
                    Grades = new Models.Grades()
                    {
                        Grade1        = comments.Count(comment => comment.Grade == 1),
                        Grade1Percent = Math.Floor((decimal)comments.Count(comment => comment.Grade == 1) / (comments.Count == 0 ? 1 : comments.Count) * 10000) / 100,
                        Grade2        = comments.Count(comment => comment.Grade == 2),
                        Grade2Percent = Math.Floor((decimal)comments.Count(comment => comment.Grade == 2) / (comments.Count == 0 ? 1 : comments.Count) * 10000) / 100,
                        Grade3        = comments.Count(comment => comment.Grade == 3),
                        Grade3Percent = Math.Floor((decimal)comments.Count(comment => comment.Grade == 3) / (comments.Count == 0 ? 1 : comments.Count) * 10000) / 100,
                        Grade4        = comments.Count(comment => comment.Grade == 4),
                        Grade4Percent = Math.Floor((decimal)comments.Count(comment => comment.Grade == 4) / (comments.Count == 0 ? 1 : comments.Count) * 10000) / 100,
                        Grade5        = comments.Count(comment => comment.Grade == 5),
                        Grade5Percent = Math.Floor((decimal)comments.Count(comment => comment.Grade == 5) / (comments.Count == 0 ? 1 : comments.Count) * 10000) / 100,
                        GradeAverage  = comments.Sum(comment => comment.Grade) / comments.Count
                    }
                };
            }
        }
Exemplo n.º 11
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            else
            {
                var student = JsonConvert.DeserializeObject <Models.Student>(
                    User.Claims.First(claim => claim.Type == "Information").Value);
                var kuasAp = new Services.KUASAPService();

                // Repositorys
                var classRepository   = new Repositorys.ClassRepository(appSettings.ConnectionStrings.DefaultConnection);
                var commentRepository = new Repositorys.CommentRepository(appSettings.ConnectionStrings.DefaultConnection);

                // Check Guid
                var classes = (await classRepository.Select(@class: new Models.Class()
                {
                    Guid = ClassGuid
                })).ToList();
                if (classes.Count <= 0)
                {
                    ModelState.AddModelError("Error", "查無此課程資料,請依照流程填寫");
                    return(Page());
                }
                else if (classes.First().SubjectChineseName != Comment.SubjectChineseName ||
                         classes.First().Teachers != Comment.Teachers ||
                         classes.First().ClassShortName != Comment.ClassShortName)
                {
                    ModelState.AddModelError("Error", "不符合的課程資訊,請依照流程填寫");
                    return(Page());
                }
                else
                {
                    if ((await commentRepository.Select(comment: new Models.Comment()
                    {
                        ClassGuid = ClassGuid
                    }))
                        .Any(comment => comment.Creator == student.Username))
                    {
                        ModelState.AddModelError("Error", "已填寫過此課程之評論");
                        return(Page());
                    }
                    else
                    {
                        Comment.ClassGuid  = ClassGuid;
                        Comment.Remark     = string.IsNullOrEmpty(Comment.Remark) ? "" : (Comment.Remark);
                        Comment.Creator    = student.Username;
                        Comment.RecordTime = DateTime.Now;
                        Comment.Guid       = Guid.NewGuid().ToString();
                        if (await commentRepository.Insert(comment: Comment) > 0)
                        {
                            TempData["Success"] = "撰寫評論成功";
                            return(RedirectToPage("/Courses/Details", new { CommentGuid = Comment.Guid }));
                        }
                        else
                        {
                            ModelState.AddModelError("Error", "撰寫評論失敗");
                            return(Page());
                        }
                    }
                }
            }
        }