示例#1
0
        public ActionResult Create(WriterView writerView)
        {
            bool   photoExist = true;
            string count      = string.Empty;
            int    i          = 0;

            if (ModelState.IsValid)
            {
                string picture = string.Empty;
                string folder  = "~/Content/Photos/Writers";

                if (writerView.PhotoFile != null)
                {
                    while (photoExist)
                    {
                        i++;
                        count = Convert.ToString(i);

                        photoExist = System.IO.File.Exists(Server.MapPath($"{folder}/{count}{writerView.PhotoFile.FileName}"));
                    }

                    if (count == "0")
                    {
                        count = string.Empty;
                    }

                    picture = FilesHelper.UploadPhoto(writerView.PhotoFile, folder, count);
                    picture = string.Format($"{folder}/{count}{picture}");
                }
                else
                {
                    picture = "Default.gif";
                    folder  = "~/Content/Photos/Writers";
                    picture = string.Format($"{folder}/{picture}");
                }

                Writer writer = ToWriter(writerView);
                writer.Photo = picture;
                db.Writers.Add(writer);

                try
                {
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    if (ex.InnerException.InnerException.Message.Contains("_Index"))
                    {
                        ModelState.AddModelError(string.Empty, "El escritor/a no puede ser guardado/a porque existe un/a con el mismo nombre.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }

            return(View(writerView));
        }
示例#2
0
        public void Update(WriterView item)
        {
            Writer writer = CustomMap.ReverseWriter(item);

            repo.Writers.Update(writer);
            repo.Save();
        }
示例#3
0
        public ActionResult DeletePhoto(WriterView writerView)
        {
            string picture = "Default.gif";
            string folder  = "~/Content/Photos/Writers";

            picture = string.Format($"{folder}/{picture}");

            Writer writer = ToWriter(writerView);

            writer.Photo = picture;

            db.Entry(writer).State = EntityState.Modified;

            try
            {
                DeletePhoto(writerView.Photo);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            return(View(writerView));
        }
示例#4
0
 public void Create(WriterView item)
 {
     if (item != null)
     {
         Writer newart = CustomMap.ReverseWriter(item);
         repo.Writers.Create(newart);
         repo.Save();
         return;
     }
     throw new ValidationException("Объект не создан", "");
 }
示例#5
0
 private Writer ToWriter(WriterView writerView)
 {
     return(new Writer
     {
         Name = writerView.Name,
         Biography = writerView.Biography,
         WriterId = writerView.WriterId,
         Book = writerView.Book,
         Photo = writerView.Photo
     });
 }
示例#6
0
        public static Writer ReverseWriter(WriterView writer)
        {
            Mapper.Initialize(p =>
            {
                p.CreateMap <WriterView, Writer>();
                p.CreateMap <ArticleView, Article>();
                p.CreateMap <BookView, Book>();
            });

            return(Mapper.Map <WriterView, Writer>(writer));;
        }
示例#7
0
        public ActionResult Edit(WriterView writer)
        {
            try
            {
                db.Update(writer);

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
示例#8
0
        public FileResult GetImage(int?id)
        {
            WriterView witer = db.Get(id);

            if (witer != null)
            {
                return(File(witer.ImageData, witer.ImageMimeType));
            }
            else
            {
                return(null);
            }
        }
示例#9
0
        public ActionResult Create(WriterView writer, HttpPostedFileBase Image)
        {
            if (Image != null)
            {
                writer.ImageMimeType = Image.ContentType;
                writer.ImageData     = new byte[Image.ContentLength];
                Image.InputStream.Read(writer.ImageData, 0, Image.ContentLength);
            }

            try
            {
                db.Create(writer);

                return(RedirectToAction("Index"));
            }
            catch (ValidationException ex)
            {
                return(Content(ex.Message));
            }
        }