Example #1
0
        public async Task <IActionResult> Create(BookVm bookVm)
        {
            if (ModelState.IsValid)
            {
                string webRootPath = _hostEnvironment.WebRootPath;
                var    files       = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    string fileName   = Guid.NewGuid().ToString();
                    var    uploads    = Path.Combine(webRootPath, @"images\books");
                    var    extenstion = Path.GetExtension(files[0].FileName);

                    if (bookVm.Book.ImageUrl != null)
                    {
                        //EDIT IMAGE
                        var imagePath = Path.Combine(webRootPath, bookVm.Book.ImageUrl.TrimStart('\\'));
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }
                    }
                    using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create))
                    {
                        files[0].CopyTo(filesStreams);
                    }
                    bookVm.Book.ImageUrl = @"\images\books\" + fileName + extenstion;
                }
                else
                {
                    //UPDATE WHEN WE DON'T CHANGE AN IMAGE
                    if (bookVm.Book.Id != 0)
                    {
                        var objFromDb = _bookRepository.GetById(bookVm.Book.Id).Result;
                        bookVm.Book.ImageUrl = objFromDb.ImageUrl;
                    }
                }
                if (bookVm.Book.Id == 0)
                {
                    await _bookRepository.Insert(bookVm.Book);
                }
                else
                {
                    await _bookRepository.Update(bookVm.Book);
                }
                return(RedirectToAction(nameof(Index)));
            }

            IEnumerable <Category> catList = await _categoryRepository.GetAll().ToListAsync();

            bookVm.CategoryList = catList.Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.Id.ToString()
            });
            if (bookVm.Book.Id != 0)
            {
                bookVm.Book = await _bookRepository.GetById(bookVm.Book.Id);
            }
            return(View(bookVm));
        }
        /// <summary>
        /// 获取一本书籍的全部信息
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        public BookVm GetBook(long bookid, string userid)
        {
            using (var db = this.OpenDb())
            {
                CreateTableIfNotExist();

                var hasPermission = db.Query <bool>(@"
select 1 from books a where is_public=1 
or exists(select 1 from book_owner b where b.book_id = a.id and user_id = @user_id)",
                                                    new { user_id = userid }).FirstOrDefault();

                if (!hasPermission)
                {
                    throw new Exception("你无权查看");
                }

                var book        = db.Query <Book>("select * from books where id=@id", new { id = bookid }).Single();
                var directories = db.Query <BookDirectory>("select * from book_directories where book_id = @book_id order by parent_id, seq", new { book_id = bookid }).ToList();
                var owner       = db.Query <BookOwner>("select * from book_owner where book_id = @id", new { id = bookid }).ToList();
                var result      = new BookVm {
                    Book = book, BookDirectory = directories, BookOwner = owner
                };

                return(result);
            }
        }
        public TextManageViewModel(IRegionManager regionManager, IPublisherService publisherService,
                                   ITextService textService)
        {
            mRegionManager    = regionManager;
            mPublisherService = publisherService;
            mTextService      = textService;
            var books = mPublisherService.GetPublishersAndBooks();

            foreach (var p in books)
            {
                foreach (var b in p.Books)
                {
                    var book = new BookVm
                    {
                        Title     = b.Title,
                        Id        = b.Id,
                        Publisher = p.Title
                    };
                    Books.Add(book);
                }
            }
            var texts = mTextService.GetTexts();

            foreach (var text in texts)
            {
                var book = Books.FirstOrDefault(a => a.Id == text.BookId);
                book?.Texts.Add(new TextVm
                {
                    Id    = text.Id,
                    Title = text.Title
                });
            }
        }
Example #4
0
        public async Task <IActionResult> Create(int?id)
        {
            IEnumerable <Category> catList = await _categoryRepository.GetAll().ToListAsync();

            BookVm bookVm = new BookVm()
            {
                Book         = new Book(),
                CategoryList = catList.Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                })
            };

            if (id == null)
            {
                //CREATE
                return(View(bookVm));
            }
            //EDIT
            bookVm.Book = await _bookRepository.GetById(id.GetValueOrDefault());

            if (bookVm.Book == null)
            {
                return(NotFound());
            }
            return(View(bookVm));
        }
Example #5
0
        public ActionResult Create()
        {
            var model = new BookVm();

            model.LanguageListItem   = GetLanguagelist();
            model.DepartmentListItem = GetDepartmentlist();
            return(View(model));
        }
Example #6
0
 public IActionResult Create(BookVm form)
 {
     if (ModelState.IsValid)
     {
         Book formCo = _mapper.Map <Book>(form);
         _unitOfWork.Books.Add(formCo);
         _unitOfWork.Complate();
         TempData["Message"] = "Sikeres hozzáadás!";
         return(RedirectToAction(nameof(Index)));
     }
     return(View(form));
 }
Example #7
0
    public void Display()
    {
        IEnumerable <Book>            books   = DataService.FetchBooks();
        ObservableCollection <BookVm> bookVms = new ObservableCollection <BookVm>();

        foreach (var book in books)
        {
            BookVm bookVm = new BookVm(book);
            bookVms.Add(bookVm);
        }
        //Get the View and then bind using the bookVms collection
    }
Example #8
0
 public ActionResult Create(BookVm model)
 {
     if (ModelState.IsValid)
     {
         var  books  = Mapper.Map <Book>(model);
         bool isSave = _bookManager.Add(books);
         if (isSave)
         {
             return(RedirectToAction("Index"));
         }
     }
     return(View());
 }
        public ApiResponse Post([FromBody] BookVm bookVm)
        {
            try
            {
                return(testBs.AddBook(_uow, bookVm, mapper));
            }
            catch (Exception)
            {
                return(ApiResponse.Exception());

                throw;
            }
        }
