Exemple #1
0
        /// <summary>
        /// Get the copy with given barcode.
        /// </summary>
        /// <param name="barcode">The barcode of the copy.</param>
        /// <returns>Returns a CopyViewModel.</returns>
        /// <exception cref="Services.Exceptions.DoesNotExistException">
        /// Thrown when no copy with given barcode could be found.</exception>
        /// <exception cref="Services.Exceptions.DataAccessException">
        /// Thrown when an error occurs in the data access layer.</exception>
        public static CopyViewModel GetCopyViewModel(string barcode)
        {
            var cvm = new CopyViewModel();
            var copy = new Copy();
            if (Copy.GetCopy(out copy, barcode))
            {
                if (copy == null)
                    throw new DoesNotExistException("Ett exemplar med angiven streckkod finns inte i databasen.");

                cvm = Mapper.Map<CopyViewModel>(copy);
            }
            else
                throw new DataAccessException("Oväntat fel när ett exemplar skulle hämtas.");

            return cvm;
        }
Exemple #2
0
        /// <summary>
        /// Update or insert a copy in the database.
        /// </summary>
        /// <param name="copyViewModel">The copy to upsert.</param>
        /// <param name="overwriteExisting">If a copy with given barcode already exists: only overwrite it if
        /// overwriteExisting is set to true.</param>
        /// <exception cref="Services.Exceptions.AlreadyExistsException">
        /// Thrown when overwriteExisting is set to false and there's already a copy with given barcode in
        /// the database.</exception>
        /// <exception cref="Services.Exceptions.DataAccessException">
        /// Thrown when an error occurs in the data access layer.</exception>
        public static void Upsert(CopyViewModel copyViewModel, bool overwriteExisting)
        {
            Copy tmp;
            if (!overwriteExisting)
            {
                if (!Copy.GetCopy(out tmp, copyViewModel.BarCode))
                    throw new DataAccessException("Oväntat fel när en kopia skulle hämtas.");
                if (tmp != null)
                    throw new AlreadyExistsException("Har man sett! Ett exemplar med denna streckkod finns redan.");
            }

            Copy copy = Mapper.Map<Copy>(copyViewModel);
            if (!Copy.Upsert(copy))
            {
                throw new DataAccessException("Oväntat fel när ett exemplar skulle skapas eller uppdateras.");
            }
        }
Exemple #3
0
        public ActionResult Copy(string isbn, string barcode)
        {
            var errors = new List<string>();

            // We must have an ISBN number.
            if (isbn == null)
            {
                TempData["error"] = "Kan endast skapa exemplar av existerande böcker.";
                return RedirectToAction("AdminPage", "Account");
            }

            // Is this ISBN valid?
            try
            {
                BookServices.GetBookDetails(isbn);
            }
            catch (DoesNotExistException e)
            {
                TempData["error"] = e.Message;
                return RedirectToAction("AdminPage", "Account");
            }
            catch (DataAccessException e)
            {
                TempData["error"] = e.Message;
                return RedirectToAction("AdminPage", "Account");
            }

            CopyViewModel cvm = new CopyViewModel();

            // If a barcode was given; is it valid?
            if (barcode != null)
            {
                try
                {
                    cvm = CopyServices.GetCopyViewModel(barcode);
                    cvm.Update = true;
                }
                catch (DoesNotExistException e) { errors.Add(e.Message); }
                catch (DataAccessException e) { errors.Add(e.Message); }
            }

            cvm.ISBN = isbn;
            string err = setCopyViewLists(cvm);
            if (err != null)
                errors.Add(err);

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

            return View(cvm);
        }
Exemple #4
0
        /// <summary>
        /// Populate Statuses and Title in a CopyViewModel.
        /// </summary>
        /// <param name="cvm">The CopyViewModel.</param>
        /// <returns>Returns an error message or null, if no error occured.</returns>
        private string setCopyViewLists(CopyViewModel cvm)
        {
            try
            {
                var statusDic = StatusServices.GetStatusesAsDictionary();
                cvm.Title = BookServices.GetBookDetails(cvm.ISBN).Title;
                cvm.Statuses = new SelectList(statusDic.OrderBy(x => x.Value), "Key", "Value");
            }
            catch (DataAccessException e)
            {
                if (cvm.Title == null)
                    cvm.Title = "";
                if (cvm.Statuses == null)
                    cvm.Statuses = new SelectList(new List<SelectListItem>());
                return e.Message;
            }

            return null;
        }
Exemple #5
0
        public ActionResult Copy(CopyViewModel copyInfo)
        {
            if (!ModelState.IsValid)
            {
                string err = setCopyViewLists(copyInfo);
                if (err != null)
                    ViewBag.error = err;

                return View(copyInfo);
            }

            try
            {
                CopyServices.Upsert(copyInfo, copyInfo.Update);
                return RedirectToAction("Book", new { copyInfo.ISBN });
            }
            catch (Exception e)
            {
                var errors = new List<string>();
                errors.Add(e.Message);
                string err = setCopyViewLists(copyInfo);
                if (err != null)
                    errors.Add("err");

                ViewBag.error = errors;

                return View(copyInfo);
            }

        }