Exemplo n.º 1
0
        public ActionResult Edit(BookModel model)
        {
            if (ModelState.IsValid)
            {
                long id = _service.SaveBook(model);

                return RedirectToAction("view", "book", new
                {
                    id = id
                });
            }

            EditBookViewModel view = new EditBookViewModel()
            {
                Book = model,
                Categories = _service.GetCategories()
            };

            return View(view);
        }
Exemplo n.º 2
0
        public BookModel GetBook(long id)
        {
            BookRepository bRepo = new BookRepository(_dal);
            StoryRepository sRepo = new StoryRepository(_dal);

            Book book = bRepo.Get(id);
            if (book == null)
                throw new HttpException(404, "Ressource not found");

            Story[] stories = sRepo.List().Where(s => s.bookid == id).ToArray();
            List<string> storyNames = new List<string>();
            foreach (Story story in stories)
            {
                storyNames.Add(story.name);
            }

            BookModel bookModel = new BookModel()
            {
                Id = book.id,
                Name = book.name,
                Number = book.number,
                CategoryId = book.catid,
                Image = book.image,
                Added = book.added,
                Stories = storyNames.ToArray()
            };

            return bookModel;
        }
Exemplo n.º 3
0
        public long SaveBook(BookModel model)
        {
            BookRepository bRepo = new BookRepository(_dal);
            StoryRepository sRepo = new StoryRepository(_dal);

            Book book = new Book
            {
                id = model.Id,
                name = model.Name.Escape().Trim(),
                number = model.Number,
                catid = model.CategoryId
            };

            if (model.Id == 0)
            {
                try
                {
                    _dal.BeginTransaction();

                    long ret = bRepo.Insert(book);
                    if (ret == 0)
                        throw new Exception("Book was not inserted.");

                    long id = bRepo.GetLastInsertId();

                    SaveImage(id, model.Imagefile);

                    foreach(string storyName in model.Stories)
                    {
                        if(!String.IsNullOrEmpty(storyName.Trim()))
                        {
                            Story story = new Story
                            {
                                name = storyName.Escape().Trim(),
                                bookid = id
                            };

                            long sret = sRepo.Insert(story);
                            if(sret == 0)
                                throw new Exception("Story was not inserted.");
                        }
                    }

                    _dal.CommitTransaction();
                    return id;
                }
                catch(Exception ex)
                {
                    _dal.RollbackTransaction();
                    log.Error(ex);
                    throw;
                }
            }
            else
            {
                try
                {
                    _dal.BeginTransaction();

                    long ret = bRepo.Update(book);
                    if (ret == 0)
                        throw new Exception("Book was not updated.");

                    SaveImage(model.Id, model.Imagefile);

                    ret = sRepo.DeleteByBookId(model.Id);
                    log.InfoFormat("Deleted {0} stories associated with book {1}", ret, model.Id);

                    foreach (string storyName in model.Stories)
                    {
                        if (!String.IsNullOrEmpty(storyName.Trim()))
                        {
                            Story story = new Story
                            {
                                name = storyName.Escape().Trim(),
                                bookid = model.Id
                            };

                            long sret = sRepo.Insert(story);
                            if (sret == 0)
                                throw new Exception("Story was not inserted.");
                        }
                    }

                    _dal.CommitTransaction();

                    return model.Id;
                }
                catch(Exception ex)
                {
                    _dal.RollbackTransaction();
                    log.Error(ex);
                    throw;
                }
            }
        }