Ejemplo n.º 1
0
        public async Task <ServiceResponse> SaveEmployeeAsync(EmployeeDto EmployeeDto)
        {
            ServiceResponse ret = new ServiceResponse();

            Guid newId = Guid.Empty;

            try
            {
                if (EmployeeDto.Id == Guid.Empty)
                {
                    newId          = Guid.NewGuid();
                    EmployeeDto.Id = newId;
                }

                Guid existing_id = await this.employeeRepository.GetIdByName(EmployeeDto.Name);

                // TODO: check everything
                // if new record and name already exists
                if (existing_id != Guid.Empty && existing_id != EmployeeDto.Id)
                {
                    ret.AddError(new ServiceResponseItem("EmployeeName", $"Employe with name: {EmployeeDto.Name} already exists."));
                }

                // WorkPlace
                if (EmployeeDto.WorkPlace.Id == Guid.Empty)
                {
                    ret.AddError(new ServiceResponseItem("WorkPlace", $"Workplace must be defined"));
                }

                // check Manager

                if (EmployeeDto.Manager != null && EmployeeDto.Manager.Id == Guid.Empty)
                {
                    ret.AddError(new ServiceResponseItem("Manager", $"Manager must be defined"));
                }

                if (EmployeeDto.Manager == null)
                {
                    ret.AddError(new ServiceResponseItem("Manager", $"Manager must be defined"));
                }

                if (EmployeeDto.Manager != null)
                {
                    Manager manager = await this.managerRepository.GetById(EmployeeDto.Manager.Id);

                    if (manager == null)
                    {
                        ret.AddError(new ServiceResponseItem("Manager", $"Invalid Manager specified!"));
                    }
                }


                // other checks goes here


                // if any errors, return
                if (ret.HasErrors())
                {
                    return(ret.Get());
                }

                //

                // compose new empoyee
                Employee employee = new Employee(EmployeeDto.Id, EmployeeDto.Name, EmployeeDto.Email);
                if (EmployeeDto.WorkPlace != null)
                {
                    WorkPlaceDto wpDto = EmployeeDto.WorkPlace;
                    WorkPlace    wp    = new WorkPlace(wpDto.Id, wpDto.Country, wpDto.City, wpDto.Name);
                    employee.SetWorkPlace(wp);
                }

                if (EmployeeDto.Manager != null)
                {
                    ManagerDto mDto = EmployeeDto.Manager;
                    employee.SetManager(new Manager(mDto.Id, mDto.Name));
                }

                foreach (EmployeeSkillDto sdto in EmployeeDto.Skills)
                {
                    employee.AddSkill(sdto.Id, sdto.Name, MaturityEnum.High);
                }

                foreach (EmployeeRoleDto rdto in EmployeeDto.Roles)
                {
                    ContributionGroup cg = new ContributionGroup(rdto.ContributionGroup.Id, rdto.ContributionGroup.Name);
                    employee.AddRole(rdto.RoleId, rdto.Name, cg);
                }

                await this.employeeRepository.UpdateEmployeeAsync(employee);
            }
            catch (Exception ex)
            {
                ret.SetErrorMessage("Error while saving an employee occured : " + ex.Message);
            }
            return(ret.Get());
        }