public void Run()
        {
            var db = new BangaloreUniversityData();
            User user = null;
            while (true)
            {
                string line = Console.ReadLine();
                if (line == null)
                {
                    break;
                }

                var route = new Route(line);
                var controllerType =
                    Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.Name == route.ControllerName);
                var controller = Activator.CreateInstance(controllerType, db, user) as Controller;
                var action = controllerType.GetMethod(route.ActionName);
                object[] @params = MapParameters(route, action);

                try
                {
                    var view = action.Invoke(controller, @params) as IView;
                    Console.WriteLine(view.Display());
                    user = controller.User;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }
Пример #2
0
        public static void Main()
        {
            var database = new BangaloreUniversityData();
            var engine   = new Engine(database);

            engine.Run();
        }
Пример #3
0
        public void Run()
        {
            var  data = new BangaloreUniversityData();
            User user = null;

            while (true)
            {
                string inputLine = Console.ReadLine();
                if (inputLine == null)
                {
                    break;
                }

                var route          = new Route(inputLine);
                var controllerType =
                    Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(type => type.Name == route.ControllerName);
                var      controller = Activator.CreateInstance(controllerType, data, user) as Controller;
                var      action     = controllerType.GetMethod(route.ActionName);
                object[] @params    = MapParameters(route, action);
                try
                {
                    var view = action.Invoke(controller, @params) as IView;
                    Console.WriteLine(view.Display());
                    user = controller.User;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }
Пример #4
0
        public void ViewAllCourse_WhereAreCoursesWithNoStudent_ShouldReturnCoursesOrderedByName()
        {
            User firstUser = new User(FirstUsername, "password", Role.Lecturer);

            var db = new BangaloreUniversityData();
            CoursesController coursesController = new CoursesController(db, firstUser);

            coursesController.Create(FirstCourse);
            coursesController.Create(SecondCourse);
            coursesController.Create(ThirdCourse);
            coursesController.Create(FourthCourse);

            IView  courseView = coursesController.All();
            string actualView = courseView.Display();

            StringBuilder expectedView = new StringBuilder();

            expectedView.AppendLine("All courses:");
            expectedView.AppendLine("CourseA (0 students)");
            expectedView.AppendLine("CourseA (0 students)");
            expectedView.AppendLine("CourseB (0 students)");
            expectedView.Append("CourseC (0 students)");

            Assert.AreEqual(expectedView.ToString(), actualView, "Does not return proper view(course order) when there are no views!");
        }
Пример #5
0
        public void TestLogoutWithWrongUserRoleShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User            user          = new User("firstLecturer", "firstPass", Role.Guest);
            UsersController controller    = new UsersController(data, user);

            controller.Logout().Display();
        }
Пример #6
0
        public void TestLogoutIfNoUserIsLoggedInShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User            user          = new User("firstLecturer", "firstPass", Role.Lecturer);
            UsersController controller    = new UsersController(data, null);

            controller.Logout().Display();
        }
Пример #7
0
        public static void Main()
        {
            IBangaloreUniversityData dataBase      = new BangaloreUniversityData();
            IRenderer                 renderer     = new ConsoleRenderer();
            IInputHandler             inputHandler = new InputHandler();
            BangaloreUniversityEngine engine       = new BangaloreUniversityEngine(dataBase, renderer, inputHandler);

            engine.Run();
        }
Пример #8
0
        public void TestSuccessfullLogoutShouldReturnAppropriateMessage()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User            user          = new User("firstLecturer", "firstPass", Role.Lecturer);
            UsersController controller    = new UsersController(data, user);

            string message = controller.Logout().Display();

            Assert.AreEqual("User firstLecturer logged out successfully.", message);
        }
Пример #9
0
        public void TestWithNoCourses()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Guest);
            CoursesController controller = new CoursesController(data, user);

            string message = controller.All().Display();

            Assert.AreEqual("No courses.", message);
        }
