Пример #1
0
        public object modify([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <AttitudeSetup>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(modify), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var attitude = _context.AttitudeSetups.Where(w => w.AttitudeAnsType == model.AttitudeAnsType & w.AttitudeAnsSubType == model.AttitudeAnsSubType).FirstOrDefault();

            if (attitude == null)
            {
                model.Create_On = DateUtil.Now();
                model.Update_On = DateUtil.Now();
                model.Create_By = model.Update_By;
                _context.AttitudeSetups.Add(model);
                _context.SaveChanges();
            }
            else
            {
                model.Update_On = DateUtil.Now();
                model.ID        = attitude.ID;
                _context.Entry(attitude).CurrentValues.SetValues(model);
                _context.SaveChanges();
            }

            return(CreatedAtAction(nameof(modify), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
Пример #2
0
        public object uploadimage([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <ImageDTO>(json.GetRawText());

            if (!string.IsNullOrEmpty(model.image))
            {
                var imgfile = new ImageFile();
                imgfile.Create_On = DateUtil.Now();
                imgfile.Update_On = DateUtil.Now();
                imgfile.Create_By = model.update_by;
                imgfile.Update_By = model.update_by;
                _context.ImageFiles.Add(imgfile);
                _context.SaveChanges();

                var str = model.image.Replace("data:image/jpeg;base64,", "");
                str = str.Replace("data:image/png;base64,", "");
                byte[] bytes = Convert.FromBase64String(str);


                var filePath = Directory.GetCurrentDirectory() + "\\wwwroot\\images\\" + imgfile.ID + ".png";
                var filename = filePath;
                filePath = filePath.Replace("\\", "/");

                Image img;
                var   memory = new MemoryStream();
                using (var ms = new MemoryStream(bytes))
                {
                    img = Image.FromStream(ms);
                    img.Save(filePath, ImageFormat.Png);
                }
                return(CreatedAtAction(nameof(uploadimage), new { result = ResultCode.Success, message = ResultMessage.Success, id = imgfile.ID }));
            }

            return(CreatedAtAction(nameof(uploadimage), new { result = ResultCode.DataHasNotFound, message = ResultMessage.DataHasNotFound }));
        }
Пример #3
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <SubjectGroup>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var group = _context.SubjectGroups.Where(w => w.Name == model.Name).FirstOrDefault();

            if (group != null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.DuplicateData, message = ResultMessage.DuplicateData }));
            }

            group             = new SubjectGroup();
            group.Color1      = model.Color1;
            group.Color2      = model.Color2;
            group.Color3      = model.Color3;
            group.Create_On   = DateUtil.Now();
            group.Update_On   = DateUtil.Now();
            group.Create_By   = model.Update_By;
            group.Update_By   = model.Update_By;
            group.Status      = model.Status;
            group.Name        = model.Name;
            group.DoExamOrder = model.DoExamOrder;

            _context.SubjectGroups.Add(group);
            _context.SaveChanges();
            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
Пример #4
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <SubjectSub>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var sub = _context.SubjectSubs.Where(w => w.Name == model.Name & w.SubjectID == model.SubjectID).FirstOrDefault();

            if (sub != null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.DuplicateData, message = ResultMessage.DuplicateData }));
            }

            sub             = new SubjectSub();
            sub.Create_On   = DateUtil.Now();
            sub.Update_On   = DateUtil.Now();
            sub.Create_By   = model.Update_By;
            sub.Update_By   = model.Update_By;
            sub.Status      = model.Status;
            sub.Name        = model.Name;
            sub.Description = model.Description;
            sub.SubjectID   = model.SubjectID;

            _context.SubjectSubs.Add(sub);
            _context.SaveChanges();
            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
        public object listexamsetup()
        {
            var examperiods = new List <ExamPeriod>();

            examperiods.Add(ExamPeriod.Morning);
            examperiods.Add(ExamPeriod.Afternoon);
            examperiods.Add(ExamPeriod.Evening);
            var subjectuse = new List <int>();

            foreach (var group in _context.SubjectGroups.Where(w => w.Status == StatusType.Active).OrderBy(o => o.ID))
            {
                foreach (var subject in _context.Subjects.Where(w => w.Status == StatusType.Active & w.SubjectGroupID == group.ID).OrderBy(o => o.Order))
                {
                    subjectuse.Add(subject.ID);
                    foreach (var period in examperiods)
                    {
                        var setup = _context.ExamSetups.Where(w => w.SubjectGroupID == group.ID & w.SubjectID == subject.ID & w.ExamPeriod == period).FirstOrDefault();
                        if (setup == null)
                        {
                            setup                = new ExamSetup();
                            setup.ExamPeriod     = period;
                            setup.SubjectGroupID = group.ID;
                            setup.SubjectID      = subject.ID;
                            setup.choosed        = false;
                            setup.Create_On      = DateUtil.Now();
                            setup.Update_On      = DateUtil.Now();
                            _context.ExamSetups.Add(setup);
                        }
                    }
                }
            }
            _context.SaveChanges();

            var unuse = _context.ExamSetups.Where(w => !subjectuse.Contains(w.SubjectID));

            if (unuse.Count() > 0)
            {
                _context.ExamSetups.RemoveRange(unuse);
            }
            _context.SaveChanges();

            return(_context.ExamSetups.Select(s => new
            {
                id = s.ID,
                examperiod = s.ExamPeriod,
                examperiodname = s.ExamPeriod.toExamPeriodName(),
                choosed = s.choosed,
                group = s.SubjectGroup.Name,
                subject = s.Subject.Name,
                subjectorder = s.Subject.Order,
                create_on = DateUtil.ToDisplayDateTime(s.Create_On),
                create_by = s.Create_By,
                update_on = DateUtil.ToDisplayDateTime(s.Update_On),
                update_by = s.Update_By,
            }).OrderBy(o => o.examperiod).ThenBy(o => o.group).ThenBy(o => o.subjectorder).ToArray());
        }
