예제 #1
0
        /// <summary>
        /// This method is used to delete the department
        /// </summary>
        /// <param name="id">Department Id</param>
        /// <returns>returns department details</returns>
        public async Task <Department> DeleteDepartmentAsync(int id)
        {
            var getDepartment = await _departmentRepository.GetDepartmentDetailsAsync(id);

            if (getDepartment == null)
            {
                return(null);
            }
            var getDesignationList = await _departmentDesignationRepository.GetDesignationList(id);

            if (getDesignationList != null)
            {
                foreach (var i in getDesignationList)
                {
                    await _designationService.DeleteDesignationAsync(i.DesignationId);
                }
            }
            using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                getDepartment.IsDeleted = true;
                await _departmentRepository.SaveChangesAsync();

                await _departmentDesignationRepository.SaveChangesAsync();

                ts.Complete();
            }
            return(getDepartment);
        }
        /// <summary>
        /// this method is used to delete the designation
        /// </summary>
        /// <param name="designationId">designation id</param>
        /// <returns>string statting the status of the method</returns>
        public async Task <string> DeleteDesignationAsync(int designationId)
        {
            var validDesg = await _designationRepository.GetDesignationDetailsAsync(designationId);

            if (validDesg == null)
            {
                throw new Exception("NotFound.Requested designation does not exist");
            }
            validDesg.IsDeleted = true;
            await _designationRepository.SaveChangesAsync();

            var validDeptDesig = await _departmentDesignationRepository.GetDepartmentDesignationDetailsAsync(designationId);

            validDeptDesig.IsDeleted = true;
            await _departmentDesignationRepository.SaveChangesAsync();

            return("Designation removed successfully");
        }
        /// <summary>
        /// this method is used to add designations to the department
        /// </summary>
        /// <param name="departmentId">department id</param>
        /// <param name="designations">list of designations</param>
        /// <returns>string stating the status of the method</returns>
        public async Task <string> AddDesignationToDepartmentAsync(int departmentId, List <string> designations)
        {
            if (designations == null)
            {
                throw new Exception("Not Found. No designations added");
            }
            var validDeptartment = await _designationRepository.GetDepartementDetailsAsync(departmentId);

            if (validDeptartment == null)
            {
                throw new Exception("NotFound.Requested department does not exist");
            }
            var designationList = new List <Designation>();

            foreach (var item in designations)
            {
                designationList.Add(new Designation
                {
                    Title     = item,
                    IsDeleted = false
                });
            }
            _designationRepository.AddDesignations(designationList);
            await _designationRepository.SaveChangesAsync();

            var designationIds            = _designationRepository.GetDesignationIds(designations);
            var departmentDesignationList = new List <DepartmentDesignation>();

            foreach (var item in designationIds)
            {
                departmentDesignationList.Add(new DepartmentDesignation
                {
                    DepartmentId  = departmentId,
                    DesignationId = item,
                    IsDeleted     = false
                });
            }
            _departmentDesignationRepository.AddDepartmentDesignation(departmentDesignationList);
            await _departmentDesignationRepository.SaveChangesAsync();

            return("Designations added successfully in the department");
        }
        /// <summary>
        /// this method is used to add new level in database and map it to designation department
        /// </summary>
        /// <param name="departmentDesignationViewModel">model that contains the mapping of department and designation to be mapped to new level</param>
        public async Task AddNewLevelAsync(DepartmentDesignationDto departmentDesignationViewModel)
        {
            if (departmentDesignationViewModel.DesignationId != null)
            {
                var designationDepartmentIdList = await _departmentDesignationRepository.GetDesignationDepartmentId(departmentDesignationViewModel.DesignationId, departmentDesignationViewModel.DepartmentId);

                var level = new Level
                {
                    IsDeleted = false,
                    Title     = departmentDesignationViewModel.NewLevelName
                };

                _levelRepository.AddLevel(level);
                await _levelRepository.SaveChangesAsync();

                var designationDepartmentLevelMapping = new List <DesignationLevel>();
                foreach (var id in designationDepartmentIdList)
                {
                    var designationLevel = new DesignationLevel
                    {
                        LevelId = level.Id,
                        DepartmentDesignationId = id,
                        IsDeleted = false
                    };
                    designationDepartmentLevelMapping.Add(designationLevel);
                }

                _departmentDesignationRepository.AddDesignationDepartmentLevelList(designationDepartmentLevelMapping);
                await _departmentDesignationRepository.SaveChangesAsync();
            }

            else
            {
                throw new Exception("Department can not be empty");
            }
        }