예제 #1
0
        public ActionResult AddMainFoto(HttpPostedFileBase f, string kittenName)
        {
            _logger.Info("Add main photo for kittenName=" + kittenName);

            var copy = new MemoryStream();

            f.InputStream.CopyTo(copy);

            string path       = SaveMainPicture(kittenName, f.InputStream);
            string redirectTo = "AdminChangeKittens";

            if (!String.IsNullOrEmpty(path))
            {
                Pets kitten         = _kittensRepository.GetKittenByName(kittenName);
                var  oldMainPicture = _picturesRepository.GetCollection().FirstOrDefault(i => i.IsMainPicture && i.Pet.ID == kitten.ID);

                if (oldMainPicture != null)
                {
                    _picturesRepository.Delete(oldMainPicture.ID);
                }

                _picturesRepository.Insert(new Pictures()
                {
                    Image = path, IsMainPicture = true, Pet = kitten, PetID = kitten.ID
                });
                _picturesRepository.Save();

                redirectTo = kitten.IsParent ? "AdminChangeParents" : "AdminChangeKittens";

                _kittensRepository.Update(kitten);
                _kittensRepository.Save();

                //Save main photo for kittens main page.
                AddPhoto(copy, kittenName);
            }

            return(RedirectToAction(redirectTo));
        }