Пример #1
0
        public async Task <ResultSpec <int> > Add(StudentAddUpdateModel studentAddUpgdateModel)
        {
            var today = await _dateTimeProvider.GetTodayDateTime();

            var specResult = StudentAddUpdateSpec.IsSatisfiedBy(studentAddUpgdateModel, StudentDomainConstants.MinAgeForStudent, today);

            if (specResult.OverallResult)
            {
                //using (var scope = new TransactionScope(
                //    TransactionScopeOption.Required,
                //    new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
                //{
                var student    = _mapper.Map <Student>(studentAddUpgdateModel.Student);
                var newStudent = await _asyncStudentRepository.Insert(student);

                try
                {
                    await _asyncStudentRepository.Save();
                }
                catch (Exception e)
                {
                    var d = e;
                    throw;
                }

                //scope.Complete();

                return(Results.WithOkSpec <int>(newStudent.Id).AddMessage(Resource.Successful).AddSpec(specResult));
                //}
            }
            else
            {
                return(Results.WithFailSpec <int>(-1).AddSpec(specResult));
            }
        }
Пример #2
0
        public async Task <ResultSpec <bool> > Update(StudentAddUpdateModel studentAddUpdateModel)
        {
            var today = await _dateTimeProvider.GetTodayDateTime();

            var specResult = StudentAddUpdateSpec.IsSatisfiedBy(studentAddUpdateModel, 18, today);

            if (specResult.OverallResult)
            {
                using (var scope = new TransactionScope(
                           TransactionScopeOption.Required,
                           new TransactionOptions {
                    IsolationLevel = IsolationLevel.ReadCommitted
                }))
                {
                    var student = _mapper.Map(studentAddUpdateModel, new Student());

                    await _asyncStudentRepository.Update(student);

                    await _asyncStudentRepository.Save();

                    scope.Complete();

                    return(Results.WithOkSpec(true).AddMessage(Resource.Successful));
                }
            }
            else
            {
                return(Results.WithFailSpec(false).AddSpec(specResult).AddMessage(Resource.Unsuccessfull));
            }
        }
Пример #3
0
        public async Task Minimum_yas_altindaki_ogrenciler_kaydedildiginde_result_fail_donmeli()
        {
            // TODO : sadece ilgilendiğimiz parametreleri doldursak olmazmıydı
            //Arrange
            var studentBlDto = new StudentBlDto
            {
                BirthDate = new DateTime(2003, 01, 01),
                FirstName = "Murat",
                LastName  = "Çabuk",
                Gender    = "Male",
                StudentId = "123456",
                Age       = 17
            };

            var student = new Student
            {
                BirthDate = new DateTime(2003, 01, 01),
                FirstName = "Murat",
                LastName  = "Çabuk",
                Gender    = "Male",
                StudentId = "123456"
            };

            mapperMock.Setup(t => t.Map <Student>(studentBlDto)).Returns(student);

            asyncStudentRespositoryMock.Setup(t => t.Insert(student));

            dateTimeProviderMock.Setup(t => t.GetTodayDateTime())
            .ReturnsAsync(() => new DateTime(2020, 01, 01));

            sutStudentManager = new StudentManager(asyncStudentRespositoryMock.Object,
                                                   mapperMock.Object,
                                                   dateTimeProviderMock.Object,
                                                   cacheMock.Object);

            var studentAddUpdateModel = new StudentAddUpdateModel
            {
                Student = studentBlDto
            };

            //Act
            var result = await sutStudentManager.Add(studentAddUpdateModel);

            var count = result.SpecResult.FailedSpecifications.ToFilteredSpecs(nameof(StudentBlDto.BirthDate)).Count;

            //Assert
            Assert.True(count > 0);


            // TODO : şu şekilde yapsaydık olmazmıydı
            // Assert.False(result.IsOk);
        }
        public static SpecificationResult IsSatisfiedBy(StudentAddUpdateModel studentAddUpdateModel, int minAge, DateTime today)
        {
            var dt = new DateTime(today.Year - minAge, today.Month, today.Day);

            var spec = Specification.NotNull <StudentAddUpdateModel>()
                       .And().NotEmpty(t => t.Student.FirstName)
                       .And().NotEmpty(t => t.Student.LastName)
                       .And().True(t => t.Student.Age > minAge)
                       .And().True(t => t.Student.BirthDate.AddYears(minAge).Date >= today.Date);


            var specificationResult = new SpecificationResult();

            spec.IsSatisfiedBy(studentAddUpdateModel, out specificationResult);

            return(specificationResult);
        }
Пример #5
0
        public async Task <IActionResult> AddUpdate(StudentAddUpdateViewModel studentAddUpdateViewModel)
        {
            var genders = await _optionManager.GetGenders();

            var studentBlDto = _mapper.Map <StudentBlDto>(studentAddUpdateViewModel.Student);

            var studentAddUpdateModel = new StudentAddUpdateModel
            {
                Student = studentBlDto
            };

            var result = await _studentManager.Add(studentAddUpdateModel);

            ModelState.FromSpec(result.SpecResult.FailedSpecifications);

            studentAddUpdateViewModel.Student.Id = result.Data;
            studentAddUpdateViewModel.Message    = result.Message;
            studentAddUpdateViewModel.Genders    = genders;

            return(View(studentAddUpdateViewModel));
        }