//Add a “ExtendDueDateForBorrowByID()” method that will extend the “DueDate” by 7 days from today.
        //An extension must actually extend the due date in order to be valid. Overdue books cannot be extended. Books cannot be extended more than 3 times.

        public static void ExtendDueDateForBorrowByID(int id)
        {
            var exception = new ValidationExceptions();

            using (var context = new LibraryContext())
            {
                var extendBook = context.Borrows.Where(x => x.BookID == id).SingleOrDefault();

                // A book can only be extended a maximum of 3 times.
                //Thanks Aaron.B for helping me correcting my logic.The Book can be extended once in a day and Can be extended maximum 3 times.If user extend the book it will extend it and set due date to be 14 days from Todays date;Note:Extension will be updated Next day becuase of DateTime.Today,So We can test this by manually changing the due date to 18/11/2020 For Example in database we changed Due date to be 18/11 and hit Extend it will increase the Extension Count by 1.Please check the image folder for test result

                if (extendBook.ExtensionCount < 3 && extendBook.DueDate < DateTime.Today.AddDays(14))
                {
                    //Extend the due date
                    extendBook.DueDate = DateTime.Today.AddDays(14);
                    //Extend by one more time
                    extendBook.ExtensionCount += 1;
                    context.SaveChanges();
                }
                //  if extension is morethan 3  donot extend and throw Exception
                else if (extendBook.ExtensionCount == 3)
                {
                    exception.newExceptions.Add(
                        new Exception("Maximum Limit of Extensions Reached,Please Return the Book"));
                    throw exception;
                }
                else if (DateTime.Compare(DateTime.Today, extendBook.DueDate) > 0)
                {
                    // Overdue books cannot be extended.
                    exception.newExceptions.Add(new Exception(
                                                    "Please Note: Book is Overdue and cannot be extended, Please return the Book and try again later."));
                    throw exception;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Seal parcel after modification
        /// May call this method on parcels in either Creating or Modifying states.
        /// Bank is used to create the ReplicationVersionInfo which is depends on store/bank implementation.
        /// Parcels can replicate only within stores/technologies that have sealed them
        /// </summary>
        public void Seal(IBank bank)
        {
            if (bank == null)
            {
                throw new DistributedDataAccessException(StringConsts.ARGUMENT_ERROR + "Parcel.Seal(bank==null)");
            }

            if (m_State != ParcelState.Creating && m_State != ParcelState.Modifying)
            {
                throw new DistributedDataAccessException(StringConsts.DISTRIBUTED_DATA_PARCEL_INVALID_OPERATION_ERROR.Args("Seal", GetType().FullName, m_State));
            }

            Validate(bank);

            if (ValidationExceptions.Any())
            {
                throw ParcelSealValidationException.ForErrors(GetType().FullName, ValidationExceptions);
            }


            m_ReplicationVersionInfo = bank.GenerateReplicationVersionInfo(this);

            DoSeal(bank);

            m_State       = ParcelState.Sealed;
            m_NewlySealed = true;
        }
Ejemplo n.º 3
0
        public void ExtendDueDateForBookByID(int id)
        {
            ValidationExceptions exception = new ValidationExceptions();
            Book target;

            using (LibraryContext context = new LibraryContext())
            {
                target = context.Books.Where(x => x.ID == id).Single();

                // A book can only be extended a maximum of 3 times.
                if (target.ExtensionCount < 3 && DateTime.Compare(DateTime.Now, target.DueDate) < 0)
                {
                    target.DueDate         = DateTime.Now.AddDays(7);
                    target.ExtensionCount += 1;
                    context.SaveChanges();
                }
                else
                if (target.ExtensionCount == 3)
                {
                    // If a user tries to extend a book a fourth time do not update the database
                    // Display an error on the page calling the method informing the user they will have to speak to the librarian.
                    exception.SubExceptions.Add(new Exception("Cannot extend book more than four times. Kindly speak to a librarian."));
                    throw exception;
                }
                else if (DateTime.Compare(DateTime.Now, target.DueDate) > 0)
                {
                    // Overdue books cannot be extended.
                    // Display an error on the page calling the method informing the user they will have to speak to the librarian.
                    exception.SubExceptions.Add(new Exception("Overdue book cannot be extended. Kindly speak to a librarian."));
                    throw exception;
                }
            }
        }
        private IEnumerable <Message> GetErrors(ValidationExceptions exception)
        {
            IList <Message> messages = new List <Message>();

            //  foreach(ValidationResult res in exception.)
            foreach (var result in exception.validationExceptionList)
            {
                Message msg = new Message(string.Empty, result.Message + result.InnerException);
                messages.Add(msg);
            }

            //foreach (ValidationResult validation in exception.)
            //{
            //    foreach (Validation propertyError in v)
            //    {
            //        Message msg = new Message(string.Empty,
            //            propertyError.ErrorMessage,
            //            propertyError.ErrorMessage,
            //            new List<string> { propertyError.PropertyName });

            //        messages.Add(msg);
            //    }
            //}

            return(messages.AsEnumerable <Message>());
        }
Ejemplo n.º 5
0
 private void addValidationException(bool isException, Exception ex)
 {
     if (isException)
     {
         ValidationExceptions.Add(ex);
         Callbacks.CallbackException(ex);
     }
 }
        public ActionResult CreateProject(string projectName, string dueDate, string employeeID)
        {
            ValidationExceptions exceptions = new ValidationExceptions();

            projectName = projectName != null?projectName.Trim() : null;

            dueDate = dueDate != null?dueDate.Trim() : null;

            employeeID = employeeID != null?employeeID.Trim() : null;

            if (string.IsNullOrEmpty(projectName))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must include project name."));
            }

            if (string.IsNullOrEmpty(dueDate))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must include due date."));
            }
            else
            if (!DateTime.TryParse(dueDate, out DateTime _dueDate))
            {
                exceptions.SubExceptions.Add(new ArgumentException("The date format is not correct."));
            }
            else
            if (DateTime.Compare(_dueDate, DateTime.Today) < 0)
            {
                exceptions.SubExceptions.Add(new ArgumentException("The due date has to be in the future."));
            }


            if (string.IsNullOrEmpty(employeeID))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Employee ID must be included to assign the project."));
            }
            else
            if (!int.TryParse(employeeID, out int _employeeID))
            {
                exceptions.SubExceptions.Add(new ArgumentException("The employee ID must be in number format."));
            }

            if (exceptions.SubExceptions.Count > 0)
            {
                return(UnprocessableEntity(new { errors = exceptions.SubExceptions.Select(x => x.Message) }));
            }
            else
            {
                try
                {
                    new ProjectController().CreateProject(projectName, DateTime.Parse(dueDate), int.Parse(employeeID));
                    return(StatusCode(200, $"Project {projectName} succesfully created. It has deadline of {dueDate} and it is assigned to student with ID of {employeeID}"));
                }
                catch (ValidationExceptions e)
                {
                    return(UnprocessableEntity(new { errors = e.SubExceptions.Select(x => x.Message) }));
                }
            }
        }
