Example #1
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;
                }
            }
        }
 public int Insert(Story story)
 {
     return _dal.Insert<Story>("insertStory", story);
 }
 public int Update(Story story)
 {
     return _dal.Update<Story>("updateStory", story);
 }
 public int Delete(Story story)
 {
     return _dal.Delete<Story>("deleteStory", story);
 }