public async Task DeleteDepartmentAsync(int id)
        {
            var dbDepartments = await _context.Departments.Where(d => d.Id == id && !d.IsDeleted)
                                .OrderBy(d => d.Id).ToArrayAsync();

            if (dbDepartments.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbDepartment          = dbDepartments[0];
            var childDepartmentsCount = await _context.Departments.Where(d => d.ParentId == id).CountAsync();

            var employeesCount = await _context.Employees.Where(e => e.DepartmentId == id).CountAsync();

            if (childDepartmentsCount != 0)
            {
                throw new RequestedResourceHasConflictException($"Department still has child departments! {id}");
            }

            if (employeesCount == 0)
            {
                _context.Departments.Remove(dbDepartment);
            }
            else
            {
                dbDepartment.IsDeleted = true;
            }

            await _context.SaveChangesAsync();
        }
Esempio n. 2
0
        public async Task <Department> CreateDepartment(UpdateDepartmentRequest createRequest)
        {
            var dbDepartments = await _context.Departments.Where(d => d.Code == createRequest.Code).ToArrayAsync();

            if (dbDepartments.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var dbDepartment = Mapper.Map <UpdateDepartmentRequest, StoreDepartment>(createRequest);

            _context.Departments.Add(dbDepartment);

            await _context.SaveChangesAsync();

            return(Mapper.Map <Department>(dbDepartment));
        }
Esempio n. 3
0
        public async Task SetStatusAsync(int id, bool deletedStatus)
        {
            var dbDepartments = await _context.Departments.Where(d => d.Id == id).ToArrayAsync();

            if (dbDepartments.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            /* var childDepartmentsCount = await _context.Departments.Where(d => d.ParentId == id).CountAsync();
             * if (childDepartmentsCount != 0)
             * {
             *   throw new RequestedResourceHasConflictException($"Department still has child departments! {id}");
             * } */

            var dbDepartment = dbDepartments[0];

            if (dbDepartment.IsDeleted != deletedStatus)
            {
                dbDepartment.IsDeleted = deletedStatus;
                await _context.SaveChangesAsync();
            }
        }