Esempio n. 1
0
        public ActionResult Add(Book book, HttpPostedFileBase picture, int categoryId)
        {
            //if (ModelState.IsValid)
            {
                KnihovnaUserDao      d = new KnihovnaUserDao();
                KnihovnaUser         u = d.GetByLogin(User.Identity.Name);
                KnihovnaKategorieDao k = new KnihovnaKategorieDao();

                Book b = new Book()
                {
                    Name             = book.Name,
                    Author           = book.Author,
                    PublishedYear    = book.PublishedYear,
                    Id               = Books.Counter(),
                    Description      = book.Description,
                    OwnerId          = u,
                    Kategorie        = k.GetbyId(2),
                    LastEditDateTime = DateTime.Now,
                    Version          = 1,
                    IsPayed          = false,
                    UnlockPrice      = 0,
                    MinimalLevel     = 0
                };


                if (picture != null)
                {
                    var z = picture.ContentLength;

                    if (picture.ContentType == "image/jpeg" || picture.ContentType == "image/png")
                    {
                        Image image     = Image.FromStream(picture.InputStream);
                        Image smalImage = ImageHelper.ScaleImage(image, 200, 200);


                        Bitmap btmBitmap = new Bitmap(smalImage);
                        Guid   guid      = Guid.NewGuid();

                        string imageName = guid.ToString() + ".jpg";
                        btmBitmap.Save(Server.MapPath("~/Uploads/Book/") + imageName, ImageFormat.Jpeg); // Je potřeba namapovat cestu!

                        btmBitmap.Dispose();
                        image.Dispose();

                        b.ImageName = imageName;
                    }
                }

                BookCategoryDao bookCategoryDao = new BookCategoryDao();
                BookCategory    bookCategory    = bookCategoryDao.GetbyId(5);

                b.Category  = bookCategory;
                b.Author    = "nějaká děvka";
                b.SectionId = categoryId;

                BookDao bookDao    = new BookDao();
                int     identifier = (int)bookDao.Create(b);

                BookVersionDao bv = new BookVersionDao();
                BookVersion    v  = new BookVersion();

                v.Text         = b.Description;
                v.ChangedBy    = u.Id;
                v.Date         = DateTime.Now;
                v.Id           = Books.Counter();
                v.IsApproved   = true;
                v.IsSuggestion = false;
                v.PostId       = identifier;
                v.SumText      = "Původní verze";
                v.Version      = 1;

                bv.Create(v);


                LevelUp.NewPost(u, d);
                NovinkyGenerator.PridatNovinku(b, u.Id);

                // Notifikace
                TempData["scs"] = "V pořádku";
            }
            // else
            {
                return(View("Create", book)); // Vrátím vstupní data
            }

            return(RedirectToAction("Index"));
        }