Example #1
0
        protected async override Task Handle(RoleAddToDepartmentRequest request, CancellationToken cancellationToken)
        {
            var role = await _context.Roles
                       .Include(r => r.Departments)
                       .FirstOrDefaultAsync(r => r.Guid == request.RoleId, cancellationToken);

            if (role == null)
            {
                throw new BadRequestException(); // role to add not found
            }

            var department = await _context.Departments.FindByGuidAsync(request.DepartmentId, cancellationToken);

            if (department == null)
            {
                throw new BadRequestException(); // department to add role not found
            }

            if (role.Departments.Select(d => d.Department).Any(d => d.Guid == department.Guid))
            {
                throw new ConflictException("role is already added to this department"); // role is already added to the department
            }

            role.Departments.Add(new RoleDepartment()
            {
                Role       = role,
                Department = department
            });

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #2
0
        protected async override Task Handle(ProductTypeAddParentsRequest request, CancellationToken cancellationToken)
        {
            var childProductType = await _context.ProductTypes
                                   .Include(pt => pt.Parents)
                                   .FirstOrDefaultAsync(pt => pt.Guid == request.BaseProductTypeId);

            if (childProductType == null)
            {
                throw new BadRequestException(); // child to add new parent to couldn't be found
            }

            foreach (var parentId in request.ParentIds)
            {
                var parentProductType = await _context.ProductTypes.FindByGuidAsync(parentId, cancellationToken);

                if (parentProductType == null)
                {
                    throw new BadRequestException(); // parent product type to add couldn't be found
                }

                if (childProductType.Parents.Any(p => p.ParentGuid == parentProductType.Guid))
                {
                    throw new ConflictException("cannot add parent product type to product type, because its already added");
                }

                await _context.ProductTypesProductTypes.AddAsync(new ProductTypeProductType()
                {
                    Child  = childProductType,
                    Parent = parentProductType
                }, cancellationToken);
            }

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #3
0
        protected async override Task Handle(DepartmentRemoveEmployeeRequest request, CancellationToken cancellationToken)
        {
            var department = await _context.Departments
                             .Include(d => d.Employees)
                             .FirstOrDefaultAsync(d => d.Guid == request.DepartmentId, cancellationToken);

            if (department == null)
            {
                throw new BadRequestException(); // department to remove employee from not found
            }

            var employee = await _context.Employees.FindByGuidAsync(request.EmployeeId, cancellationToken);

            if (employee == null)
            {
                throw new BadRequestException(); // employee to remove from the department not found
            }

            if (!department.Employees.Any(e => e.Guid == employee.Guid))
            {
                throw new BadRequestException(); // department doesn't have this employee added => can't be removed
            }

            department.Employees.Remove(employee);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #4
0
        public async Task <ProductTypeResponse> Handle(ProductTypeUpdateRequest request, CancellationToken cancellationToken)
        {
            // check if name is unique
            if (await _context.ProductTypes.AnyAsync(n => (n.Guid != request.ProductTypeGuid && n.Name == request.Name), cancellationToken))
            {
                throw new ConflictException(nameof(request.Name), $"A product type with the name: {request.Name} already exists, please choose another name");
            }

            var productType = await _context.ProductTypes
                              .Include(pt => pt.Children)
                              .Include(pt => pt.Parents)
                              .FirstOrDefaultAsync(pt => pt.Guid == request.ProductTypeGuid, cancellationToken);

            if (productType == null)
            {
                throw new BadRequestException(); // product type to update not found
            }

            productType.Name        = request.Name;
            productType.Description = request.Description;
            productType.EAN         = request.EAN;
            productType.Price       = request.Price;
            productType.Image       = Convert.FromBase64String(request.Image);

            await _context.SaveChangesAsync(cancellationToken);

            var updatedProductTypeDto = _mapper.Map <ProductType, ProductTypeResponse>(productType);

            updatedProductTypeDto.ChildGuids  = productType.Children.Select(ptc => ptc.ChildGuid);
            updatedProductTypeDto.ParentGuids = productType.Parents.Select(ptp => ptp.ParentGuid);

            return(updatedProductTypeDto);
        }
Example #5
0
        public async Task <EmployeeResponse> Handle(EmployeeUpdateRequest request, CancellationToken cancellationToken)
        {
            var employee = await _context.Employees.FindByGuidAsync(request.EmployeeId, cancellationToken);

            if (employee == null)
            {
                throw new BadRequestException(); // employee to update not found
            }

            employee.Birthday  = request.Birthday;
            employee.Email     = request.Email;
            employee.Firstname = request.Firstname;
            employee.Lastname  = request.Lastname;

            // set new password only if the current and new pw are provided and the current password entered is correct
            if (!string.IsNullOrEmpty(request.CurrentPassword) &&
                !string.IsNullOrEmpty(request.NewPassword) &&
                _hasher.PasswordMatches(request.CurrentPassword, employee.Password))
            {
                employee.Password = _hasher.HashPassword(request.NewPassword);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <Employee, EmployeeResponse>(employee));
        }
        protected async override Task Handle(DepartmentAddEmployeeRequest request, CancellationToken cancellationToken)
        {
            var department = await _context.Departments
                             .Include(d => d.Employees)
                             .FirstOrDefaultAsync(d => d.Guid == request.DepartmentId, cancellationToken);

            if (department == null)
            {
                throw new BadRequestException(); // department to add employee not found
            }

            var employee = await _context.Employees.FindByGuidAsync(request.EmployeeId, cancellationToken);

            if (employee == null)
            {
                throw new BadRequestException(); // employee to add to department not found
            }

            if (department.Employees.Any(e => e.Guid == employee.Guid))
            {
                throw new ConflictException("employee is already added to this department"); // employee is already added to this department
            }

            department.Employees.Add(employee);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #7
0
        protected async override Task Handle(RoleDeleteRequest request, CancellationToken cancellationToken)
        {
            var roleToDelete = await _context.Roles.FindByGuidAsync(request.RoleId, cancellationToken);

            if (roleToDelete == null)
            {
                throw new BadRequestException(); // role to delete not found
            }

            _context.Roles.Remove(roleToDelete);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #8
0
        protected async override Task Handle(DepartmentDeleteRequest request, CancellationToken cancellationToken)
        {
            var department = await _context.Departments.FindByGuidAsync(request.RoleId, cancellationToken);

            if (department == null)
            {
                throw new BadRequestException(); // department to delete not found
            }

            _context.Departments.Remove(department);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #9
0
        protected async override Task Handle(ProductDeleteRequest request, CancellationToken cancellationToken)
        {
            var product = await _context.Products.FindByGuidAsync(request.ProductGuid, cancellationToken);

            if (product == null)
            {
                throw new BadRequestException(); // product coulnd't be found => cant delete product
            }

            _context.Products.Remove(product);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #10
0
        protected async override Task Handle(EmployeeDeleteRequest request, CancellationToken cancellationToken)
        {
            var employee = await _context.Employees.FindByGuidAsync(request.EmployeeId, cancellationToken);

            if (employee == null)
            {
                throw new BadRequestException(); // employee to delete not found
            }

            _context.Employees.Remove(employee);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #11
0
        protected async override Task Handle(ProductTypeDeleteRequest request, CancellationToken cancellationToken)
        {
            var productType = await _context.ProductTypes.FindByGuidAsync(request.ProductTypeGuid, cancellationToken);

            if (productType == null)
            {
                throw new BadRequestException(); //  product type to delete not found
            }

            _context.ProductTypes.Remove(productType);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #12
0
        protected async override Task Handle(RoleRemoveFromDepartmentRequest request, CancellationToken cancellationToken)
        {
            var linkRoleDepartment = await _context.RolesDepartments.FindAsync(
                keyValues : new object[] { request.RoleId, request.DepartmentId },
                cancellationToken);

            if (linkRoleDepartment == null)
            {
                throw new BadRequestException(); // department doesn't have this role => can't be removed
            }

            _context.RolesDepartments.Remove(linkRoleDepartment);

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #13
0
        protected async override Task Handle(ProductTypeRemoveChildrenRequest request, CancellationToken cancellationToken)
        {
            var linksProductTypesProductTypes = await _context.ProductTypesProductTypes
                                                .Where(pt => request.ChildIds.Any(ci => ci == pt.ChildGuid) &&
                                                       pt.ParentGuid == request.BaseProductTypeId)
                                                .ToListAsync(cancellationToken);

            if (linksProductTypesProductTypes.Count == 0 || linksProductTypesProductTypes.Count != request.ChildIds.Count())
            {
                throw new BadRequestException(); // parent product type doesn't have one or more of the roles which should be removed
            }

            linksProductTypesProductTypes.ForEach(lpt => _context.ProductTypesProductTypes.Remove(lpt));

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #14
0
        public async Task <RoleResponse> Handle(RoleUpdateRequest request, CancellationToken cancellationToken)
        {
            var role = await _context.Roles.FindByGuidAsync(request.RoleId, cancellationToken);

            if (role == null)
            {
                throw new BadRequestException(); // role to update not found
            }

            role.Name        = request.Name;
            role.Description = request.Description;

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <Role, RoleResponse>(role));
        }
Example #15
0
        protected async override Task Handle(ProductTypeRemoveParentsRequest request, CancellationToken cancellationToken)
        {
            var linksParentsBase = await _context.ProductTypesProductTypes
                                   .Where(pt => pt.ChildGuid == request.BaseProductTypeId &&
                                          request.ParentIds.Any(pi => pi == pt.ParentGuid))
                                   .ToListAsync(cancellationToken);

            if (linksParentsBase.Count == 0 || linksParentsBase.Count != request.ParentIds.Count())
            {
                throw new BadRequestException(); // link between base and parents not found, can't remove relation
            }

            linksParentsBase.ForEach(lpb => _context.ProductTypesProductTypes.Remove(lpb));

            await _context.SaveChangesAsync(cancellationToken);
        }
        protected async override Task Handle(RoleRemoveFromEmployeeRequest request, CancellationToken cancellationToken)
        {
            var linksRolesEmployees = await _context.RolesEmployees
                                      .Where(re => request.RoleIds.Any(ri => ri == re.RoleGuid) &&
                                             re.EmployeeGuid == request.EmployeeId)
                                      .ToListAsync(cancellationToken);

            // check if all roles which should be removed could be found
            if (linksRolesEmployees.Count == 0 || linksRolesEmployees.Count != request.RoleIds.Count())
            {
                throw new BadRequestException(); // department doesn't have these roles => can't be removed
            }

            linksRolesEmployees.ForEach(lre => _context.RolesEmployees.Remove(lre));

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #17
0
        public async Task <ProductResponse> Handle(ProductUpdateRequest request, CancellationToken cancellationToken)
        {
            var product = await _context.Products.FindByGuidAsync(request.ProductGuid, cancellationToken);

            if (product == null)
            {
                throw new BadRequestException(); // product to update couldn't be found => cant update product
            }

            product.Description     = request.Description;
            product.State           = request.State;
            product.CompartmentGuid = request.CompartmentGuid;

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <ProductResponse>(product));
        }
Example #18
0
        public async Task <DepartmentCreateResponse> Handle(DepartmentCreateRequest request, CancellationToken cancellationToken)
        {
            // Check if title of the department is unique
            if (_context.Departments.Where(d => d.Title == request.Title).Count() > 0)
            {
                throw new ConflictException(nameof(request.Title), $"A department with the title: {request.Title} already exists, please choose another name");
            }

            var newDepartment = _mapper.Map <DepartmentCreateRequest, Department>(request);

            await _context.Departments.AddAsync(newDepartment, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(new DepartmentCreateResponse()
            {
                DepartmentId = newDepartment.Guid
            });
        }
Example #19
0
        public async Task <RoleCreateResponse> Handle(RoleCreateRequest request, CancellationToken cancellationToken)
        {
            // Check if name of the role is unique
            if (_context.Roles.Where(r => r.Name == request.Name).Count() > 0)
            {
                throw new ConflictException(nameof(request.Name), $"A role with the name: {request.Name} already exists, please choose another name");
            }

            var newRole = _mapper.Map <RoleCreateRequest, Role>(request);

            await _context.Roles.AddAsync(newRole, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(new RoleCreateResponse()
            {
                RoleId = newRole.Guid
            });
        }
Example #20
0
        public async Task <EmployeeCreateResponse> Handle(EmployeeCreateRequest request, CancellationToken cancellationToken)
        {
            var employeesWithSimilarUsernameEmail = await _context.Employees
                                                    .Where(e => e.Username == request.Username || e.Email == request.Email)
                                                    .ToListAsync(cancellationToken);

            // check if username is available / unique
            if (employeesWithSimilarUsernameEmail.Any(e => e.Username == request.Username))
            {
                throw new ConflictException(nameof(request.Username), "Chosen username isn't available, please take another one.");
            }

            // check if email is available / unique
            if (employeesWithSimilarUsernameEmail.Any(e => e.Email == request.Email))
            {
                throw new ConflictException(nameof(request.Email), "Chosen email isn't available, please take another one.");
            }

            string hashedPassword = _hasher.HashPassword(request.Password);

            Employee newEmployee = new Employee()
            {
                Birthday    = request.Birthday,
                Email       = request.Email,
                Firstname   = request.Firstname,
                Lastname    = request.Lastname,
                PhoneNumber = request.PhoneNumber,
                Username    = request.Username,
                Password    = hashedPassword
            };

            await _context.AddAsync(newEmployee, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            _logger.LogDebug($"Successfully created user {newEmployee.Username} with the id {newEmployee.Guid}");

            return(new EmployeeCreateResponse()
            {
                EmployeeId = newEmployee.Guid
            });
        }
Example #21
0
        public async Task <DepartmentResponse> Handle(DepartmentUpdateRequest request, CancellationToken cancellationToken)
        {
            var department = await _context.Departments.FindByGuidAsync(request.DepartmentId, cancellationToken);

            if (department == null)
            {
                throw new BadRequestException(); // department to update not found
            }

            // Check if title of the department is unique
            if (_context.Departments.Where(d => d.Title == request.Title).Count() > 0)
            {
                throw new ConflictException(nameof(request.Title), $"A department with the title: {request.Title} already exists, please choose another name");
            }

            department.Title       = request.Title;
            department.Description = request.Description;

            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <Department, DepartmentResponse>(department));
        }
Example #22
0
        public async Task <ProductTypeCreateResponse> Handle(ProductTypeCreateRequest request, CancellationToken cancellationToken)
        {
            // check if name is unique
            if (await _context.ProductTypes.Select(pt => pt.Name).AnyAsync(n => n == request.Name, cancellationToken))
            {
                throw new ConflictException(nameof(request.Name), $"A product type with the name: {request.Name} already exists, please choose another name");
            }

            var newProductType = _mapper.Map <ProductTypeCreateRequest, ProductType>(request);

            if (request.ChildGuids?.Count() > 0)
            {
                var children = await _context.ProductTypesProductTypes
                               .Where(pt => request.ChildGuids.Contains(pt.ParentGuid))
                               .ToListAsync(cancellationToken);

                children.ForEach(c => newProductType.Children.Add(c));
            }

            if (request.ParentGuids?.Count() > 0)
            {
                var parents = await _context.ProductTypesProductTypes
                              .Where(pt => request.ParentGuids.Contains(pt.ChildGuid))
                              .ToListAsync(cancellationToken);

                parents.ForEach(p => newProductType.Parents.Add(p));
            }

            await _context.ProductTypes.AddAsync(newProductType, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(new ProductTypeCreateResponse()
            {
                ProductTypeGuid = newProductType.Guid
            });
        }
Example #23
0
        protected async override Task Handle(RoleAddToEmployeeRequest request, CancellationToken cancellationToken)
        {
            // Load all requested roles including the employees which have these roles
            var roles = await _context.Roles.Where(r => request.RoleIds.Any(ri => ri == r.Guid))
                        .Include(r => r.Employees)
                        .Include(r => r.Departments)
                        .ToListAsync(cancellationToken);

            // check if count of requested roles match the count of found roles
            if (roles.Count == 0 || roles.Count != request.RoleIds.Count())
            {
                throw new BadRequestException(); // not all roles to add not found
            }

            var employee = await _context.Employees.FindByGuidAsync(request.EmployeeId, cancellationToken);

            if (employee == null)
            {
                throw new BadRequestException(); // employee to add role not found
            }

            roles = roles.Where(r => !r.Employees.Any(re => re.EmployeeGuid == employee.Guid) &&
                                !r.Departments.Any(rd => rd.DepartmentGuid == employee.DepartmentGuid))
                    .ToList(); // filter roles which the employee (including via the department) doesnt have already

            roles.ForEach(r =>
            {
                r.Employees.Add(new RoleEmployee()
                {
                    Employee = employee,
                    Role     = r
                });
            });

            await _context.SaveChangesAsync(cancellationToken);
        }
Example #24
0
        public async Task<ProductCreateResponse> Handle(ProductCreateRequest request, CancellationToken cancellationToken)
        {
            var responseData = new List<ProductCreateResponseModel>();

            foreach (var productModel in request.Products)
            {
                var product = new Product()
                {
                    Description = productModel.Description,
                    SerialNumber = productModel.SerialNumber,
                    State = ProductState.InStock
                };

                product.Type = await _context.ProductTypes.FindByGuidAsync(productModel.TypeGuid, cancellationToken);

                if (product.Type == null)
                {
                    throw new BadRequestException(); // cant create product => product type not found
                }

                product.Compartment = await _context.Compartments.FindByGuidAsync(productModel.CompartmentGuid, cancellationToken);

                if (product.Compartment == null)
                {
                    throw new BadRequestException(); // cant create product => compartment not found
                }

                var parentProduct = productModel.ParentGuid != null ? 
                    await _context.Products.FindByGuidAsync(new Guid(productModel.ParentGuid.ToString()), cancellationToken) : null;

                if (parentProduct != null) // only add parent if found or parent id is not null
                {
                    product.Parent = parentProduct;
                }

                // add child products if existing
                if (productModel.ChildrenGuids != null && productModel.ChildrenGuids.Any())
                {
                    foreach (var childGuid in productModel.ChildrenGuids)
                    {
                        var child = await _context.Products.FindByGuidAsync(childGuid, cancellationToken);

                        if (child != null)
                        {
                            product.Children.Add(child);
                        }
                    }
                }

                await _context.Products.AddAsync(product, cancellationToken);

                responseData.Add(new ProductCreateResponseModel()
                {
                    ProductGuid = product.Guid,
                    SerialNumber = product.SerialNumber
                });
            }

            await _context.SaveChangesAsync(cancellationToken);

            return new ProductCreateResponse() { Products =  responseData };
        }