示例#1
0
        public SubjectVM CreateOrUpdate(SubjectForCreateOrUpdateDTO subjectForCreateOrUpdateDTO)
        {
            if (subjectForCreateOrUpdateDTO == null)
            {
                throw new ArgumentNullException($"DTO of type is null");
            }
            var subjectEntity = _mapper.Map <Subject>(subjectForCreateOrUpdateDTO);

            if (subjectForCreateOrUpdateDTO.Id == null || subjectForCreateOrUpdateDTO.Id == 0)
            {
                _dbContext.Subjects.Add(subjectEntity);
            }
            else
            {
                _dbContext.Subjects.Update(subjectEntity);
            }

            try
            {
                if (_dbContext.SaveChanges() < 1)
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }

            var subjectVM = _mapper.Map <SubjectVM>(subjectEntity);

            return(subjectVM);
        }
示例#2
0
 public IActionResult CreateOrUpdateSubject(SubjectForCreateOrUpdateDTO subjectForCreateOrUpdateDTO)
 {
     if (ModelState.IsValid)
     {
         if (_subjectService.CreateOrUpdate(subjectForCreateOrUpdateDTO) != null)
         {
             return(RedirectToAction("Index"));
         }
         return(View("Error", new ErrorViewModel()
         {
             Description = _localizer["This subject already exists"]
         }));                                                                                                   //already exists with this name
     }
     return(View("CreateOrUpdateSubject"));
 }
示例#3
0
        public void WhenIdIsNullShouldModelBeNullAndViewShouldNotBeError()
        {
            Mock <ISubjectService> _subjectServiceMock = new Mock <ISubjectService>();

            _subjectServiceMock.Setup(s => s.GetSubject(It.IsAny <Expression <Func <Subject, bool> > >())).Returns(() => null);
            SubjectController _subjectController = new SubjectController(_subjectServiceMock.Object, _teacherServiceMock.Object, null, _mapperMock.Object, _localizerMock.Object, _loggerFactoryMock.Object);

            int?id = null;

            SubjectForCreateOrUpdateDTO expectedModel = null;

            var result = (ViewResult)_subjectController.CreateOrUpdateSubject(id);

            Assert.Equal(expectedModel, result.Model);
            Assert.NotEqual("Error", result.ViewName);
        }
示例#4
0
        public CreateOrUpdateSubjectTests()
        {
            _correctSubjectVM = new SubjectVM()
            {
                Id = 1, Name = "Name", Description = "Description"
            };
            _correctSubjectDTO = new SubjectForCreateOrUpdateDTO()
            {
                Id = 1, Name = "Name", Description = "Description", TeacherId = 4
            };
            _alreadyExistsErrorDescription = "This subject already exists!";

            _loggerFactoryMock  = new Mock <ILoggerFactory>();
            _teacherServiceMock = new Mock <ITeacherService>();
            _teacherServiceMock.Setup(ts => ts.GetTeachers(null)).Returns(() => ListOfTeachersVMs());
            _mapperMock = new Mock <IMapper>();
            _mapperMock.Setup(m => m.Map <SubjectForCreateOrUpdateDTO>(_correctSubjectVM)).Returns(() => _correctSubjectDTO);
            _localizerMock = new Mock <IStringLocalizer <SubjectController> >();
            _localizerMock.Setup(l => l["This subject already exists"]).Returns(() =>
                                                                                new LocalizedString("This subject already exists", _alreadyExistsErrorDescription));
        }