Esempio n. 1
0
        public Teacher Create(TeacherViewModel viewModel)
        {
            Teacher teacher = new Teacher
            {
                Name              = viewModel.Name,
                SecondName        = viewModel.SecondName,
                Salary            = viewModel.Salary,
                YearsOfExperience = viewModel.YearsOfExperience,
                SectionId         = viewModel.SectionId,
                Position          = _positionFactory.GetPosition(DefaultPositions.Teacher)
            };

            teacher.SetSectionName(_context.Sections.Single(s => s.Id == viewModel.SectionId).Name);
            return(teacher);
        }
        public IActionResult Create(TeacherViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.Sections = _schoolServices.ListOfSections().ToList();
                return(View(viewModel));
            }


            string uniqueFileName = null;

            if (viewModel.Photo != null)
            {
                string uploadsFolder = Path.Combine(_hostingEnviroment.WebRootPath, "images", "teachers");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + viewModel.Photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                viewModel.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
            }
            IPositionStrategy strategy = new PositionStrategy();
            string            name     = strategy.CreatePosition(viewModel.SectionId);
            var      sectionName       = _schoolServices.GetSection((int)viewModel.SectionId).Name;
            Teacher  teacher           = _factory.Create(viewModel);
            Position position          = _positionFactory.GetPosition(name);

            teacher.Position  = position;
            teacher.PhotoPath = uniqueFileName;
            _repository.Add(teacher);
            return(RedirectToAction("Index"));
        }