Пример #6
0
        public object listsendresultsetup()
        {
            var subjectuse = new List <int>();

            foreach (var group in _context.SubjectGroups.Where(w => w.Status == StatusType.Active).OrderBy(o => o.ID))
            {
                foreach (var subject in _context.Subjects.Where(w => w.Status == StatusType.Active & w.SubjectGroupID == group.ID).OrderBy(o => o.Order))
                {
                    subjectuse.Add(subject.ID);

                    var setup = _context.SendResultSetups.Where(w => w.SubjectGroupID == group.ID & w.SubjectID == subject.ID).FirstOrDefault();
                    if (setup == null)
                    {
                        setup = new SendResultSetup();
                        setup.SubjectGroupID = group.ID;
                        setup.SubjectID      = subject.ID;
                        setup.Other          = true;
                        setup.SendByEmail    = true;
                        setup.SendByPost     = true;
                        setup.Create_On      = DateUtil.Now();
                        setup.Update_On      = DateUtil.Now();
                        _context.SendResultSetups.Add(setup);
                    }
                }
            }
            _context.SaveChanges();

            var unuse = _context.SendResultSetups.Where(w => !subjectuse.Contains(w.SubjectID));

            if (unuse.Count() > 0)
            {
                _context.SendResultSetups.RemoveRange(unuse);
            }
            _context.SaveChanges();

            return(_context.SendResultSetups.Select(s => new
            {
                id = s.ID,
                group = s.SubjectGroup.Name,
                groupid = s.SubjectGroupID,
                subject = s.Subject.Name,
                order = s.Subject.Order,
                subjectid = s.SubjectID,
                other = s.Other,
                sendbyemail = s.SendByEmail,
                sendbypost = s.SendByPost,
                description = s.Description,
                create_on = DateUtil.ToDisplayDateTime(s.Create_On),
                create_by = s.Create_By,
                update_on = DateUtil.ToDisplayDateTime(s.Update_On),
                update_by = s.Update_By,
            }).OrderBy(o => o.group).ThenBy(o => o.order).ToArray());
        }
Пример #7
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <Subject>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var subject = _context.Subjects.Where(w => w.Name == model.Name & w.SubjectGroupID == model.SubjectGroupID).FirstOrDefault();

            if (subject != null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.DuplicateData, message = ResultMessage.DuplicateData }));
            }

            subject                = new Subject();
            subject.Create_On      = DateUtil.Now();
            subject.Update_On      = DateUtil.Now();
            subject.Create_By      = model.Update_By;
            subject.Update_By      = model.Update_By;
            subject.Status         = model.Status;
            subject.Name           = model.Name;
            subject.Description    = model.Description;
            subject.SubjectGroupID = model.SubjectGroupID;
            subject.Order          = model.Order;

            _context.Subjects.Add(subject);
            _context.SaveChanges();

            foreach (var group in _context.SubjectGroups)
            {
                var order = 1;
                foreach (var s in _context.Subjects.Where(w => w.SubjectGroupID == group.ID).OrderBy(o => o.Order))
                {
                    s.Order = order;
                    order++;
                }
            }
            _context.SaveChanges();
            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
