public IHttpActionResult UpdateStudent(int id, StudentServiceModel updatedStudent)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            var oldStudent = this.GetStudentById(id);

            oldStudent.FirstName = updatedStudent.FirstName;
            oldStudent.LastName  = updatedStudent.LastName;
            foreach (var courseName in updatedStudent.Courses)
            {
                if (!oldStudent.Courses.Any(c => c.Name == courseName))
                {
                    oldStudent.Courses.Add(new Course
                    {
                        Name = courseName
                    });
                }
            }

            this.data.Students.SaveChanges();

            return(Ok(StudentServiceModel.ConvertFromStudent(oldStudent)));
        }
        public IHttpActionResult RemoveStudent(int id)
        {
            var student = this.GetStudentById(id);

            this.data.Students.Delete(student);
            this.data.Students.SaveChanges();

            return(Ok(StudentServiceModel.ConvertFromStudent(student)));
        }
        public IHttpActionResult EnrollInCourse(int studentId, CourseServiceModel newCourse)
        {
            var student = this.GetStudentById(studentId);

            var course = newCourse.ConvertToCourse();

            student.Courses.Add(course);
            this.data.SaveChanges();

            return(Ok(StudentServiceModel.ConvertFromStudent(student)));
        }
        public async Task CreateStudent_WithNonExistentClassId_ShouldThrowArgumentNullException()
        {
            string errorMessagePrefix = "StudentService CreateStudentAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.studentService = new StudentService(context);

            var testStudent = new StudentServiceModel();

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await this.studentService.CreateStudentAsync(testStudent));
        }
Example #5
0
        public IHttpActionResult AddStudent(Guid courseId, StudentServiceModel studentModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            var course = this.GetCourseById(courseId);

            course.Students.Add(studentModel.ConverToStudent());
            this.data.SaveChanges();

            return(Ok(CourseServiceModel.ConvertFromCourse(course)));
        }
        public IHttpActionResult AddStudent(StudentServiceModel newStudent)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            Student student = newStudent.ConverToStudent();

            this.data.Students.Add(student);
            this.data.SaveChanges();

            newStudent.Id = student.StudentId;
            return(Ok(newStudent));
        }
        public IHttpActionResult EnrollInCourse(int studentId, Guid courseId)
        {
            var student = this.GetStudentById(studentId);

            var course = this.data.Courses.All().FirstOrDefault(c => c.Id == courseId);

            if (course == null)
            {
                BadRequest("Invalud course id" + courseId);
            }

            student.Courses.Add(course);

            return(Ok(StudentServiceModel.ConvertFromStudent(student)));
        }
        public async Task EditStudent_WithExistentId_ShouldSuccessfullyEditStudent()
        {
            string errorMessagePrefix = "StudentService EditStudentAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.studentService = new StudentService(context);

            var testId = context.Students.First().Id;

            var testStudent = new StudentServiceModel
            {
                Id           = testId,
                FullName     = "EditStudent",
                PIN          = "EditPIN",
                Citizenship  = "TestCitizenship",
                DateOfBirth  = DateTime.UtcNow,
                PhoneNumber  = "TestPhone",
                Town         = "TestTown",
                Municipality = "TestMunicipality",
                Region       = "TestRegion",
                Address      = "TestAddress",
            };

            var actualResult = await this.studentService.EditStudentAsync(testStudent);

            var editedStudent = context.Students.Find(testId);

            Assert.True(actualResult, errorMessagePrefix);

            Assert.True(editedStudent.FullName == testStudent.FullName, errorMessagePrefix + " " + "FullName is not set properly");
            Assert.True(editedStudent.PIN == testStudent.PIN, errorMessagePrefix + " " + "PIN is not set properly");
            Assert.True(editedStudent.Citizenship == testStudent.Citizenship, errorMessagePrefix + " " + "Citizenship is not set properly");
            Assert.True(editedStudent.DateOfBirth == testStudent.DateOfBirth, errorMessagePrefix + " " + "DateOfBirth is not set properly");
            Assert.True(editedStudent.PhoneNumber == testStudent.PhoneNumber, errorMessagePrefix + " " + "PhoneNumber is not set properly");
            Assert.True(editedStudent.Town == testStudent.Town, errorMessagePrefix + " " + "Town is not set properly");
            Assert.True(editedStudent.Municipality == testStudent.Municipality, errorMessagePrefix + " " + "Municipality is not set properly");
            Assert.True(editedStudent.Region == testStudent.Region, errorMessagePrefix + " " + "Region is not set properly");
            Assert.True(editedStudent.Address == testStudent.Address, errorMessagePrefix + " " + "Address is not set properly");
        }
        public async Task <IActionResult> Edit(StudentEditInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                StudentServiceModel serviceModel = model.To <StudentServiceModel>();

                bool result = await this.studentService.EditStudentAsync(serviceModel);

                if (result)
                {
                    return(this.RedirectToAction("All"));
                }
            }

            var studentClass = await this.classService.GetStudentClassDropdownAsync(model.Id);

            this.ViewBag.StudentClass = studentClass;

            return(this.View(model));
        }
        public async Task <IActionResult> Create(StudentCreateInputModel model)
        {
            if (this.ModelState.IsValid)
            {
                StudentServiceModel serviceModel = model.To <StudentServiceModel>();

                bool result = await this.studentService.CreateStudentAsync(serviceModel);

                if (result)
                {
                    return(this.RedirectToAction("All"));
                }
            }

            var classes = await this.classService.GetClassesDropdownAsync();

            this.ViewBag.ClassesNames = classes;

            return(this.View(model));
        }
