/// <summary>
        /// Initializes a new instance of the <see cref="Service"/> class.
        /// </summary>
        public Service()
        {
            string solutionDirectory = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetAssembly(this.GetType()).Location))));

            Threshold = GetThresholdFromJSON(solutionDirectory);

            ErrorsHandler = new ErrorsHandler();
        }
        /// <summary>
        /// Inserts a new Book object in the database.
        /// </summary>
        /// <param name="book">The book.</param>
        /// <returns>True or false.</returns>
        public bool AddNewBook(Book book)
        {
            if (IsValidBook(book))
            {
                bookRepository.Insert(book);
                return(true);
            }
            else
            {
                ErrorsHandler.PrintErrors();
            }

            return(false);
        }
        /// <summary>
        /// Validates the given Book object.
        /// </summary>
        /// <param name="book">The book.</param>
        /// <returns>True or False.</returns>
        public bool IsValidBook(Book book)
        {
            bool isValid = true;

            ValidationContext        context           = new ValidationContext(book);
            IList <ValidationResult> validationResults = new List <ValidationResult>();

            isValid = Validator.TryValidateObject(book, context, validationResults, true);

            if (validationResults.Count != 0)
            {
                foreach (ValidationResult result in validationResults)
                {
                    ErrorsHandler.Add(result.ErrorMessage);
                }

                isValid = false;
            }

            if (!isValid)
            {
                return(false);
            }

            if (book.Domains.Count > Threshold.MaxDomains)
            {
                ErrorsHandler.Add(ValidationErrors.TooManyDomains);
                isValid = false;
            }

            if (!isValid)
            {
                return(false);
            }

            if (AreRelatedDomains(book.Domains))
            {
                ErrorsHandler.Add(ValidationErrors.DomainAreRelated);
                isValid = false;
            }

            return(isValid);
        }
Beispiel #4
0
        /// <summary>
        /// Validates the given Loan object.
        /// </summary>
        /// <param name="loan">The loan.</param>
        /// <returns>True or False.</returns>
        public bool IsValidLoan(Loan loan)
        {
            bool isValid = true;

            ValidationContext        context           = new ValidationContext(loan);
            IList <ValidationResult> validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(loan, context, validationResults, true);

            if (validationResults.Count != 0)
            {
                foreach (ValidationResult result in validationResults)
                {
                    ErrorsHandler.Add(result.ErrorMessage);
                }

                isValid = false;
            }

            ////if (!isValid)
            ////{
            ////    return false;
            ////}

            ////if(loan.BorrowedEditions.Count > Threshold.MaxBooks)
            ////{
            ////    ErrorsHandler.Add(ValidationErrors.TooManyBooks);
            ////    isValid = false;
            ////} else
            ////{
            ////    if(loan.BorrowedEditions.Count >= 3)
            ////    {
            ////        BookService service = new BookService();
            ////        ContainsMoreThatTwoDomains(loan.BorrowedEditions);
            ////    }
            ////}

            foreach (Edition edition in loan.BorrowedEditions)
            {
                if (edition.NoForLoan == 0)
                {
                    ErrorsHandler.Add(ValidationErrors.NoBooksForLoan);
                    isValid = false;
                    break;
                }
                else
                {
                    if (edition.NoForLoan < edition.NoTotal * 0.1)
                    {
                        ErrorsHandler.Add(ValidationErrors.TooFewBooks);
                        isValid = false;
                        break;
                    }
                }
            }

            ////if (!isValid)
            ////{
            ////    return false;
            ////}

            return(isValid);
        }