Пример #10
0
        public void Run()
        {
            var  database    = new BangaloreUniversityData();
            User currentUser = null;

            while (true)
            {
                string routeUrl = Console.ReadLine();
                if (routeUrl == null)
                {
                    break;
                }

                var route = new Route(routeUrl);

                // gets the controller type through reflection
                var controllerType = Assembly
                                     .GetExecutingAssembly()
                                     .GetTypes()
                                     .FirstOrDefault(type => type.Name == route.ControllerName);

                // create an instance of the specified type using constructor that best match specified parameters
                var controller = Activator
                                 .CreateInstance(controllerType, database, currentUser)
                                 as Controller; //as == cast (only for referent types)

                // search for the public method with the specified name
                var      action          = controllerType.GetMethod(route.ActionName);
                object[] argumentsToPass = MapParameters(route, action);
                try
                {
                    //invoke the method represented by the current instance, using the specified parameters (through reflection)
                    // it is the same:
                    // var controller = new UsersController();
                    // controller.Register(argumentsToPass);
                    var view   = action.Invoke(controller, argumentsToPass) as IView;
                    var output = view.Display();
                    Console.WriteLine(output);
                    currentUser = controller.CurrentUser;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is ArgumentException || ex.InnerException is AuthorizationFailedException)
                    {
                        Console.WriteLine(ex.InnerException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        public static void Main()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();

            IInputReader reader = new ConsoleReader();

            IOutputWriter writer = new ConsoleWriter
            {
                AutoFlush = true
            };

            IEngine engine = new BangaloreUniversityEngine(reader, writer, data);
            engine.Run();
        }
        public void Run()
        {
            var database = new BangaloreUniversityData();
            User currentUser = null;

            while (true)
            {
                string routeUrl = Console.ReadLine();
                if (routeUrl == null)
                {
                    break;
                }

                var route = new Route(routeUrl);

                var expectedController = route.ControllerName;
                var controllerType = Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .FirstOrDefault(type => type.Name == expectedController);

                var controller = Activator.CreateInstance(
                    controllerType,
                    database,
                    currentUser) as Controller;

                var action = controllerType.GetMethod(route.ActionName);
                var argumentsToPass = MapParameters(route, action);

                try
                {
                    var view = action.Invoke(controller, argumentsToPass) as IView;

                    var output = view.Display();
                    Console.WriteLine(output);
                    currentUser = controller.CurrentUser;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is ArgumentException || ex.InnerException is AuthorizationFailedException)
                    {
                        Console.WriteLine(ex.InnerException.Message);
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
            }
        }
        public void Run()
        {
            var  database    = new BangaloreUniversityData();
            User currentUser = null;

            while (true)
            {
                string routeUrl = Console.ReadLine();
                if (routeUrl == null)
                {
                    break;
                }

                var route = new Route(routeUrl);
                var expectedController = route.ControllerName;
                var controllerType     = Assembly.GetExecutingAssembly()
                                         .GetTypes()
                                         .FirstOrDefault(type => type.Name == expectedController);

                var controller = Activator.CreateInstance(controllerType, database, currentUser) as Controller;

                var action          = controllerType.GetMethod(route.ActionName);
                var argumentsToPass = MapParameters(route, action);
                try
                {
                    var view = action.Invoke(controller, argumentsToPass) as IView;

                    var output = view.Display();
                    Console.WriteLine(output);

                    currentUser = controller.CurrentUser;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is ArgumentException || ex.InnerException is AuthorizationFailedException)
                    {
                        Console.WriteLine(ex.InnerException.Message);
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
            }
        }
Пример #14
0
        public void TestWithOneCourseAndNoStudents()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            CoursesController controller = new CoursesController(data, user);

            controller.Create("Object-Oriented+Programming");
            string message = controller.All().Display();

            var courses = new StringBuilder();

            courses.AppendLine("All courses:");
            foreach (var course in data.Courses.Items)
            {
                courses.AppendFormat("{0} ({1} students)", course.CourseName, course.Students.Count);
            }

            Assert.AreEqual(courses.ToString(), message);
        }
Пример #15
0
        public void TestAllCoursesWhenHasCoursesShouldReturnTheCourses()
        {
            var data            = new BangaloreUniversityData();
            var loginController = new UsersController(data, null);

            loginController.Register("habala", "babbala", "babbala", "Lecturer");
            var user       = new User("habala", "babbala", Role.Lecturer);
            var controller = new CoursesController(data, user);

            controller.Create("tessst");
            controller.Create("tessst1");
            controller.Create("tessst3");
            var result  = controller.All();
            var message = @"All courses:
tessst (0 students)
tessst1 (0 students)
tessst3 (0 students)";

            Assert.AreEqual(message, result.Display());
        }
        public void Run()
        {
            var  dataBase = new BangaloreUniversityData();
            User user     = null;

            while (true)
            {
                string routeUrl = Console.ReadLine();
                if (routeUrl == null)
                {
                    break;
                }

                var route = new Route(routeUrl);

                var controllerType = Assembly
                                     .GetExecutingAssembly()
                                     .GetTypes()
                                     .FirstOrDefault(type => type.Name == route.ControllerName);

                var controller = Activator.CreateInstance(controllerType, dataBase, user) as Controller;

                var      action     = controllerType.GetMethod(route.ActionName);
                object[] parameters = MapParameters(route, action);
                string   viewResult = string.Empty;

                try
                {
                    var view = action.Invoke(controller, parameters) as IView;
                    viewResult = view.Display();
                    //Console.WriteLine(view.Display());
                    user = controller.User;
                }
                catch (Exception ex)
                {
                    viewResult = ex.InnerException.Message;
                }

                Console.WriteLine(viewResult);
            }
        }
Пример #17
0
        public void ViewAllCourse_WhereAreCoursesWithSameNameButDifferenStudentsCount_ShouldReturnCoursesOrderedByStudents()
        {
            User firstUser  = new User(FirstUsername, "password", Role.Lecturer);
            User secondUser = new User(SecondUsername, "password", Role.Student);
            User thirdUser  = new User(SecondUsername, "password", Role.Student);

            var db = new BangaloreUniversityData();
            CoursesController coursesController = new CoursesController(db, firstUser);

            coursesController.Create(FirstCourse);
            coursesController.Create(FirstCourse);
            coursesController.Create(FirstCourse);
            coursesController.Create(FirstCourse);
            coursesController.Enroll(1);

            Enroll_InCourse(db, secondUser, 1);
            Enroll_InCourse(db, thirdUser, 1);
            Enroll_InCourse(db, secondUser, 2);
            Enroll_InCourse(db, firstUser, 2);

            Enroll_InCourse(db, secondUser, 3);
            Enroll_InCourse(db, firstUser, 3);

            Enroll_InCourse(db, firstUser, 4);

            IView  courseView = coursesController.All();
            string actualView = courseView.Display();

            StringBuilder expectedView = new StringBuilder();

            expectedView.AppendLine("All courses:");
            expectedView.AppendLine("CourseA (3 students)");
            expectedView.AppendLine("CourseA (2 students)");
            expectedView.AppendLine("CourseA (2 students)");
            expectedView.Append("CourseA (1 students)");

            Assert.AreEqual(expectedView.ToString(), actualView, "Does not return proper view(course order) when there are no views!");
        }
Пример #18
0
        public void Run()
        {
            var context = new BangaloreUniversityData();

            User user = null;
            var  line = Console.ReadLine();

            while (line != null && line != "end")
            {
                var route          = new Router(line);
                var controllerType = Assembly.GetExecutingAssembly()
                                     .GetTypes()
                                     .FirstOrDefault(type => type.Name == route.ControllerName);
                var controller = Activator.CreateInstance(controllerType, context, user) as BaseController;
                var action     = controllerType.GetMethod(route.ActionName);
                var parameters = MapParameters(route, action);

                try
                {
                    var view = action.Invoke(controller, parameters) as IView;
                    Console.WriteLine(view.Display());
                    user = controller.User;
                }
                catch (TargetInvocationException ex)
                {
                    if (ex.InnerException is ArgumentException || ex.InnerException is AuthorizationFailedException)
                    {
                        Console.WriteLine(ex.InnerException.Message);
                    }
                    else
                    {
                        throw ex;
                    }
                }

                line = Console.ReadLine();
            }
        }
Пример #19
0
        public void TestWithSeveralCoursesAndDifferentStudents()
        {
            IBangaloreUniversityData data = new BangaloreUniversityData();
            User user = new User("firstLecturer", "firstPass", Role.Lecturer);
            CoursesController controller = new CoursesController(data, user);

            controller.Create("Object-Oriented+Programming");
            controller.Create("High-Quality+Code");
            controller.Create("Java+Basics");
            controller.Enroll(1);
            controller.Enroll(3);
            string message = controller.All().Display();

            var courses = new StringBuilder();

            courses.AppendLine("All courses:");
            foreach (var course in data.Courses.Items.OrderBy(c => c.CourseName).ThenByDescending(c => c.Students.Count))
            {
                courses.AppendFormat("{0} ({1} students)", course.CourseName, course.Students.Count).AppendLine();
            }

            Assert.AreEqual(courses.ToString().Trim(), message);
        }
        public void Run()
        {
            var database = new BangaloreUniversityData();
            User currentUser = null;
            while (true)
            {
                string routeUrl = Console.ReadLine();
                if (routeUrl == null)
                {
                    break;
                }

                var route = new Route(routeUrl);

                var controllerType = Assembly
                    .GetExecutingAssembly()
                    .GetTypes()
                    .FirstOrDefault(type => type.Name == route.ControllerName);
                var controller = Activator.CreateInstance(controllerType, database, currentUser) as Controller;
                var action = controllerType.GetMethod(route.ActionName);
                object[] parameters = MapParameters(route, action);
                string viewResult = string.Empty;
                try
                {
                    var view = action.Invoke(controller, parameters) as IView;
                    viewResult = view.Display();
                    currentUser = controller.CurrentUser;
                }
                catch (Exception ex)
                {
                    viewResult = ex.InnerException.Message;
                }

                Console.WriteLine(viewResult);
            }
        }
Пример #21
0
        private static void Enroll_InCourse(BangaloreUniversityData db, User user, int courseId)
        {
            CoursesController controller = new CoursesController(db, user);

            controller.Enroll(courseId);
        }