Пример #8
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <UserDTO>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var dupid = _context.Students.Where(w => w.IDCard == model.idcard).FirstOrDefault();

            if (dupid != null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.DuplicateData, message = "เลขประจำตัวประชาชนซ้ำ" }));
            }

            if (!string.IsNullOrEmpty(model.studentcode))
            {
                var dupscode = _context.Students.Where(w => w.StudentCode == model.studentcode).FirstOrDefault();
                if (dupscode != null)
                {
                    return(CreatedAtAction(nameof(insert), new { result = ResultCode.DuplicateData, message = "รหัสนักศึกษาซ้ำ" }));
                }
            }

            var users = _context.Users.Count() + 1;

            model.username = model.idcard;

            var u = new User();

            u.UserName        = model.username;
            u.Password        = DataEncryptor.Encrypt(model.idcard);
            u.ConfirmPassword = DataEncryptor.Encrypt(model.idcard);
            u.Create_On       = DateUtil.Now();
            u.Create_By       = model.update_by;
            u.Update_On       = DateUtil.Now();
            u.Update_By       = model.update_by;

            var student = new Student();

            student.FirstName   = model.firstname;
            student.LastName    = model.lastname;
            student.FirstNameEn = model.firstnameen;
            student.LastNameEn  = model.lastnameen;
            student.Prefix      = model.prefix.toPrefix();
            student.Address     = model.address;
            student.Email       = model.email;
            student.Phone       = model.phone;
            student.Passport    = model.passport;
            student.IDCard      = model.idcard;
            student.StudentCode = model.studentcode;
            student.FacultyID   = model.faculty;
            student.Course      = model.course.toCourse();
            student.Status      = model.status.toStatus();
            student.Create_On   = DateUtil.Now();
            student.Create_By   = model.update_by;
            student.Update_On   = DateUtil.Now();
            student.Update_By   = model.update_by;
            student.User        = u;

            _context.Students.Add(student);
            _context.SaveChanges();
            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
Пример #9
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <Test>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            model.Create_On = DateUtil.Now();
            model.Update_On = DateUtil.Now();
            model.Create_By = model.Update_By;
            model.Update_By = model.Update_By;

            _context.Tests.Add(model);
            _context.SaveChanges();

            var code = "T" + model.ID.ToString("00000000");

            model.TestCode = code;
            _context.SaveChanges();

            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success, id = model.ID, groupid = model.SubjectGroupID, subjectid = model.SubjectID }));
        }
Пример #10
0
        public object insert([FromBody] JsonElement json)
        {
            var model = JsonConvert.DeserializeObject <UserDTO>(json.GetRawText());

            if (model == null)
            {
                return(CreatedAtAction(nameof(insert), new { result = ResultCode.InputHasNotFound, message = ResultMessage.InputHasNotFound }));
            }

            var users    = _context.Users.Count() + 1;
            var username = "******" + users.ToString("0000");

            model.username = username;

            var u = new User();

            u.UserName        = model.username;
            u.Password        = DataEncryptor.Encrypt(model.username);
            u.ConfirmPassword = DataEncryptor.Encrypt(model.username);
            u.Create_On       = DateUtil.Now();
            u.Create_By       = model.update_by;
            u.Update_On       = DateUtil.Now();
            u.Update_By       = model.update_by;
            u.isAdmin         = true;

            var staff = new Staff();

            staff.FirstName            = model.firstname;
            staff.LastName             = model.lastname;
            staff.Prefix               = model.prefix.toPrefix();
            staff.Address              = model.address;
            staff.Email                = model.email;
            staff.Phone                = model.phone;
            staff.Phone2               = model.phone2;
            staff.Passport             = model.passport;
            staff.IDCard               = model.idcard;
            staff.OpenDate             = DateUtil.ToDate(model.opendate);
            staff.ExpiryDate           = DateUtil.ToDate(model.expirydate);
            staff.Status               = model.status.toStatus();
            staff.Create_On            = DateUtil.Now();
            staff.Create_By            = model.update_by;
            staff.Update_On            = DateUtil.Now();
            staff.Update_By            = model.update_by;
            staff.isAdmin              = model.isadmin.HasValue ? model.isadmin.Value : false;
            staff.isMasterAdmin        = model.ismasteradmin.HasValue ? model.ismasteradmin.Value : false;
            staff.isQuestionAppr       = model.isquestionappr.HasValue ? model.isquestionappr.Value : false;
            staff.isMasterQuestionAppr = model.ismasterquestionappr.HasValue ? model.ismasterquestionappr.Value : false;
            staff.isTestAppr           = model.istestappr.HasValue ? model.istestappr.Value : false;
            staff.isMasterTestAppr     = model.ismastertestappr.HasValue ? model.ismastertestappr.Value : false;
            staff.User = u;

            _context.Staffs.Add(staff);
            _context.SaveChanges();

            username   = "******" + u.ID.ToString("0000");
            u.UserName = username;
            u.Password = DataEncryptor.Encrypt(u.UserName);
            _context.SaveChanges();

            return(CreatedAtAction(nameof(insert), new { result = ResultCode.Success, message = ResultMessage.Success }));
        }
