Exemple #1
0
        /// <summary>
        /// Get an EditBookViewModel object from given isbn.
        /// If no book can be found, return an empty EditBookViewModel.
        /// </summary>
        /// <remarks>
        /// Does not fill out the Copies property!
        /// </remarks>
        /// <param name="isbn">ISBN as a string.</param>
        /// <returns>EditBookViewModel</returns>
        /// <exception cref="Services.Exceptions.DoesNotExistException">
        /// Thrown when a book or its authors could not be found.</exception>
        /// <exception cref="Services.Exceptions.DataAccessException">
        /// Thrown when an error occurs in the data access layer.</exception>
        public static EditBookViewModel GetEditBookViewModel(string isbn)
        {
            EditBookViewModel ebvm = new EditBookViewModel();
            Book book = new Book();

            if (!Book.GetBook(out book, isbn))
                throw new DataAccessException("Ett oväntat fel uppstod när en bok skulle hämtas.");
            if (book == null)
                throw new DoesNotExistException("Ajdå! Boken hittades inte.");

            var bookAuthors = new List<BookAuthor>();
            if (!BookAuthor.GetBookAuthors(out bookAuthors, book.ISBN))
                throw new DataAccessException("Ett oväntat fel uppstod när författare till en bok skulle hämtas.");
            if (bookAuthors.Count < 1)
                throw new DoesNotExistException("Mystiskt! Angivna författare kunde inte hittas.");

            var authorIds = new List<int>();
            foreach (BookAuthor ba in bookAuthors)
                authorIds.Add(ba.Aid);

            ebvm = Mapper.Map<EditBookViewModel>(book);
            ebvm.AuthorIds = authorIds;

            return ebvm;
        }
Exemple #2
0
        public ActionResult Book(EditBookViewModel bookInfo)
        {
            string err = setBookViewLists(bookInfo);
            bool upsertSuccess = false;
            if (err != null)
                ViewBag.error = err + "\n";

            if (ModelState.IsValid)
            {
                try
                {
                    BookServices.Upsert(bookInfo, bookInfo.Update);
                    bookInfo.Update = true;
                    upsertSuccess = true;
                }
                catch (AlreadyExistsException e) { ViewBag.error += e.Message; }
                catch (DoesNotExistException e) { ViewBag.error += e.Message; }
                catch (DataAccessException e) { ViewBag.error += e.Message; }
                catch (Exception) { ViewBag.error += "Oväntat fel."; }
            }
            if(!upsertSuccess)
                return View(bookInfo);
            else
                return RedirectToAction("Book", "Search", new { search = bookInfo.ISBN});
        }
Exemple #3
0
        public ActionResult Book(string isbn)
        {
            var errors = new List<string>();
            var bookInfo = new EditBookViewModel();

            if (isbn != null)
            {
                try
                {
                    bookInfo = BookServices.GetEditBookViewModel(isbn);
                    bookInfo.Update = true;
                }
                catch (DoesNotExistException e) { errors.Add(e.Message); }
                catch (AlreadyExistsException e) { errors.Add(e.Message); }
                catch (DataAccessException e) { errors.Add(e.Message); }
            }

            string err = setBookViewLists(bookInfo);
            if (err != null)
                errors.Add(err);

            if (errors.Count > 0)
                ViewBag.error = errors;

            return View(bookInfo);
        }
Exemple #4
0
        /// <summary>
        /// Write a book to the database.
        /// </summary>
        /// <param name="editBookViewModel">The EditBookViewModel to write to the database.</param>
        /// <param name="overwriteExisting">If a book with given ISBN already exists, then overwrite it only
        /// if overwriteExisting is set to true.</param>
        /// <exception cref="Services.Exceptions.AlreadyExistsException">
        /// Thrown when the item already exists in the database and overwriteExisting is set to false.</exception>
        /// <exception cref="Services.Exceptions.DoesNotExistException">
        /// Thrown when the author ids in the viewmodel are invalid.</exception>
        /// <exception cref="Services.Exceptions.DataAccessException">
        /// Thrown when an error occurs in the data access layer.</exception>
        public static void Upsert(EditBookViewModel editBookViewModel, bool overwriteExisting)
        {
            if (!overwriteExisting)
            {
                Book tmp;
                Book.GetBook(out tmp, editBookViewModel.ISBN);
                if (tmp != null)
                {
                    throw new AlreadyExistsException("Aj, aj! En bok med angivet ISBN finns redan!");
                }
            }

            Book book = Mapper.Map<Book>(editBookViewModel);
            bool success = true;
            try
            {
                success = Book.Upsert(book, editBookViewModel.AuthorIds);
            }
            catch (ArgumentException e)
            {
                if (e.ParamName == "authorIdList")
                    throw new DoesNotExistException("De angivna författarna finns inte i databasen.");
                else
                    throw;
            }

            if (!success)
                throw new DataAccessException("Tusan! Något gick fel när en bok skulle skapas eller uppdateras. Kontakta en administratör om felet kvarstår.");
        }
Exemple #5
0
        /// <summary>
        /// Populate lists in an EditBookViewModel.
        /// </summary>
        /// <param name="ebvm">The EditBookViewModel.</param>
        /// <returns>Returns an error message or null, if no error occured.</returns>
        private string setBookViewLists(EditBookViewModel ebvm)
        {
            try
            {
                if (ebvm.ISBN != null)
                    ebvm.Copies = CopyServices.getCopyViewModels(ebvm.ISBN);

                var classDic = ClassificationServices.GetClassificationsAsDictionary();
                var authorDic = AuthorServices.GetAuthorsAsDictionary();
                ebvm.Classifications = new SelectList(classDic.OrderBy(x => x.Value), "Key", "Value");
                ebvm.Authors = new SelectList(authorDic.OrderBy(x => x.Value), "Key", "Value");
            }
            catch (DataAccessException e)
            {
                if (ebvm.Copies == null) ebvm.Copies = new List<CopyViewModel>();
                if (ebvm.Classifications == null) ebvm.Classifications = new SelectList(new List<SelectListItem>());
                if (ebvm.Authors == null) ebvm.Authors = new SelectList(new List<SelectListItem>());
                return e.Message;
            }

            return null;
        }