Example #10
0
        public IActionResult Edit(BookVm form)
        {
            if (ModelState.IsValid)
            {
                Book bookCo = _mapper.Map <Book>(form);
                _unitOfWork.Books.Update(bookCo);
                _unitOfWork.Complate();

                TempData["Message"] = "A változtatásokat sikeresen elmentettük!";
                return(RedirectToAction(nameof(Index)));
            }
            TempData["Message"] = "Mentés sikertelen!";
            return(RedirectToAction(nameof(Create), form.Id));
        }
Example #11
0
 public ActionResult Edit(BookVm model)
 {
     if (ModelState.IsValid)
     {
         var  books    = Mapper.Map <Book>(model);
         bool isUpdate = _bookManager.Update(books);
         if (isUpdate)
         {
             return(RedirectToAction("Index"));
         }
         model.LanguageListItem   = GetLanguagelist();
         model.DepartmentListItem = GetDepartmentlist();
     }
     return(View());
 }
Example #12
0
 public IHttpActionResult Remove(BookVm bookVm)
 {
     try
     {
         _bookBLL.Remove(bookVm.Id);
         return(Ok());
     }
     catch (BusinessLogicException ex)
     {
         return(Content(HttpStatusCode.BadRequest, new ErrorResult(ex.Message)));
     }
     catch (Exception ex)
     {
         return(Content(HttpStatusCode.InternalServerError, new ErrorResult(Resources.Messages.Status500)));
     }
 }
        public async Task <BookVm> GetBookById(string id)
        {
            BookVm apiResult = null;
            var    response  = await client.GetAsync("api/books/GetByBookId/" + id);

            if (response.IsSuccessStatusCode)
            {
                var stream = await response.Content.ReadAsStreamAsync();

                using (var reader = new StreamReader(stream))
                {
                    apiResult = JsonConvert.DeserializeObject <BookVm>(reader.ReadToEnd());
                }
            }
            return(apiResult);
        }
Example #14
0
        /// <summary>
        /// 返回一个目录的顺序结构,用于前台导航
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        private List <long> GetBookDirectoryNavigator(BookVm book)
        {
            var navigator = new List <long>();

            var bookDir = book.BookDirectory
                          .OrderBy(t => t.parent_id)
                          .ThenBy(t => t.seq)
                          .ThenBy(t => t.id)
                          .ToList();

            var first = bookDir.FirstOrDefault();

            if (first != null)
            {
                navigator.Add(first.id);
                WalkDirectory(bookDir, navigator, first);
            }

            return(navigator);
        }
Example #15
0
        public IHttpActionResult Edit(BookVm bookVm)
        {
            try
            {
                var book = _bookBLL.Get(bookVm.Id);
                book = MapToBook(bookVm, book);
                _bookBLL.Edit(book);

                var bookDto = MapToBookDto(book);
                return(Ok(bookDto));
            }
            catch (BusinessLogicException ex)
            {
                return(Content(HttpStatusCode.BadRequest, new ErrorResult(ex.Message)));
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.InternalServerError, new ErrorResult(Resources.Messages.Status500)));
            }
        }
Example #16
0
        public async Task <ActionResult <Book> > UpdateBook(int id, BookVm book)
        {
            var result = await context.Book.GetFirstOrDefault(item => item.Id == id);

            if (result == null)
            {
                return(NotFound());
            }

            result.Name           = book.Name;
            result.Code           = book.Code;
            result.NumberOfCopies = Convert.ToInt32(book.NumberOfCopies);
            result.Author         = book.Author;
            result.Publication    = book.Publication;
            result.Description    = book.Description;
            result.UpdatedDate    = System.DateTime.Now;
            await context.Save();

            return(result);
        }
Example #17
0
        public async Task <ActionResult <Book> > PostBook(BookVm bookVm)
        {
            if (bookVm == null)
            {
                return(BadRequest());
            }

            Book book = new Book()
            {
                Name              = bookVm.Name,
                Code              = bookVm.Code,
                NumberOfCopies    = Convert.ToInt32(bookVm.NumberOfCopies),
                Author            = bookVm.Author,
                Publication       = bookVm.Publication,
                Description       = bookVm.Description,
                AvailableQuantity = Convert.ToInt32(bookVm.NumberOfCopies),
            };

            context.Book.Save(book);
            await context.Save();

            return(book);
        }
        public ApiResponse AddBook(UnitOfWork uow, BookVm bookVm, IMapper mapper)
        {
            try
            {
                Book book = new Book();
                mapper.Map(bookVm, book);
                uow.CreateTransaction();
                uow.GenericRepository <Book>().Insert(book);
                uow.Save();
                uow.Commit();

                _response.Data    = null;
                _response.Message = "Successfully inserted ...!";
                _response.Status  = 200;

                return(_response);
            }
            catch (Exception ex)
            {
                uow.Rollback();
                ApiResponse.Exception();
                throw;
            }
        }
Example #19
0
        private Book MapToBook(BookVm bookVm, Book bookEdit = null)
        {
            var book      = bookEdit ?? new Book();
            var publisher = _publisherBLL.Get(bookVm.PublisherId);

            book.Authors.Clear();
            if (bookVm.AuthorsIds != null)
            {
                foreach (var autorId in bookVm.AuthorsIds)
                {
                    var autor = _authorBLL.Get(autorId);
                    book.Authors.Add(autor);
                }
            }

            book.Id          = bookVm.Id;
            book.Description = bookVm.Description;
            book.Price       = bookVm.Price;
            book.PublisherId = publisher?.Id ?? 0;
            book.Quantity    = bookVm.Quantity;
            book.Title       = bookVm.Title;

            return(book);
        }