Esempio n. 1
0
        public async Task CreateStudent_ReturnsCreatedStudent()
        {
            var student = new StudentInputViewModel
            {
                FirstName = "Stu",
                LastName  = "Steiner"
            };

            var service = new Mock <IStudentService>();

            service.Setup(x => x.AddStudent(It.Is <Student>(g => g.FirstName == student.FirstName)))
            .Returns(Task.FromResult(new Student
            {
                Id        = 2,
                FirstName = student.FirstName,
                LastName  = student.LastName
            }))
            .Verifiable();

            var controller = new StudentsController(service.Object, Mapper.Instance);

            var     result      = (await controller.AddStudent(student)).Result as OkObjectResult;
            Student resultValue = (Student)result.Value;

            Assert.IsNotNull(resultValue);
            Assert.AreEqual(2, resultValue.Id);
            Assert.AreEqual("Stu", resultValue.FirstName);
            Assert.AreEqual("Steiner", resultValue.LastName);
            service.VerifyAll();
        }
Esempio n. 2
0
        public IActionResult CreateStudent(StudentInputViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            var student = new Student
            {
                FullName       = model.FullName,
                Faculty        = model.Faculty,
                EnrollmentDate = model.EnrollmentDate,
                Email          = model.Email
            };

            dbContext.Add(student);
            try
            {
                dbContext.SaveChanges();
                return(this.RedirectToAction("DetailsStudent", new { Id = student.Id }));
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }
Esempio n. 3
0
        public IActionResult EditStudent(StudentInputViewModel model, int id)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(model));
            }
            var student = dbContext.Students.FirstOrDefault(c => c.Id == id);

            if (student == null)
            {
                return(this.BadRequest("Invalid professor ID."));
            }

            student.FullName       = model.FullName;
            student.Faculty        = model.Faculty;
            student.EnrollmentDate = model.EnrollmentDate;
            student.Email          = model.Email;

            try
            {
                this.dbContext.SaveChanges();
                return(this.RedirectToAction("DetailsStudent", new { Id = id }));
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }
Esempio n. 4
0
        public async Task <ActionResult <StudentViewModel> > AddStudent(StudentInputViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

            Student createdStudent = await StudentService.AddStudent(Mapper.Map <Student>(viewModel)).ConfigureAwait(false);

            return(Ok(createdStudent));
        }
Esempio n. 5
0
        public async Task <ActionResult <StudentViewModel> > UpdateStudent(int id, StudentInputViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }
            Student fetchedStudent = await StudentService.GetById(id).ConfigureAwait(false);

            if (fetchedStudent == null)
            {
                return(NotFound());
            }

            Mapper.Map(viewModel, fetchedStudent);
            await StudentService.UpdateStudent(fetchedStudent).ConfigureAwait(false);

            return(NoContent());
        }
Esempio n. 6
0
        public IActionResult DeleteStudent(int id)
        {
            var professor = dbContext.Students.FirstOrDefault(c => c.Id == id);

            if (professor == null)
            {
                return(this.BadRequest("Invalid student ID."));
            }

            var viewModel = new StudentInputViewModel
            {
                FullName       = professor.FullName,
                Faculty        = professor.Faculty,
                EnrollmentDate = professor.EnrollmentDate,
                Email          = professor.Email
            };

            return(this.View(viewModel));
        }
Esempio n. 7
0
        public async Task UpdateStudent_ReturnsUpdatedStudent()
        {
            var student = new StudentInputViewModel
            {
                FirstName = "Stu",
                LastName  = "Steiner"
            };
            var service = new Mock <IStudentService>();
            //service.Setup(x => x.GetById(2))
            //    .Returns(Task.FromResult(new Student
            //    {
            //        Id = 2,
            //        Name = student.Name
            //    }))
            //    .Verifiable();

            var controller = new StudentsController(service.Object, Mapper.Instance);

            var result = (await controller.UpdateStudent(2, student));

            Assert.IsNotNull(result);
            service.VerifyAll();
        }
Esempio n. 8
0
        public async Task <IActionResult> Add(StudentInputViewModel viewModel)
        {
            IActionResult result = View();

            if (ModelState.IsValid)
            {
                using (var httpClient = ClientFactory.CreateClient("GeorestApi"))
                {
                    try
                    {
                        var georestClient = new GeorestClient(httpClient.BaseAddress.ToString(), httpClient);
                        await georestClient.AddStudentAsync(viewModel);

                        result = RedirectToAction(nameof(Index));
                    }
                    catch (SwaggerException se)
                    {
                        ModelState.AddModelError("", se.Message);
                    }
                }
            }

            return(result);
        }