Пример #11
0
        public object listAllexam(string text_search, string status_search, string group_search, string subject_search, string from_search, string to_search, int?move_from, int pageno = 1)
        {
            /*repair test student exam*/
            var curdate    = DateUtil.Now().Date;
            var rtstudents = _context.TestResultStudents.Include(i => i.Test).Where(w => w.ExamingStatus == ExamingStatus.Examing);

            foreach (var rtstudent in rtstudents)
            {
                if (rtstudent.Test != null)
                {
                    var remain = FuncUtil.GetTimeRemaining(rtstudent.Test.TimeLimitType, rtstudent.Test.TimeLimit, rtstudent.Start_On);
                    if (remain <= 0)
                    {
                        rtstudent.ExamingStatus = ExamingStatus.Done;
                        rtstudent.Other         = true;
                        rtstudent.Update_On     = curdate;
                        rtstudent.End_On        = rtstudent.Start_On.Value.AddSeconds(FuncUtil.GetMaxTimeLimit(rtstudent.Test.TimeLimitType, rtstudent.Test.TimeLimit));
                    }
                }
            }
            _context.SaveChanges();

            var exam = _context.Exams.Include(i => i.Test).Include(i => i.Subject).Include(i => i.Subject.SubjectGroup).Where(w => 1 == 1);

            if (move_from.HasValue)
            {
                exam = exam.Where(w => w.ID != move_from & w.ExamDate.Value >= curdate.Date);
            }

            if (!string.IsNullOrEmpty(status_search))
            {
                exam = exam.Where(w => w.Status == status_search.toStatus());
            }

            if (!string.IsNullOrEmpty(subject_search))
            {
                var subjectID = NumUtil.ParseInteger(subject_search);
                if (subjectID > 0)
                {
                    exam = exam.Where(w => w.SubjectID == subjectID);
                }
            }
            if (!string.IsNullOrEmpty(group_search))
            {
                var groupID = NumUtil.ParseInteger(group_search);
                if (groupID > 0)
                {
                    exam = exam.Where(w => w.Subject.SubjectGroupID == groupID);
                }
            }
            if (!string.IsNullOrEmpty(from_search))
            {
                var date = DateUtil.ToDate(from_search);
                exam = exam.Where(w => w.ExamDate.Value.Date >= date);
            }
            if (!string.IsNullOrEmpty(to_search))
            {
                var date = DateUtil.ToDate(to_search);
                exam = exam.Where(w => w.ExamDate.Value.Date <= date);
            }

            var exams = new List <Exam>();

            if (!string.IsNullOrEmpty(text_search))
            {
                var text_splits = text_search.Split(",", StringSplitOptions.RemoveEmptyEntries);
                foreach (var text_split in text_splits)
                {
                    if (!string.IsNullOrEmpty(text_split))
                    {
                        var text = text_split.Trim();

                        var registerIDs = _context.ExamRegisters.Where(w => w.Student.FirstName.Contains(text)
                                                                       | w.Student.LastName.Contains(text)
                                                                       | w.Student.FirstNameEn.Contains(text)
                                                                       | w.Student.LastNameEn.Contains(text)
                                                                       | w.Student.IDCard.Contains(text)
                                                                       | w.Student.Phone.Contains(text)
                                                                       | w.Student.Email.Contains(text)
                                                                       | w.Student.Passport.Contains(text)
                                                                       | w.Student.StudentCode.Contains(text)
                                                                       | (w.Student.FirstName + " " + w.Student.LastName).Contains(text)
                                                                       | (w.Student.FirstNameEn + " " + w.Student.LastNameEn).Contains(text)
                                                                       ).Select(s => s.ExamID);

                        exams.AddRange(exam.Where(w => w.Test.Name.Contains(text)
                                                  | w.ExamCode.Contains(text)
                                                  | registerIDs.Contains(w.ID)
                                                  ));
                    }
                }
                exams = exams.Distinct().ToList();
            }
            else
            {
                exams = exam.ToList();
            }

            int skipRows = (pageno - 1) * 25;
            var itemcnt  = exams.Count();
            var pagelen  = itemcnt / 25;

            if (itemcnt % 25 > 0)
            {
                pagelen += 1;
            }
            return(CreatedAtAction(nameof(listAllexam), new
            {
                data = exams.Select(s => new
                {
                    id = s.ID,
                    test = s.ExamTestType == ExamTestType.Random ? _context.Tests.Where(w => w.SubjectID == s.SubjectID & w.Status == StatusType.Active).Count() == 1 ? _context.Tests.Where(w => w.SubjectID == s.SubjectID & w.Status == StatusType.Active).FirstOrDefault().Name :  "สุ่ม" : s.Test.Name,
                    status = s.Status.toStatusName(),
                    group = s.Subject.SubjectGroup.Name,
                    subject = s.Subject.Name,
                    subjectorder = s.Subject.Order,
                    examcode = s.ExamCode,
                    examdate = DateUtil.ToDisplayDate(s.ExamDate),
                    date = s.ExamDate,
                    examperiod = s.ExamPeriod.toExamPeriodName(),
                    examperiodid = s.ExamPeriod,
                    examtesttype = s.ExamTestType.toExamTestType(),
                    registercnt = s.RegisterCnt,
                    examregistercnt = s.ExamRegisterCnt,
                    create_on = DateUtil.ToDisplayDateTime(s.Create_On),
                    create_by = s.Create_By,
                    update_on = DateUtil.ToDisplayDateTime(s.Update_On),
                    update_by = s.Update_By,
                }).OrderByDescending(o => o.date).ThenBy(o => o.examperiodid).ThenBy(o => o.group).ThenBy(o => o.subjectorder).Skip(skipRows).Take(25).ToArray(),
                pagelen = pagelen,
                itemcnt = itemcnt,
            }));;
        }
