示例#1
0
        public ActionResult Add()
        {
            GifBaseViewModel viewModel = new GifBaseViewModel();

            viewModel.Init(_categoryRepository);

            return(View(viewModel));
        }
示例#2
0
        public ActionResult Add(GifBaseViewModel viewModel, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    viewModel.FileErrorMessage = "You need to choose a file!";
                    viewModel.Init(_categoryRepository);

                    return(View(viewModel));
                }

                var allowedExtensions = ".gif";
                var checkextension    = Path.GetExtension(file.FileName).ToLower();

                if (!allowedExtensions.Contains(checkextension))
                {
                    viewModel.FileErrorMessage = "Only Gif files are allowed!";
                    viewModel.Init(_categoryRepository);

                    return(View(viewModel));
                }

                var gif = viewModel.Gif;

                gif.User = _userRepository.GetUser(User.Identity.Name);

                gif.Bytes = new byte[file.ContentLength];
                file.InputStream.Read(gif.Bytes, 0, file.ContentLength);

                var category = _categoryRepository.Get(viewModel.Gif.CategoryId);

                gif.addCategory(category);
                category.AddGif(gif.Id);

                //Add Gif
                _gifRepository.Add(gif);
                TempData["Message"] = "Gif was successfully added!";

                return(RedirectToAction("Index"));
            }

            viewModel.Init(_categoryRepository);

            return(View(viewModel));
        }