Ejemplo n.º 7
0
        // These methods are for data management.Ensure that “CreateBook()” is no longer accepting an ID, as it is database generated

        public Book CreateBook(string title, int authorID, string publicationDate)
        {
            var exception = new ValidationExceptions();

            //Trimmed all data prior to processing.,NOT NULL fields must have values that are not whitespace.

            if (string.IsNullOrWhiteSpace(title))
            {
                exception.newExceptions.Add(new Exception("The title cannot be empty."));
            }
            else

            //   String data cannot exceed its database size.//All comparison validation must be case insensitive.Book titles must be unique for that author.
            {
                if (title.Trim().Length > 30)
                {
                    exception.newExceptions.Add(new Exception("The Maximum Length of Title is 30 Characters"));
                }
                else if (GetBooks().Any(x => x.Title.ToLower() == title.Trim().ToLower()))
                {
                    exception.newExceptions.Add(new Exception("Book Title already Exists,Only New Books allowed "));
                }
            }

            /*“CheckedOutDate” cannot be prior to “PublicationDate”. “ReturnedDate” cannot be prior to “CheckedOutDate” ; PublishedDate” cannot be in the future  */

            if (publicationDate == null)
            {
                exception.newExceptions.Add(new Exception("Publication Date is Required to Proceed Further."));
            }
            else if (DateTime.Compare(DateTime.Parse(publicationDate), DateTime.Today) > 0)
            {
                exception.newExceptions.Add(new Exception("Publication date cannot be in the future."));
            }

            if (exception.newExceptions.Count > 0)
            {
                throw exception;
            }

            //Create a Book
            var newBook = new Book
            {
                Title = title.Trim().ToLower(),

                AuthorID = authorID,

                PublicationDate = DateTime.Parse(publicationDate)
            };

            using (var context = new LibraryContext())
            {
                context.Books.Add(newBook);
                context.SaveChanges();
            }

            return(newBook);
        }
        public ActionResult CreateProjectByCohort(string projectName, string dueDate, string cohort, string isCohortProject)
        {
            isCohortProject = isCohortProject != null?isCohortProject.Trim().ToLower() : null;

            projectName = projectName != null?projectName.Trim() : null;

            dueDate = dueDate != null?dueDate.Trim() : null;

            cohort = cohort != null?cohort.Trim() : null;

            ValidationExceptions exceptions = new ValidationExceptions();

            if (string.IsNullOrEmpty(projectName))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must include project name."));
            }

            if (string.IsNullOrEmpty(dueDate))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must include due date."));
            }
            else
            if (!DateTime.TryParse(dueDate, out DateTime _dueDate))
            {
                exceptions.SubExceptions.Add(new ArgumentException("The date format is not correct."));
            }
            else
            if (DateTime.Compare(_dueDate, DateTime.Today) < 0)
            {
                exceptions.SubExceptions.Add(new ArgumentException("The due date has to be in the future."));
            }

            if (string.IsNullOrEmpty(cohort))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must enter a cohort number."));
            }
            else
            if (!float.TryParse(cohort, out float _cohort))
            {
                exceptions.SubExceptions.Add(new ArgumentException("Must enter a proper format."));
            }

            if (exceptions.SubExceptions.Count > 0)
            {
                return(UnprocessableEntity(new { errors = exceptions.SubExceptions.Select(x => x.Message) }));
            }
            else
            if (bool.TryParse(isCohortProject, out bool _isCohortProject))
            {
                new ProjectController().CreateProjectForCohort(projectName, DateTime.Parse(dueDate), float.Parse(cohort));
                return(StatusCode(200, $"Project {projectName} succesfully created. It has deadline of {dueDate} and it is assigned to cohort {cohort}"));
            }
            else
            {
                return(StatusCode(400, "Must enter either true or false."));
            }
        }
        // This template is from 4.1-ReactAPI project @link: https://github.com/TECHCareers-by-Manpower/4.1-ReactAPI/tree/master/Controllers
        // Create project for individual person
        // Create project with parameters, project name, due date, date creation, employee_id
        public int CreateProject(string projectName, DateTime dueDate, int employeeID)
        {
            int target;

            // Create Validation Exception to catch any exceptions that is related to database error such as taken email, taken id, and taken name.
            ValidationExceptions exception = new ValidationExceptions();

            using (TimesheetContext context = new TimesheetContext())
            {
                // The project cannot be assigned to employee twice
                // Only instructor can make projects and assign projects
                // Example: JavaScript Assignment cannot be assigned twice
                // Any employee that have that project assigned, the project cannot be created and an response will be created.

                // Find the employee first
                if (!context.Employees.Any(x => x.ID == employeeID))
                {
                    exception.SubExceptions.Add(new ArgumentException($"The student with ID of {employeeID} cannot be found in the database."));
                }
                else
                if (context.Projects.Where(x => x.EmployeeID == employeeID).Any(x => x.ProjectName == projectName))
                {
                    // If the employee alreaddy has a project that matches the project being assigned, throw error
                    exception.SubExceptions.Add(new ArgumentException($"This project, {projectName} has been assigned to this student with ID: {employeeID}."));
                }

                if (exception.SubExceptions.Count > 0)
                {
                    throw exception;
                }
                else
                {
                    // If no exceptions are thrown, the project will be created for the student, and will return a project ID integer.
                    Project newProject = new Project()
                    {
                        ProjectName = projectName,
                        DueDate     = dueDate,
                        EmployeeID  = employeeID,
                        DateCreated = DateTime.Now
                    };

                    context.Add(newProject);
                    context.SaveChanges();
                    target = newProject.ID;
                }
            }
            // This will return a projectID number.
            return(target);
        }
