Esempio n. 1
0
        public AddMaterialControl()
        {
            InitializeComponent();

            this.DataContext = addMaterialViewModel = new AddMaterialViewModel();
            addMaterialViewModel.SaveMachiningEvent += AddMaterialViewModel_SaveMachiningEvent;
            this.Loaded += AddMaterialControl_Loaded;
        }
        public async Task <IActionResult> Create([Bind("Id,IdTopic,UrlMaterial")] AddMaterialViewModel material)
        {
            if (ModelState.IsValid)
            {
                material.Id = Guid.NewGuid();
                _context.Add(material);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(material));
        }
        public ActionResult AddMaterial(AddMaterialViewModel model)
        {
            if (model != null && ModelState.IsValid)
            {
                var dbMaterial = Mapper.Map <StudyMaterial>(model);

                dbMaterial.Author = this.UserProfile;

                if (model.UploadedImage != null)
                {
                    //Helper class in Infrastructure
                    using (var memory = new MemoryStream())
                    {
                        model.UploadedImage.InputStream.CopyTo(memory);
                        var content = memory.GetBuffer();

                        var fileExtension = model.UploadedImage.FileName.Split(new[] { '.' }).Last();

                        if (fileExtension != null && fileExtension != "jpeg")
                        {
                            fileExtension = "jpeg";
                        }

                        dbMaterial.Images = new List <Image>()
                        {
                            new Image()
                            {
                                Content       = content,
                                FileExtension = fileExtension
                            }
                        };
                    }
                }

                this.Data.StudyMaterials.Add(dbMaterial);

                this.Data.SaveChanges();

                return(RedirectToAction("AllMaterials", "StudyMaterial"));
            }

            model.Sections = this.Data.Sections
                             .All()
                             .Select(s => new SelectListItem
            {
                Value = s.Id.ToString(),
                Text  = s.Title
            });

            return(View(model));
        }
        public ActionResult AddMaterial()
        {
            var addMaterialViewModel = new AddMaterialViewModel()
            {
                Sections = this.Data.Sections
                           .All()
                           .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text  = s.Title
                })
            };

            return(this.View(addMaterialViewModel));
        }
Esempio n. 5
0
        public IActionResult Add(int lessonId)
        {
            var lesson = _traversalService.GetLesson(lessonId);

            if (lesson == null)
            {
                return(RedirectToAction("ResourceNotFound", "Error", new { error = "przedmiot o podanym Id nie istnieje." }));
            }

            ConfigureAddMaterialBreadcrumbs(lesson);

            var vm = new AddMaterialViewModel
            {
                LessonId = lessonId
            };

            return(View(vm));
        }
Esempio n. 6
0
        [HttpPost] // AJAX request
        public IActionResult Add(AddMaterialViewModel vm)
        {
            var identityUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var lesson = _traversalService.GetLesson(vm.LessonId);

            if (lesson == null)
            {
                return(BadRequest(new[] { "Przedmiot o podanym Id nie istnieje." }));
            }

            var creator = _userService.FindByIdentityUserId(identityUserId);
            var state   = _service.GetState(MaterialStateEnum.PENDING);

            var materialToAdd = new BLL.Models.Material(vm.Name, vm.Description, lesson, creator, state);

            _service.AddMaterial(materialToAdd);

            var guid = Guid.NewGuid();

            try
            {
                _filesManagement.SaveFilesToFileSystem(vm.FormFiles, materialToAdd.MaterialId, guid);
            }
            catch (Exception)
            {
                return(BadRequest(new[] { "Somethig went wrong while saving files to the file system. Try again." }));
            }

            // TODO splitted into two roundtrips to name folders with materialId (can be changed in the future)
            var filesToAdd = _filesManagement.ExtractFilesFromForm(vm.FormFiles, materialToAdd.MaterialId, guid);

            materialToAdd.AddFiles(filesToAdd);
            _service.UpdateMaterial(materialToAdd);

            return(Json(materialToAdd.MaterialId));
        }
Esempio n. 7
0
        public ActionResult Add(AddMaterialViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // check material existed
                    var isExisted = db.Materials
                                    .Where(t => string.Compare(t.MaterialName, model.MaterialName, true) == 0)
                                    .FirstOrDefault() != null;
                    if (isExisted)
                    {
                        throw new Exception("Nguyên liệu tên [" + model.MaterialName + "] đã tồn tại trong hệ thống!");
                    }
                    else
                    {
                        // insert new material
                        Material material = new Material();
                        material.MaterialName    = model.MaterialName;
                        material.Description     = model.Description;
                        material.CreationDate    = DateTime.Now;
                        material.QuantityPerUnit = model.QuantityPerUnit;

                        db.Materials.Add(material);
                        db.SaveChanges();

                        return(RedirectToAction("Index"));
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return(View(model));
        }
 public AddMaterialPage()
 {
     InitializeComponent();
     _viewModel  = new AddMaterialViewModel();
     DataContext = _viewModel;
 }