public Company Get(int ID)
        {
            IQuery  query = new GetCompanyQuery();
            Company res   = null;

            try
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    //reducing into one object
                    connection.Query <Company, AdressOfDepartment, Company>(
                        query.Sql,
                        (company, adress) =>
                    {
                        if (res == null)
                        {
                            res = company;
                        }
                        res.AddAdress(adress);

                        return(company);
                    },
                        splitOn: "ID",
                        param: new { ID })
                    .First();
                }
            } catch (Exception e)
            {
                return(null);
            }

            return(res);
        }
Exemple #2
0
        public async Task <ActionResult <IEnumerable <CompanyDto> > > GetAll()
        {
            var query  = new GetCompanyQuery();
            var result = await Mediator.Send(query);

            return(Ok(result));
        }
        public async Task <DeleteCompanyByIdResponse> Handle(DeleteCompanyByIdRequest request, CancellationToken cancellationToken)
        {
            if (request.AuthenticatorRole == AppRole.Employee)
            {
                return(new DeleteCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.Unauthorized)
                });
            }

            var query = new GetCompanyQuery()
            {
                Id = request.CompanyId
            };
            var company = await queryExecutor.Execute(query);

            if (company == null)
            {
                return(new DeleteCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var command = new DeleteCompanyCommand()
            {
                Parameter = company
            };
            var deletedCompany = await commandExecutor.Execute(command);

            return(new DeleteCompanyByIdResponse
            {
                Data = deletedCompany
            });
        }
Exemple #4
0
        public async Task <AddEmployeeResponse> Handle(AddEmployeeRequest request, CancellationToken cancellationToken)
        {
            if (request.AuthenticatorRole == AppRole.Employee)
            {
                return(new AddEmployeeResponse()
                {
                    Error = new ErrorModel(ErrorType.Unauthorized)
                });
            }

            var employeeQuery = new GetUserQuery <Employee>()
            {
                Login = request.Login
            };
            var availableManager = await queryExecutor.Execute(employeeQuery);

            if (availableManager != null)
            {
                return(new AddEmployeeResponse()
                {
                    Error = new ErrorModel(ErrorType.Conflict)
                });
            }

            var query = new GetCompanyQuery()
            {
                Id        = request.CompanyId,
                CompanyId = request.AuthenticatorCompanyId
            };
            var company = await queryExecutor.Execute(query);

            if (company == null)
            {
                return(new AddEmployeeResponse
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var auth = passwordHasher.Hash(request.Password);

            request.Password = auth[0];
            request.Salt     = auth[1];
            var employee = mapper.Map <Employee>(request);

            employee.Board = new Board();

            var command = new AddEmployeeCommand()
            {
                Parameter = employee
            };
            var employeeFromDb = await commandExecutor.Execute(command);

            employeeFromDb.Company.EmployeesList = null;
            employeeFromDb.Company.Manager       = null;
            return(new AddEmployeeResponse()
            {
                Data = mapper.Map <EmployeeDto>(employeeFromDb)
            });
        }
Exemple #5
0
        public async Task <PutEmployeeByIdResponse> Handle(PutEmployeeByIdRequest request, CancellationToken cancellationToken)
        {
            var query = new GetEmployeeQuery()
            {
                Id        = request.Id,
                CompanyId = request.AuthenticatorCompanyId
            };

            var query2 = new GetCompanyQuery()
            {
                Id        = request.CompanyId,
                CompanyId = request.AuthenticatorCompanyId
            };

            var employee = await queryExecutor.Execute(query);

            var company = await queryExecutor.Execute(query2);

            if (request.Login == null || request.Password != null)
            {
                request.Login = employee.Login;
                var auth = passwordHasher.Hash(request.Password);
                request.Password = auth[0];
                request.Salt     = auth[1];
            }

            if (request.Login == null || request.Password == null)
            {
                request.Login    = employee.Login;
                request.Password = employee.Password;
                request.Salt     = employee.Salt;
            }

            if (employee == null || company == null)
            {
                return(new PutEmployeeByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var updateEmployee = mapper.Map <Employee>(request);
            var command        = new PutEmployeeCommand
            {
                Parameter = updateEmployee
            };
            var updatedEmployee = await commandExecutor.Execute(command);

            var response = mapper.Map <EmployeeDto>(updatedEmployee);

            return(new PutEmployeeByIdResponse
            {
                Data = response
            });
        }
Exemple #6
0
        public async Task <PutCompanyByIdResponse> Handle(PutCompanyByIdRequest request, CancellationToken cancellationToken)
        {
            if (request.AuthenticatorRole == AppRole.Employee)
            {
                return(new PutCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.Unauthorized)
                });
            }

            var query = new GetCompanyQuery()
            {
                Id        = request.Id,
                CompanyId = request.AuthenticatorCompanyId
            };
            var query2 = new GetManagerQuery()
            {
                Id        = request.ManagerId,
                CompanyId = request.AuthenticatorCompanyId
            };

            var company = await queryExecutor.Execute(query);

            var manager = await queryExecutor.Execute(query2);

            if (company == null || manager == null)
            {
                return(new PutCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var updateCompany = mapper.Map <Company>(request);
            var command       = new PutCompanyCommand
            {
                Parameter = updateCompany
            };
            var updatedCompany = await commandExecutor.Execute(command);

            var response = mapper.Map <CompanyDto>(updatedCompany);

            return(new PutCompanyByIdResponse
            {
                Data = response
            });
        }
        public async Task <IActionResult> GetCompanyAsync([FromRoute] int companyId)
        {
            if (companyId < 1)
            {
                return(Error("Invalid Company Id"));
            }

            _logger.LogInformation("GetCompanyAsync Called.");

            var query = new GetCompanyQuery
            {
                CompanyId = companyId
            };

            var list = await _messages.Dispatch(query).ConfigureAwait(false);

            return(Ok(list));
        }
Exemple #8
0
        public async Task <UpdateCompanyByIdResponse> Handle(UpdateCompanyByIdRequest request, CancellationToken cancellationToken)
        {
            if (request.AuthenticationCompanyId != request.Id)
            {
                return(new UpdateCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.Forbidden)
                });
            }

            var query = new GetCompanyQuery()
            {
                Id = request.Id
            };
            var getCompany = await this.queryExecutor.Execute(query);

            if (getCompany == null)
            {
                return(new UpdateCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }
            var mappedCommand = this.mapper.Map <Company>(request);
            var command       = new UpdateCompanyCommand()
            {
                Parameter = mappedCommand
            };
            var updatedCompany = await this.commandExecutor.Execute(command);

            var response = new UpdateCompanyByIdResponse()
            {
                Data = this.mapper.Map <API.Domain.Models.Company>(updatedCompany)
            };

            return(response);
        }
Exemple #9
0
        public async Task <GetCompanyByIdResponse> Handle(GetCompanyByIdRequest request, CancellationToken cancellationToken)
        {
            var query = new GetCompanyQuery()
            {
                Id = request.Id
            };
            var companyFromDb = await this.queryExecutor.Execute(query);

            if (companyFromDb == null)
            {
                return(new GetCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var response = new GetCompanyByIdResponse()

            {
                Data = this.mapper.Map <API.Domain.Models.Company>(companyFromDb)
            };

            return(response);
        }
        public async Task <GetCompanyByIdResponse> Handle(GetCompanyByIdRequest request, CancellationToken cancellationToken)
        {
            var query = new GetCompanyQuery()
            {
                CompanyId = request.AuthenticatorCompanyId,
                Id        = request.CompanyId
            };
            var company = await queryExecutor.Execute(query);

            if (company == null)
            {
                return(new GetCompanyByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }

            var mappedCompany = mapper.Map <CompanyDto>(company);

            return(new GetCompanyByIdResponse
            {
                Data = mappedCompany
            });
        }
 public async Task <IEnumerable <CompanyDto> > Handle(GetCompanyQuery request, CancellationToken cancellationToken)
 {
     return(await _companyRepository.GetAll());
 }