Example #11
0
        public async Task <bool> CreateStudentAsync(StudentServiceModel model)
        {
            Student student = AutoMapper.Mapper.Map <Student>(model);

            Class classFromDb = await this.context.Classes.SingleOrDefaultAsync(c => c.Id == model.ClassId);

            if (classFromDb == null)
            {
                throw new ArgumentNullException(nameof(classFromDb));
            }

            classFromDb.Students.Add(student);

            this.context.Classes.Update(classFromDb);

            await this.context.Students.AddAsync(student);

            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task CreateStudent_WithCorrectData_ShouldSuccessfullyCreateStudent()
        {
            string errorMessagePrefix = "StudentService CreateStudentAsync() does not work properly.";

            var context = NetBookDbContextInMemoryFactory.InitializeContext();

            await this.SeedData(context);

            this.studentService = new StudentService(context);

            var classId = context.Classes.First().Id;

            var testStudent = new StudentServiceModel
            {
                Id      = "CreateStudent",
                ClassId = classId,
            };

            var actualResult = await this.studentService.CreateStudentAsync(testStudent);

            Assert.True(actualResult, errorMessagePrefix);

            Assert.True(context.Students.Any(x => x.Id == "CreateStudent"), errorMessagePrefix + " " + "Student is not added to context.");
        }
Example #13
0
        private void check_User()
        {
            if (this.tb_userName.Text.Length > 0)
            {
                Exception exception;
                try
                {
                    StudentServiceModel model = new StudentServiceModel();//new StudentService().getStudent(this.tb_userName.Text, "~!{#$@cdf3}");
                    if (false && model.version != 4)
                    {
                        MessageBox.Show("版本太低,请从新下载最新的版本");
                        //Application.Exit();
                    }
                    else if (true || model != null)
                    {
                        if (false && model.starttime.ToString() == "0001/1/1 0:00:00")
                        {
                            MessageBox.Show("没有该堂考试信息。。。");
                            //Application.Exit();
                        }
                        else
                        {
                            TimeSpan span  = (TimeSpan)(model.currenttime - model.starttime);
                            TimeSpan span2 = (TimeSpan)(model.endtime - model.currenttime);
                            if (false && span2.Minutes < 0)
                            {
                                MessageBox.Show("考试时间已过请联系管理员。。。");
                                //Application.Exit();
                            }
                            else if (false && span.Minutes < 0)
                            {
                                MessageBox.Show("未到考试时间。。。");
                                //Application.Exit();
                            }
                            else
                            {
                                //StudentModel.isReGeneration = model.isregeneration;
                                //StudentModel.studentNumber = model.studentnumber;
                                //StudentModel.mac = model.mac;
                                //StudentModel.chooseNumber = model.choosenumber;
                                //StudentModel.gapfillingNumber = model.gapfillingnumber;
                                //StudentModel.calculationNumber = model.calculationnumber;
                                //StudentModel.endTime = model.endtime;
                                //StudentModel.examCode = model.examcode;
                                //StudentModel.grade = model.grade;
                                //StudentModel.startTime = model.starttime;
                                //StudentModel.status = model.status;
                                //StudentModel.studentName = model.studentname;
                                //StudentModel.studentClass = model.studentclass;

                                StudentModel.isReGeneration    = true;
                                StudentModel.studentNumber     = "02166407";
                                StudentModel.mac               = "FF:FF:FF:FF:FF:FF";
                                StudentModel.chooseNumber      = generateChoice();     //"ChapterOne_25,3,2,1,4,|ChapterTwo_9,1,4,2,3,|ChapterThree_3,3,1,4,2,|ChapterFour_11,4,2,1,3,|ChapterFive_3,4,1,2,3,|";
                                StudentModel.gapfillingNumber  = generateGapFilling(); //"g_four_1.xml|g_one_2.xml|g_five_1.xml|g_six_1.xml|g_two_2_6.xml|";
                                StudentModel.calculationNumber = generateCal();        //"c8.xml|c1.xml|c3.xml|c10.xml|";
                                StudentModel.endTime           = DateTime.Now.AddDays(1);
                                StudentModel.examCode          = 2;
                                StudentModel.grade             = 0;
                                StudentModel.startTime         = DateTime.Now.AddDays(-1);
                                StudentModel.status            = 0;
                                StudentModel.studentName       = "AMFairy";
                                StudentModel.studentClass      = "NEU Mathe";

                                if (true || (StudentModel.mac.Length == 0) || ((StudentModel.mac == new GetMacAddress().getmac()) && (StudentModel.status == 1)))
                                {
                                    string str2;
                                    string str3;
                                    string str = "temp";
                                    SupportTools.DirectoryIsExist("temp");
                                    SupportTools.DirectoryIsExist("temp/generate");
                                    SupportTools.DirectoryIsExist("temp/UA");
                                    if (!StudentModel.isReGeneration)
                                    {
                                        str2 = StudentModel.examCode + "/" + StudentModel.studentNumber;
                                        str3 = "Parameter.zip";
                                        try
                                        {
                                            UploadAndDownload.DownloadFile(str2, str3);
                                            Zip.Extract("./" + str + "/" + str3, "./", 0x400);
                                            UploadAndDownload.DownloadFile(str2, "Answer.zip");
                                            Zip.Extract("./" + str + "/Answer.zip", "./", 0x400);
                                        }
                                        catch (Exception)
                                        {
                                            try
                                            {
                                                UploadAndDownload.DownloadFile(str2, str3);
                                                Zip.Extract("./" + str + "/" + str3, "./", 0x400);
                                                UploadAndDownload.DownloadFile(str2, "AnswerCopy.zip");
                                                Zip.Extract("./" + str + "/AnswerCopy.zip", "./", 0x400);
                                            }
                                            catch (Exception exception2)
                                            {
                                                exception = exception2;
                                                MessageBox.Show("试卷还原的参数下载失败");
                                            }
                                        }
                                    }
                                    List <string> list  = new List <string>();
                                    List <string> list2 = new List <string>();
                                    foreach (string str4 in StudentModel.chooseNumber.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                                    {
                                        string[] strArray2 = str4.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                        list.Add(strArray2[0]);
                                        string[] strArray3 = strArray2[0].Split(new char[] { '_' });
                                        list2.Add(strArray3[0]);
                                    }
                                    try
                                    {
                                        MessageBox.Show("即将开始考试,请耐心等待试卷生成");
                                        for (int j = 0; j < list.Count; j++)
                                        {
                                            str2 = list2[j];
                                            str3 = list[j] + ".zip";

                                            UploadAndDownload2.DownloadFile(str2, str3);
                                        }
                                    }
                                    catch (Exception exception3)
                                    {
                                        exception = exception3;
                                        MessageBox.Show("从服务器上下载文件失败\r\n" + exception.Message);
                                    }
                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        str2 = list2[i];
                                        Zip.Extract("./temp/" + (list[i] + ".zip"), "./temp", 0x400);
                                    }
                                    new Origination().Show();
                                    base.Hide();
                                }
                                else
                                {
                                    MessageBox.Show("在另外一台机器上登陆了。。。");
                                    //Application.Exit();
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("输入的学号不存在或不在考试时间范围内");
                        //Application.Exit();
                    }
                }
                catch (Exception exception4)
                {
                    exception = exception4;
                    if (exception.Message.Contains("System.Number.ParseInt32"))
                    {
                        MessageBox.Show("未安排你的考试");
                    }
                    else
                    {
                        MessageBox.Show("连接服务器错误。。。" + exception.Message);
                    }
                    //Application.Exit();
                }
            }
            else
            {
                MessageBox.Show("请输入学号");
            }
        }
        public IHttpActionResult StudentById(int id)
        {
            var student = this.GetStudentById(id);

            return(Ok(StudentServiceModel.ConvertFromStudent(student)));
        }
        public IHttpActionResult AllStudents()
        {
            var students = this.data.Students.All().ToList().Select(s => StudentServiceModel.ConvertFromStudent(s));

            return(Ok(students));
        }
Example #16
0
        public async Task <bool> EditStudentAsync(StudentServiceModel model)
        {
            Student student = await this.context.Students.Include(x => x.Class)
                              .SingleOrDefaultAsync(x => x.Id == model.Id);

            if (student == null)
            {
                throw new ArgumentNullException(nameof(student));
            }

            if (student.FullName != model.FullName)
            {
                student.FullName = model.FullName;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.PIN != model.PIN)
            {
                student.PIN = model.PIN;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.Citizenship != model.Citizenship)
            {
                student.Citizenship = model.Citizenship;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.DateOfBirth != model.DateOfBirth)
            {
                student.DateOfBirth = model.DateOfBirth;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.PhoneNumber != model.PhoneNumber)
            {
                student.PhoneNumber = model.PhoneNumber;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.Town != model.Town)
            {
                student.Town = model.Town;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.Municipality != model.Municipality)
            {
                student.Municipality = model.Municipality;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.Region != model.Region)
            {
                student.Region = model.Region;

                student.ModifiedOn = DateTime.UtcNow;
            }

            if (student.Address != model.Address)
            {
                student.Address = model.Address;

                student.ModifiedOn = DateTime.UtcNow;
            }

            this.context.Students.Update(student);

            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }