Exemplo n.º 1
0
        public async Task <ActionResult <CompanyLightDTO> > PostCompany([FromBody] CompanyFullDTO companyModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    _logger.LogTrace("New company could not be posted due to invalid parameters.", companyModel);
                    return(BadRequest("Parameters Invalid."));
                }

                if (companyModel.PrimarySector == companyModel.SecondarySector)
                {
                    _logger.LogError($"Company posted with duplicate sectors");
                    return(BadRequest("Primary and secondary industries cannot be the same."));
                }

                switch (companyModel.Structure.ToLower())
                {
                case "multinational":
                {
                    if (!companyModel.Country.Equals("xx", StringComparison.OrdinalIgnoreCase))
                    {
                        _logger.LogError($"National company posted with invalid address details: " +
                                         $"Country: {companyModel.Country}");
                        return(BadRequest("Invalid country entry for multinational company."));
                    }

                    companyModel.StreetNumber = null;
                    companyModel.StreetName   = null;
                    companyModel.City         = null;
                    companyModel.Region       = null;
                    companyModel.PostCode     = null;
                    companyModel.Longitude    = null;
                    companyModel.Latitude     = null;

                    break;
                }

                case "national":
                {
                    if (companyModel.Country.Equals("xx", StringComparison.OrdinalIgnoreCase))
                    {
                        _logger.LogError($"National company posted with invalid address details: " +
                                         $"Country: {companyModel.Country}");
                        return(BadRequest("National companies must have a valid country."));
                    }

                    companyModel.StreetNumber = null;
                    companyModel.StreetName   = null;
                    companyModel.City         = null;
                    companyModel.Region       = null;
                    companyModel.PostCode     = null;
                    companyModel.Longitude    = null;
                    companyModel.Latitude     = null;

                    break;
                }

                case "regional":
                {
                    if (companyModel.Country.Equals("xx", StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(companyModel.Region))
                    {
                        _logger.LogError($"Regional company posted with invalid address details: " +
                                         $"Country: {companyModel.Country}, Region: {companyModel.Region}");
                        return(BadRequest("Regional companies must have a valid country and region."));
                    }

                    companyModel.StreetNumber = null;
                    companyModel.StreetName   = null;
                    companyModel.City         = null;
                    companyModel.PostCode     = null;
                    companyModel.Longitude    = null;
                    companyModel.Latitude     = null;

                    break;
                }

                case "local":
                {
                    if (companyModel.Country.Equals("xx", StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(companyModel.Region) ||
                        string.IsNullOrWhiteSpace(companyModel.City) || string.IsNullOrWhiteSpace(companyModel.StreetName))
                    {
                        _logger.LogError($"Local company posted with invalid address details: " +
                                         $"Country: {companyModel.Country}, Region: {companyModel.Region}, City: {companyModel.City}, Street: {companyModel.StreetName}");
                        return(BadRequest("Local companies must have a valid country, region, city and street."));
                    }

                    var alignments = Company.CheckBrandAlignment(companyModel.Name, companyModel.Brands, companyModel.POIName);
                    if (alignments[0] != companyModel.CompanyAligns)
                    {
                        _logger.LogError("Server and client checks for company name disagree.");
                        return(BadRequest("Company name identification mismatch."));
                    }
                    if (alignments[1] != companyModel.BrandAligns)
                    {
                        _logger.LogError("Server and client checks for company brand disagree.");
                        return(BadRequest("Company brand identification mismatch."));
                    }
                    break;
                }

                default:
                {
                    _logger.LogWarning("Company posted with invalid structure.");
                    return(BadRequest("Company structure not valid."));
                }
                }

                if (!companyModel.HasBranches)
                {
                    companyModel.BranchDescriptor = null;
                }

                var user = await _unitOfWork.UserManager.FindVerifiedByNameAsync(User.Identity.Name).ConfigureAwait(false);

                if (user == null)
                {
                    _logger.LogWarning("Company creation attempted with unknown or unverified user.");
                    return(StatusCode(500, "Failed to save new company."));
                }

                var newCompany = _mapper.Map <CompanyFullDTO, Company>(companyModel);
                newCompany.CreatedBy = user;

                _unitOfWork.Companies.Add(newCompany);
                await _unitOfWork.Complete().ConfigureAwait(false);

                _logger.LogInformation("New company {company} created", newCompany);

                return(CreatedAtAction("GetCompany", new { id = newCompany.Id }, _mapper.Map <Company, CompanyLightDTO>(newCompany)));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogError(ex, "Exception thrown attempting to save new company to database.");
                return(StatusCode(500, "Failed to save new company."));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to post new company.");
                return(StatusCode(500, "Failed to save new company."));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <CompanyLightDTO> > > GetSimilarCompanies([FromBody] CompanyFullDTO company, [FromQuery] PaginationQueryDTO model)
        {
            try
            {
                if (company == null || !ModelState.IsValid)
                {
                    _logger.LogTrace("Similar companies search failed to return due to invalid parameters.", company, model);
                    return(BadRequest("Invalid parameters."));
                }

                var user = await _unitOfWork.UserManager.FindVerifiedByNameAsync(User.Identity.Name).ConfigureAwait(false);

                if (user == null)
                {
                    _logger.LogWarning("Company similarity request attempted by unknown or unverified user.");
                    return(StatusCode(500, "Failed to return existing companies."));
                }

                var results = await _unitOfWork.Companies.GetSimilarCompaniesAsync(company, model).ConfigureAwait(false);

                Response.Headers.Add("Total-Count", results.Count.ToString());
                _logger.LogInformation("Returned {count} results for companies similar to {company}", results.Count, company);

                return(Ok(_mapper.Map <IEnumerable <CompanyLightDTO> >(results.Companies)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to return existing companies.");
                return(StatusCode(500, "Failed to return existing companies."));
            }
        }