Ejemplo n.º 10
0
        public string GetErrorMessageString(string separator, bool includeExceptionMessages = false)
        {
            separator = separator ?? "";
            var errorString = string.Join(separator, ErrorMessages.ToArray());

            if (includeExceptionMessages)
            {
                if (!string.IsNullOrWhiteSpace(errorString))
                {
                    errorString += separator;
                }

                errorString += string.Join(separator, ValidationExceptions.Select(x => x.Message).ToArray());
            }

            return(errorString);
        }
Ejemplo n.º 11
0
        // Add a “ReturnBookByID()” method.
        // Set the returned date of the specified book to today’s date.
        // Overdue books cannot be returned.
        // Display an error on the page calling the method informing the user they will have to speak to the librarian.
        public void ReturnBookByID(int id)
        {
            Book target;
            ValidationExceptions exception = new ValidationExceptions();

            using (LibraryContext context = new LibraryContext())
            {
                target = context.Books.Where(x => x.ID == id).Single();

                if (DateTime.Compare(DateTime.Now, target.DueDate) < 0)
                {
                    target.ReturnedDate = DateTime.Now;
                    target.DueDate      = DateTime.Now;
                    context.SaveChanges();
                }
                else
                {
                    exception.SubExceptions.Add(new Exception("Cannot return book, kindly speak to a librarian."));
                    throw exception;
                }
            }
        }
        //Add a “ReturnBorrowByID()” method that will set the “Borrow”s “ReturnedDate” to today.//“ReturnedDate” cannot be prior to “CheckedOutDate”.

        public static void ReturnBorrowByID(int id)
        {
            var exception = new ValidationExceptions();

            using (var context = new LibraryContext())
            {
                {
                    var returnBook = context.Borrows.Where(x => x.BookID == id).SingleOrDefault();
                    if (DateTime.Compare(DateTime.Now, returnBook.DueDate) < 0)
                    {
                        returnBook.ReturnedDate = DateTime.Now;
                        returnBook.DueDate      = DateTime.Now;
                        context.SaveChanges();
                    }
                    else
                    {
                        exception.newExceptions.Add(new Exception("Cannot return book,Please visit Reception Area."));
                        throw exception;
                    }
                }
            }
        }
