public void GetEmployees_ListOfIds_ListOfEmployees()
        {
            IList<Employee> employee;

            using (var dbContext = new DatabaseContext(testUser, testPass))
            {
                var repository = new EmployeesRepository(dbContext);
                var ids = new int[5] { 1, 2, 3, 4, 5 };
                employee = repository.GetByIdList(ids);
            }

            Assert.IsTrue(employee.Count > 1);
        }
        public void FindByFilter_FilterString_ListOfEmployees()
        {
            IList<Employee> employee;
            string lastNameFilter = "Aleksandr";

            using (var dbContext = new DatabaseContext(testUser, testPass))
            {
                var repository = new EmployeesRepository(dbContext);
                employee = repository.FindByLastName(lastNameFilter);
            }

            Assert.IsTrue(employee.All(x => x.LastName.Contains(lastNameFilter)));
        }
        public void ChangeCredentialsToDBonRuntime_twousers_bonusesFound()
        {
            int numberOfBonusesFirstUser;
            int numberOfBonusesSecondUser;
            using (var dbContext = new DatabaseContext("ryakh", "1"))
            {
                var repository = new BonusesRepository(dbContext);
                numberOfBonusesFirstUser = repository.FindAll().Count();
            }

            using (var dbContext = new DatabaseContext("kmikula", "1"))
            {
                var repository = new BonusesRepository(dbContext);
                numberOfBonusesSecondUser = repository.FindAll().Count();
            }

            Assert.IsTrue(numberOfBonusesFirstUser > 0);
            Assert.IsTrue(numberOfBonusesSecondUser > 0);
        }
        public JsonResult Create(BonusDto bonusDto)
        {
            BonusAggregate bonus;

            if (bonusDto.Amount <= 0)
                throw new ArgumentOutOfRangeException("Amount should be more than 0");

            if (bonusDto.EmployeeId <= 0)
                throw new ArgumentNullException("You should specify an existing employee");

            using (var dbContext = new DatabaseContext())
            {
                BonusesRepository = new BonusesRepository(dbContext);
                var employeeRepository = new EmployeesRepository(dbContext);
                bonus = new BonusFactory(employeeRepository).Create(bonusDto);

                BonusesRepository.Save(bonus);
            }

            return Json(bonus);
        }
 public void Save_noParams_exception()
 {
     using (var dbContext = new DatabaseContext(testUser, testPass))
     {
         employeeRepository = new EmployeesRepository(dbContext);
         employeeRepository.Save(new Employee("", "", ""));
     }
 }
        public JsonResult Edit(BonusDto bonusDto)
        {
            if (bonusDto == null)
                throw new ArgumentNullException("bonusDto can not be null in controller Edit");

            Employee employee = null;
            BonusAggregate bonus = null;
            using (var dbContext = new DatabaseContext())
            {
                if (bonusDto.EmployeeId != 0)
                {
                    var employeeRepository = new EmployeesRepository(dbContext);
                    employee = employeeRepository.GetById(bonusDto.EmployeeId);
                }

                BonusesRepository = new BonusesRepository(dbContext);
                bonus = BonusesRepository.GetById(bonusDto.BonusId);
                bonus.Comment = bonusDto.Comment;
                bonus.Amount = bonusDto.Amount;
                bonus.Date = bonusDto.Date;
                bonus.IsActive = bonusDto.IsActive;

                if (employee != null &&
                    employee.EmployeeId != bonus.EmployeeId)
                {
                    bonus.Employee = employee;
                }

                BonusesRepository.Save(bonus);
            }

            return Json(bonus);
        }
        public JsonResult GetPagedJsonBonuses(int take, int skip)
        {
            PagedResponse<BonusAggregate> bonuses;

            var filteredRequest = new FilteredRequest(Request.Params);

            using (var dbContext = new DatabaseContext())
            {
                BonusesRepository = new BonusesRepository(dbContext);
                bonuses = BonusesRepository.FindAll(skip,
                                                    take,
                                                    filteredRequest.SortingField,
                                                    filteredRequest.Direction,
                                                    filteredRequest.FilterField,
                                                    filteredRequest.FilterPattern);
            }

            return Json(bonuses, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetJsonEmployeesByLastName()
        {
            IList<Employee> employees = new List<Employee>();
            string lastName = FilterStringFactory.FormFilterValue(Request.Params);

            if (String.IsNullOrEmpty(lastName) == false)
            {
                using (var dbContext = new DatabaseContext())
                {
                    var employeesRepository = new EmployeesRepository(dbContext);
                    employees = employeesRepository.FindByLastName(lastName);
                }
            }

            if (employees.Count == 0)                    // this's because some magic happens with autocomplete combobox
                employees.Add(new Employee("", "", "")); // and it start to cycle ajax requests if the list is empty or null

            return Json(employees, JsonRequestBehavior.AllowGet);
        }
        public void FindAll_NoParams_EmployeeExists()
        {
            BonusAggregate bonusAggregate;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusAggregate = bonusRepository.FindAll().First();
            }

            Assert.IsNotNull(bonusAggregate.Employee);
        }
        /// <summary>
        /// Gets the employee by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>Employee.</returns>
        private Employee GetEmployeeById(int id = 1)
        {
            Employee employee;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                employeeRepository = new EmployeesRepository(dbContext);
                employee = employeeRepository.GetById(id);
            }

            return employee;
        }
        public void Save_BonusesList_2BonusesUpdated()
        {
            IList<BonusAggregate> bonusesToUpdate;
            IList<BonusAggregate> updatedBonuses = new List<BonusAggregate>();
            var bonusesIds = new int[2];

            string newComment = "comment on " + DateTime.Now;
            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusesToUpdate = bonusRepository.FindAll().Take(2).ToList();
                bonusesToUpdate[0].Comment = newComment;
                bonusesToUpdate[1].Comment = newComment;

                bonusesIds[0] = bonusesToUpdate[0].BonusId;
                bonusesIds[1] = bonusesToUpdate[1].BonusId;
                bonusRepository.Save(bonusesToUpdate);
            }

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[0]));
                updatedBonuses.Add(bonusRepository.GetById(bonusesIds[1]));
            }

            Assert.AreEqual(updatedBonuses[0].Comment, newComment);
            Assert.AreEqual(updatedBonuses[1].Comment, newComment);
        }
        public void Save_BonusesList_2BonusesAdded()
        {
            int numberOfItemsBeforSave;

            int numberOfCurrentBonuses;
            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusFactory = new BonusFactory(new EmployeesRepository(dbContext));
                var bonusesList = new List<BonusAggregate>
                                        {
                                          bonusFactory.Create(GetEmployeeById(4), DateTime.Now, 100),
                                          bonusFactory.Create(GetEmployeeById(5), DateTime.Now, 90)
                                        };

                bonusRepository = new BonusesRepository(dbContext);
                numberOfItemsBeforSave = bonusRepository.FindAll().Count();
                bonusRepository.Save(bonusesList);
                numberOfCurrentBonuses = bonusRepository.FindAll().Count();
            }

            Assert.AreEqual(numberOfCurrentBonuses - 2, numberOfItemsBeforSave);
        }
        public void GetById_NoParams_Bonus()
        {
            BonusAggregate bonusAggregate;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonusAggregate = bonusRepository.GetById(1);
            }

            Assert.AreEqual(1, bonusAggregate.BonusId);
        }
        public void FindAll_noParams_SomeBonuses()
        {
            IList<BonusAggregate> bonuses;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                bonuses = bonusRepository.FindAll();
            }

            Assert.IsNotNull(bonuses);
            Assert.AreNotEqual(0, bonuses.Count);
        }
 public void FindAllWithPaging_NegativeSkip2Take3_3bonuses()
 {
     using (var dbContext = new DatabaseContext(TestUser, TestPass))
     {
         bonusRepository = new BonusesRepository(dbContext);
         bonusRepository.FindAllWithPaging(-2, 3);
     }
 }
        public void GetFirstEmployee_noParams_employee()
        {
            Employee employee;

            using (var dbContext = new DatabaseContext(testUser, testPass))
            {
                employeeRepository = new EmployeesRepository(dbContext);
                employee = employeeRepository.FindAll().FirstOrDefault();
            }

            Assert.IsNotNull(employee);
        }
        public void FindAllWithPaging_Skip2Take3_CorrectTotalCount()
        {
            IList<BonusAggregate> notSkipedBonuses;
            PagedResponse<BonusAggregate> skipedBonuses;

            using (var dbContext = new DatabaseContext(TestUser, TestPass))
            {
                bonusRepository = new BonusesRepository(dbContext);
                notSkipedBonuses = bonusRepository.FindAll();
                skipedBonuses = bonusRepository.FindAllWithPaging(2, 3);
            }
            Assert.AreEqual(skipedBonuses.TotalCount, notSkipedBonuses.Count);
        }