Esempio n. 1
0
        public async Task <IActionResult> ChangeReviewVisibility([FromBody] AdminVisibilityDTO model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Failed to change review visibility - invalid parameters."));
                }

                var admin = await _unitOfWork.UserManager.FindAdminByName(User.Identity.Name).ConfigureAwait(false);

                if (admin == null || model.Password != _config["Admin:ApiPassword"])
                {
                    _logger.LogWarning("Unauthorised user attempted to change review visibility!");
                    return(BadRequest("Unauthorised access."));
                }

                Guid guid   = (ShortGuid)model.EntityID;
                var  review = await _unitOfWork.Reviews.GetReviewById(guid, false, false).ConfigureAwait(false);

                if (review == null)
                {
                    return(BadRequest("Review not found"));
                }

                Report report = null;
                if (model.ReportId > 0)
                {
                    report = await _unitOfWork.Reports.GetReportById(model.ReportId).ConfigureAwait(false);

                    if (report == null)
                    {
                        return(BadRequest("Report not found."));
                    }
                }

                if (model.Hide)
                {
                    _unitOfWork.Reviews.HideReview(review, admin, RemovedReason.Delete, report);
                }
                else
                {
                    _unitOfWork.Reviews.UnhideReview(review);
                }

                if (review.HasRatings())
                {
                    if (review.Branch != null)
                    {
                        var reviewedBranch = await _unitOfWork.Branches.GetBranchById(review.Branch.Guid, true).ConfigureAwait(false);

                        if (reviewedBranch == null)
                        {
                            return(BadRequest("Failed to adjust review visibility due to reviewed branch not being found."));
                        }

                        _unitOfWork.Branches.ModifyBranchReviewsAsync(reviewedBranch, review, model.Hide ? Operation.Removal : Operation.Addition);
                    }

                    var reviewedCompany = await _unitOfWork.Companies.GetCompanyById(review.Company.Guid, false, true, true, false).ConfigureAwait(false);

                    if (reviewedCompany == null)
                    {
                        return(BadRequest("Failed to adjust review visibility due to reviewed company not being found."));
                    }

                    _unitOfWork.Companies.ModifyCompanyReviewsAsync(reviewedCompany, review, model.Hide ? Operation.Removal : Operation.Addition);
                }

                await _unitOfWork.Complete().ConfigureAwait(false);

                _logger.LogInformation("Admin changed review visibility to " + (model.Hide ? "hidden" : "visible") + $": {review}");
                return(NoContent());
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogError(ex, "Database Exception thrown attempting to change reviews visibility.");
                return(StatusCode(500, "Database exception prevented changing reviews visibility."));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception thrown attempting to change review's visibility.");
                return(StatusCode(500, "Exception prevented changing review's visibility."));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> ChangeCompanyVisibility([FromBody] AdminVisibilityDTO model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Failed to change company visibility - invalid parameters."));
                }

                var admin = await _unitOfWork.UserManager.FindAdminByName(User.Identity.Name).ConfigureAwait(false);

                if (admin == null || model.Password != _config["Admin:ApiPassword"])
                {
                    _logger.LogWarning("Unauthorised user attempted to change company visibility!");
                    return(BadRequest("Unauthorised access."));
                }

                Guid guid    = (ShortGuid)model.EntityID;
                var  company = await _unitOfWork.Companies.GetCompanyById(guid, false, false, true, false).ConfigureAwait(false);

                if (company == null)
                {
                    return(BadRequest("Company not found."));
                }

                Report report = null;
                if (model.ReportId > 0)
                {
                    report = await _unitOfWork.Reports.GetReportById(model.ReportId).ConfigureAwait(false);

                    if (report == null)
                    {
                        return(BadRequest("Report not found."));
                    }
                }

                if (model.Hide)
                {
                    _unitOfWork.Companies.HideCompany(company, admin, RemovedReason.Delete, report);
                }
                else
                {
                    _unitOfWork.Companies.UnhideCompany(company);
                }

                await _unitOfWork.Complete().ConfigureAwait(false);

                _logger.LogInformation("Admin changed company visibility to " + (model.Hide ? "hidden" : "visible") + $": {company}");
                return(NoContent());
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogError(ex, "Database Exception thrown attempting to change company's visibility.");
                return(StatusCode(500, "Database exception prevented changing company's visibility."));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception thrown attempting to change company's visibility.");
                return(StatusCode(500, "Exception prevented changing company's visibility."));
            }
        }