コード例 #1
0
        public async Task AddAsync_StudentNotNull_PropertiesAreEqual()
        {
            var students = new List <Student> {
                new()
                {
                    Id = 1
                }
            };
            var context = await GetMockContextAsync(students);

            IStudentRepository repo = new StudentRepository(context);
            var studentForAdding    = new Student
            {
                Name       = TestName,
                LastName   = TestLastName,
                MiddleName = TestMiddleName,
                Nickname   = TestNickname,
                Sex        = TestSex
            };

            await repo.AddAsync(studentForAdding);

            await context.SaveChangesAsync();

            var addedStudent = context
                               .Students
                               .AsEnumerable()
                               .LastOrDefault();

            Assert.AreEqual(TestName, addedStudent.Name);
            Assert.AreEqual(TestLastName, addedStudent.LastName);
            Assert.AreEqual(TestMiddleName, addedStudent.MiddleName);
            Assert.AreEqual(TestNickname, addedStudent.Nickname);
            Assert.AreEqual(TestSex, addedStudent.Sex);
        }
コード例 #2
0
        public async Task AddAsync_StudentNotNull_IncrementedId()
        {
            var students = new List <Student>
            {
                new()
                {
                    Id = 2
                }
            };
            var context = await GetMockContextAsync(students);

            IStudentRepository repo = new StudentRepository(context);
            var studentForAdding    = new Student();

            await repo.AddAsync(studentForAdding);

            await context.SaveChangesAsync();

            var addedStudent = context
                               .Students
                               .AsEnumerable()
                               .LastOrDefault();

            Assert.AreEqual(3, addedStudent.Id);
        }
コード例 #3
0
        public async Task Should_Add_Entity()
        {
            // Arrange
            var student = new Student()
            {
                Id                = 1,
                FirstName         = "test",
                LastName          = "last",
                RegisteredCourses = new List <RegisteredCourse> {
                    new RegisteredCourse {
                        Id = 2, CourseId = 1, StudentId = 1
                    }
                }
            };
            int count = 0;

            // Act
            using (var context = new SampleProjectDbContext(dbContextOptions))
            {
                IStudentRepository repository = new StudentRepository(context);
                await repository.AddAsync(student);

                context.SaveChanges();
                count = context.Students.Where(x => x.Id == 1).Count();
            }

            // Assert
            Assert.AreEqual(1, count);
        }
コード例 #4
0
        public async Task <IDataResult <long> > Add(AddStudentModel addStudentModel)
        {
            var validation = new AddStudentModelValidator().Valid(addStudentModel);

            if (!validation.Success)
            {
                return(new ErrorDataResult <long>(validation.Message));
            }

            StudentDomain domain = new StudentDomain(DatabaseUnitOfWork, StudentRepository, CourseRepository);

            var    student      = domain.ConvertToStudentEntity(addStudentModel);
            string errorMessage = await domain.ApplyBusinessRules(student);

            if (!string.IsNullOrWhiteSpace(errorMessage))
            {
                return(new ErrorDataResult <long>(errorMessage));
            }

            await StudentRepository.AddAsync(student);

            await DatabaseUnitOfWork.SaveChangesAsync();

            return(new SuccessDataResult <long>(student.StudentId));
        }
コード例 #5
0
        public async Task <ActionResult <StudentDto> > CreateStudent(StudentDto studentDto)
        {
            if (await repo.GetStudentAsync(studentDto.Email, false) != null)
            {
                ModelState.AddModelError("Email", "Email in use");
                return(BadRequest(ModelState));
            }

            var student = mapper.Map <Student>(studentDto);
            await repo.AddAsync(student);

            if (await repo.SaveAsync())
            {
                var model = mapper.Map <StudentDto>(student);
                return(CreatedAtAction(nameof(GetStudent), new { email = model.Email }, model));
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #6
0
        public void GetByIdAsync_Student_GetsCorrectly()
        {
            using var context = new UniversityContext(builder.Options);
            var repository = new StudentRepository(context);

            var student = new Student()
            {
                Name = "TestStudent1"
            };
            var res           = repository.AddAsync(student).Result;
            var studentFromDb = repository.GetByIdAsync(res.Id).Result;

            Assert.That(studentFromDb, Is.EqualTo(res));
        }
コード例 #7
0
        public void AddAsync_Student_AddsCorrectly()
        {
            using var context = new UniversityContext(builder.Options);

            var repository = new StudentRepository(context);

            var student = new Student()
            {
                Name = "TestStudent1"
            };
            var res = repository.AddAsync(student).Result;

            Assert.That(res.Id, Is.Not.EqualTo(0));
        }
コード例 #8
0
        public void DeleteAsync_Student_DeletesCorrectly()
        {
            using var context = new UniversityContext(builder.Options);
            var repository = new StudentRepository(context);

            var student = new Student()
            {
                Name = "TestStudent1"
            };
            var res = repository.AddAsync(student).Result;

            repository.DeleteAsync(res.Id).Wait();
            var deletedStudent = repository.GetByIdAsync(res.Id).Result;

            Assert.That(deletedStudent, Is.EqualTo(null));
        }
コード例 #9
0
        public void UpdateAsync_Student_UpdatesCorrectly()
        {
            using var context = new UniversityContext(builder.Options);
            var repository = new StudentRepository(context);

            var student = new Student()
            {
                Name = "TestStudent1"
            };
            var res           = repository.AddAsync(student).Result;
            var studentFromDb = repository.GetByIdAsync(res.Id).Result;

            studentFromDb.Name = "ChangedName";
            var changedRes = repository.UpdateAsync(studentFromDb).Result;

            Assert.That(changedRes.Name, Is.EqualTo("ChangedName"));
        }
コード例 #10
0
        public async Task AddAsync_Test()
        {
            var options = new DbContextOptionsBuilder <ApplicationContext>()
                          .UseInMemoryDatabase(databaseName: "AddTestDatabase")
                          .Options;

            using (var context = new ApplicationContext(options))
            {
                context.Database.EnsureCreated();
                var repository = new StudentRepository(context);
                var newStudent = new Student {
                    Email = "Test", Mobile = "Test", Name = "Test"
                };
                var prevCount = repository.GetAll().Count();
                await repository.AddAsync(newStudent);

                await context.SaveChangesAsync();

                var students = repository.GetAll();
                Assert.That(students.Count(), Is.EqualTo(prevCount + 1));
            }
        }
コード例 #11
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var newStudent = new Student()
                {
                    FirstMidName = model.FirstMidName, LastName = model.LastName, UserID = user.Id
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    using (var Context = new StudentRepository())
                    {
                        await Context.AddAsync(newStudent);

                        await Context.SaveAsync();
                    }
                    await UserManager.AddToRoleAsync(user.Id, "Student");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }
            return(View(model));
        }