예제 #1
0
        /// <summary>
        /// Creates the specified bonus aggregate from DTO object.
        /// </summary>
        /// <param name="bonusDto">The bonus DTO object.</param>
        /// <returns>
        /// BonusAggregate.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">Create;Can not create new BonusAggregate from null Dto object</exception>
        public BonusAggregate Create(BonusDto bonusDto)
        {
            if (bonusDto == null)
                throw new ArgumentNullException("Create", "can not create new BonusAggregate from null DTO object");

            Employee employee = EmployeesRepository.GetById(bonusDto.EmployeeId);

            return new BonusAggregate(employee,
                                        bonusDto.Date,
                                        bonusDto.Amount,
                                        bonusDto.Comment,
                                        bonusDto.IsActive,
                                        bonusDto.BonusId);
        }
        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 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);
        }