Пример #12
0
        public object login(string username, string password)
        {
            var user = _context.Users.Where(w => w.UserName == username).FirstOrDefault();

            if (user == null)
            {
                return(CreatedAtAction(nameof(login), new { result = ResultCode.WrongAccountorPassword, message = ResultMessage.WrongAccountorPassword }));
            }

            var dpassword = DataEncryptor.Decrypt(user.Password);

            if (password == dpassword)
            {
                var token = CreateToken(user);
                var staff = _context.Staffs.Where(w => w.UserID == user.ID);

                if (staff.FirstOrDefault() == null)
                {
                    return(CreatedAtAction(nameof(login), new { result = ResultCode.DataHasNotFound, message = ResultMessage.DataHasNotFound }));
                }

                if (staff.FirstOrDefault().Status == StatusType.InActive)
                {
                    return(CreatedAtAction(nameof(login), new { result = ResultCode.InactiveAccount, message = ResultMessage.InactiveAccount }));
                }

                var s = staff.Select(s => new
                {
                    username             = s.User.UserName,
                    id                   = s.UserID,
                    staffid              = s.ID,
                    firstname            = s.FirstName,
                    lastname             = s.LastName,
                    profileImg           = "",
                    isAdmin              = s.isAdmin,
                    isMasterAdmin        = s.isMasterAdmin,
                    isQuestionAppr       = s.isQuestionAppr,
                    isMasterQuestionAppr = s.isMasterQuestionAppr,
                    isTestAppr           = s.isTestAppr,
                    isMasterTestAppr     = s.isMasterTestAppr,
                }).FirstOrDefault();

                if (s == null)
                {
                    return(CreatedAtAction(nameof(login), new { result = ResultCode.DataHasNotFound, message = ResultMessage.DataHasNotFound }));
                }

                var log = new LoginStaffHistory();
                log.StaffID   = s.staffid;
                log.UserID    = s.id;
                log.AuthType  = AuthType.Login;
                log.Create_On = DateUtil.Now();
                log.Create_By = s.username;
                log.Update_On = DateUtil.Now();
                log.Update_By = s.username;
                _context.LoginStaffHistorys.Add(log);


                _context.SaveChanges();
                return(CreatedAtAction(nameof(login), new { result = ResultCode.Success, message = ResultMessage.Success, token = token, user = s }));
            }
            return(CreatedAtAction(nameof(login), new { result = ResultCode.WrongAccountorPassword, message = ResultMessage.WrongAccountorPassword }));
        }