public IActionResult GetStudentSubject(string studentId)
        {
            CheckValid valid = new CheckValid();

            try
            {
                if (!valid.IsExistedStudentId(studentId))
                {
                    valid.IsValid = false;
                }
                if (valid.IsValid)
                {
                    var listClassSubject = GetClassSubjectByStudentId(studentId);

                    if (listClassSubject.Count > 0)
                    {
                        return(Ok(new BaseResponse(listClassSubject, "Get list class subject success", true)));
                    }
                    else
                    {
                        return(NotFound(new BaseResponse(listClassSubject, "Student havent study anything", false)));
                    }
                }

                return(NotFound(new BaseResponse(null, valid.ErrorMessage, false)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BaseResponse(null, e.Message, false)));
            }
        }
        public IActionResult CreateTicket(string studentId, string teacherId, string content)
        {
            CheckValid valid = new CheckValid();

            try
            {
                if (!valid.IsExistedTeacherId(teacherId))
                {
                    valid.IsValid = false;
                }
                if (!valid.IsExistedStudentId(studentId))
                {
                    valid.IsValid = false;
                }

                if (valid.IsValid)
                {
                    var context = new FaceIOContext();
                    var student = context.Users.Where(q => q.Username == studentId).FirstOrDefault();
                    var teacher = context.Teacher.Where(t => t.Id == teacherId).FirstOrDefault();

                    if (student == null || teacher == null || string.IsNullOrEmpty(content))
                    {
                        return(NotFound(new BaseResponse(null, "Teacher or student not found or content is blank", false)));
                    }
                    else
                    {
                        context.Ticket.Add(new Ticket()
                        {
                            Content   = content,
                            StartDate = DateTime.Now,
                            EndDate   = DateTime.Now.AddDays(10),
                            Status    = "Open"
                        });


                        context.SaveChanges();

                        context.StudentTeacherTicket.Add(new StudentTeacherTicket()
                        {
                            StudentId = studentId,
                            TeacherId = teacherId,
                            TicketId  = context.Ticket.Where(t => t.Content == content).FirstOrDefault().Id
                        });

                        context.SaveChanges();
                    }

                    return(Ok(new BaseResponse(null, "Create ticket success", true)));
                }

                return(NotFound(new BaseResponse(null, valid.ErrorMessage, false)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BaseResponse(null, e.Message, false)));
            }
        }
        public IActionResult GetTicketByStudentId(string studentId)
        {
            CheckValid valid = new CheckValid();

            try
            {
                if (!valid.IsExistedStudentId(studentId))
                {
                    valid.IsValid = false;
                }

                if (valid.IsValid)
                {
                    var context = new FaceIOContext();


                    var listIdTicket = context.StudentTeacherTicket.Where(x => x.StudentId == studentId).Select(x => x.TicketId)
                                       .ToList();

                    var tickets = context.Ticket.Where(x => listIdTicket.Contains(x.Id)).ToList();

                    var result = new List <Object>();

                    foreach (var ticket in tickets)
                    {
                        result.Add(new
                        {
                            id        = ticket.Id,
                            content   = ticket.Content,
                            startDate = ticket.StartDate,
                            endDate   = ticket.EndDate,
                            status    = ticket.Status,
                            teacherId = context.StudentTeacherTicket.Where(x => x.TicketId == ticket.Id).FirstOrDefault()
                                        .TeacherId
                        });
                    }

                    return(Ok(new BaseResponse(result, "Success", true)));
                }

                return(NotFound(new BaseResponse(null, valid.ErrorMessage, false)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BaseResponse(null, e.Message, false)));
            }
        }
        public IActionResult GetScheduleOnWeek(string studentId, DateTime date)
        {
            CheckValid valid = new CheckValid();

            try
            {
                if (!valid.IsExistedStudentId(studentId))
                {
                    valid.IsValid = false;
                }
                if (valid.StringIsNullOrEmpty(date.ToString()))
                {
                    valid.IsValid = false;
                }

                if (valid.IsValid)
                {
                    var listClassSubject = GetClassSubjectByStudentId(studentId);

                    if (listClassSubject != null)
                    {
                        Dictionary <string, Object> dicClassSubjectSchedule = new Dictionary <string, object>();

                        var mondayInWeek = date.StartOfWeek(DayOfWeek.Monday);

                        var sundayInWeek = mondayInWeek.AddDays(6.0);

                        foreach (var classSubject in listClassSubject)
                        {
                            var ds = GetScheduleByClassSubjectId(classSubject.Id);

                            var scheduleOfClassSubject = GetScheduleByClassSubjectId(classSubject.Id).Where(x => x.Date >= mondayInWeek && x.Date <= sundayInWeek).ToList();

                            foreach (var schedule in scheduleOfClassSubject)
                            {
                                string s = $"{schedule.Date.DayOfWeek.ToString()}/{schedule.Slot}";

                                int a = schedule.ClassSubjectSchedule.ToList()[0].Id;

                                var attendance = GetAttendance(studentId, schedule.ClassSubjectSchedule.ToList()[0].Id,
                                                               classSubject.Id);

                                dicClassSubjectSchedule.Add(s, new
                                {
                                    subject = classSubject.SubjectId,
                                    classId = classSubject.ClassId,
                                    atten   = attendance.Attendance
                                });
                            }
                        }

                        if (dicClassSubjectSchedule.Count > 0)
                        {
                            return(Ok(new BaseResponse(dicClassSubjectSchedule, "Success with schedule", true)));
                        }

                        return(Ok(new BaseResponse(dicClassSubjectSchedule, "Success but not have any schedule on this week", true)));
                    }

                    return(NotFound(new BaseResponse(null, "Student is not study any subject", false)));
                }

                return(NotFound(new BaseResponse(null, valid.ErrorMessage, false)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BaseResponse(null, e.Message, false)));
            }
        }
        public IActionResult UpdateAttendance(string studentId, bool attendance)
        {
            CheckValid valid       = new CheckValid();
            var        timeRequest = DateTime.Now;
            var        slotJoin    = getSlot(timeRequest);
            var        check       = false;

            try
            {
                if (!valid.IsExistedStudentId(studentId))
                {
                    valid.IsValid = false;
                }
                if (valid.StringIsNullOrEmpty(attendance.ToString()))
                {
                    valid.IsValid = false;
                }

                if (valid.IsValid)
                {
                    var listClassSubject = GetClassSubjectByStudentId(studentId);

                    if (listClassSubject.Count > 0)
                    {
                        foreach (var classSubject in listClassSubject)
                        {
                            var ListSchedule = GetScheduleByClassSubjectId(classSubject.Id).Where(x => x.Date == timeRequest.Date);
                            foreach (var schedule in ListSchedule)
                            {
                                if (schedule.Slot == slotJoin)
                                {
                                    var context = new FaceIOContext();
                                    var classSubjectSchedule = context.ClassSubjectSchedule.Where(x =>
                                                                                                  x.ClassSubjectId == classSubject.Id && x.ScheduleId == schedule.Id).FirstOrDefault();

                                    var studentStudyAttendance =
                                        context.StudentStudyAttendance.Where(x =>
                                                                             x.ClassSubjectScheduleId == classSubjectSchedule.Id).FirstOrDefault();

                                    if (studentStudyAttendance != null)
                                    {
                                        studentStudyAttendance.Attendance = true;

                                        context.StudentStudyAttendance.Update(studentStudyAttendance);

                                        context.SaveChanges();

                                        check = true;
                                        break;
                                    }
                                }

                                else
                                {
                                    continue;
                                }
                            }

                            if (check)
                            {
                                break;
                            }
                        }

                        if (check == false)
                        {
                            return(NotFound(new BaseResponse(null, "Attendance failed", false)));
                        }
                        else
                        {
                            return(Ok(new BaseResponse(null, "Update attendance success", true)));
                        }
                    }

                    return(NotFound(new BaseResponse(null, "Not found any subject by student ID", false)));
                }

                return(NotFound(new BaseResponse(null, valid.ErrorMessage, false)));
            }
            catch (Exception e)
            {
                return(BadRequest(new BaseResponse(null, e.Message, false)));
            }
        }