public async Task <ActionResult> DeleteConfirmed(int id)
        {
            Ad ad = await repository.FetchbyAdId((int)id);

            await DeleteAdBlobsAsync(ad);

            await repository.DeleteAd(id);

            Trace.TraceInformation("Deleted ad {0}", ad.AdId);
            return(RedirectToAction("Index"));
        }
Beispiel #2
0
        public async Task <ActionResult <AdDto> > DeleteAd(string userid, string projectid, string adid)
        {
            if (!ExternalIdPassedGuidValidation(userid))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(userid)}."));
            }

            if (!ExternalIdPassedGuidValidation(projectid))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(projectid)}."));
            }

            if (!ExternalIdPassedGuidValidation(adid))
            {
                return(StatusCode(StatusCodes.Status400BadRequest, $"Invalid {nameof(adid)}."));
            }

            Ad ad;

            try
            {
                ad = await adsRepository.GetAdAsync(adid);
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
            }

            bool userHaveAccessToProject;

            try
            {
                userHaveAccessToProject = await UserHaveAccessToProject(userid, projectid);
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }

            if (!userHaveAccessToProject)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Access to project ads denied."));
            }

            try
            {
                adsRepository.DeleteAd(ad);
                await adsRepository.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }

            AdDto adDto;

            try
            {
                adDto = mapper.Map <AdDto>(ad);
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }

            return(Ok(adDto));
        }