Esempio n. 1
0
        public async Task <Department> CreateDepartmentAsync(UpdateDepartmentRequest createRequest)
        {
            var departmentId = createRequest.ParentId;

            if (departmentId != null)
            {
                var dbDepartments = await _context.Departments.Where(d => d.Id == departmentId).ToArrayAsync();

                if (dbDepartments.Length == 0)
                {
                    throw new RequestedResourceHasConflictException($"Cannot add new department with not existing parent {nameof(departmentId)}");
                }
            }

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

            _context.Departments.Add(dbDepartment);
            await _context.SaveChangesAsync();

            return(Mapper.Map <Department>(dbDepartment));
        }
Esempio n. 2
0
        public async Task <Department> UpdateDepartment(int departmentId, UpdateDepartmentRequest updateRequest)
        {
            var dbDepartments = await _context.Departments.Where(p => p.Code == updateRequest.Code && p.Id != departmentId).ToArrayAsync();

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

            dbDepartments = _context.Departments.Where(p => p.Id == departmentId).ToArray();
            if (dbDepartments.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbDepartment = dbDepartments[0];

            Mapper.Map(updateRequest, dbDepartment);

            await _context.SaveChangesAsync();

            return(Mapper.Map <Department>(dbDepartment));
        }