Ejemplo n.º 13
0
        /*
         * Modify “CreateBook()”.
         *      Check that “Title” is unique before saving books to the database.
         *      If “Title” is not unique do not add the new book.
         *      Ensure this comparison is case insensitive and trimmed.
         *
         */
        public Book CreateBook(string title, int authorID, string publicationDate)
        {
            // Display itemized errors for every field that has an issue.
            ValidationExceptions exception = new ValidationExceptions();

            // “Title” cannot be empty or whitespace.
            if (string.IsNullOrWhiteSpace(title))
            {
                exception.SubExceptions.Add(new Exception("The title cannot be empty."));
            }
            else // “Title” cannot exceed its size in the database.
            if (title.Trim().Length > 30)
            {
                exception.SubExceptions.Add(new Exception("The title cannot exceed 30 characters."));
            }
            else
            if (GetBooks().Any(x => x.Title.ToLower() == title.Trim().ToLower()))
            {   // Ensure this comparison is case insensitive and trimmed.
                // If “Title” is not unique do not add the new book.
                exception.SubExceptions.Add(new Exception("This title is already recorded in the database. Please add new title."));
            }

            // “PublishedDate” cannot be in the future
            if (publicationDate == null)
            {
                exception.SubExceptions.Add(new Exception("Please insert a publication date."));
            }
            else
            if (DateTime.Compare(DateTime.Parse(publicationDate), DateTime.Now) > 0)
            {
                exception.SubExceptions.Add(new Exception("Publication date cannot be in the future."));
            }

            if (exception.SubExceptions.Count > 0)
            {
                throw exception;
            }

            Book newBook = new Book()
            {
                // Trim “Title” before saving it’s value.
                Title           = title.Trim(),
                AuthorID        = authorID,
                PublicationDate = DateTime.Parse(publicationDate),

                // Set “CheckedOutDate” to today’s date.
                CheckedOutDate = DateTime.Now,

                // Keep the logic to set DueDate and ReturnedDate.
                DueDate      = DateTime.Now.AddDays(14),
                ReturnedDate = null,

                // Set “ExtensionCount” to 0.
                ExtensionCount = 0
            };

            using (LibraryContext context = new LibraryContext())
            {
                context.Books.Add(newBook);
                context.SaveChanges();
            }

            return(newBook);
        }
        public ActionResult <ProjectDTO> UpdateProject(string projectID, string design, string doing, string codeReview, string testing, string deliverables)
        {
            ActionResult <ProjectDTO> response;
            ValidationExceptions      exceptions = new ValidationExceptions();

            projectID = projectID != null?projectID.Trim().ToLower() : null;

            design = design != null?design.Trim().ToLower() : "0";

            doing = doing != null?doing.Trim().ToLower() : "0";

            codeReview = codeReview != null?codeReview.Trim().ToLower() : "0";

            testing = testing != null?testing.Trim().ToLower() : "0";

            deliverables = deliverables != null?deliverables.Trim().ToLower() : "0";

            if (string.IsNullOrEmpty(projectID))
            {
                response = StatusCode(400, "Project must have ID in order to update the hours.");
            }
            else
            if (!int.TryParse(projectID, out int _projectID))
            {
                response = StatusCode(400, "Project ID must be in number format.");
            }
            else
            {
                if (!float.TryParse(design, out float _design))
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be number format. Example: 0.25."));
                }
                else
                if (_design % 0.25 != 0)
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be quarterly format of 0.25, 0.50, 0.75, or 1.00 and above."));
                }

                if (!float.TryParse(doing, out float _doing))
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be number format. Example: 0.25."));
                }
                else
                if (_doing % 0.25 != 0)
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be quarterly format of 0.25, 0.50, 0.75, or 1.00 and above."));
                }

                if (!float.TryParse(codeReview, out float _codeReview))
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be number format. Example: 0.25."));
                }
                else
                if (_codeReview % 0.25 != 0)
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be quaterly format of 0.25, 0.50, 0.75, or 1.00 and above."));
                }

                if (!float.TryParse(testing, out float _testing))
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be number format. Example: 0.25."));
                }
                else
                if (_testing % 0.25 != 0)
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be quaterly format of 0.25, 0.50, 0.75, or 1.00 and above."));
                }

                if (!float.TryParse(deliverables, out float _deliverables))
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be number format. Example: 0.25."));
                }
                else
                if (_deliverables % 0.25 != 0)
                {
                    exceptions.SubExceptions.Add(new ArgumentException("Hours have to be quarterly format of 0.25, 0.50, 0.75, or 1.00 and above."));
                }

                if (exceptions.SubExceptions.Count > 0)
                {
                    response = UnprocessableEntity(new { errors = exceptions.SubExceptions.Select(x => x.Message) });
                }
                else
                {
                    new ProjectController().UpdateHours(int.Parse(projectID), float.Parse(design), float.Parse(doing), float.Parse(codeReview), float.Parse(testing), float.Parse(deliverables));
                    response = StatusCode(200, "Hours updated.");
                }
            }
            return(response);
        }
 public void AddValidationException(string validationException)
 {
     ValidationExceptions.Concat(new[] { validationException });
 }
 public void AddValidationExceptions(IEnumerable <string> validationExceptions)
 {
     ValidationExceptions.Concat(validationExceptions);
 }