예제 #1
0
        public async Task <ActionResult> UpdateCharityInfoImage(Guid id, [FromForm] CharityInfoImageUpdateDto charityDto)
        {
            // Check if the donor already exists
            var charityResponse = await CharitableEntityApplication.GetCharity((c) => c.Id.Equals(id), true);

            var imageName = charityDto.Name.ToLower();

            if (imageName != "picture" && imageName != "image01" && imageName != "image02")
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"O nome da imagem deve ser picture, image01 ou image02");
                return(BadRequest(error));
            }

            if (charityResponse == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"A entidade beneficente, {id}, não foi encontrada.");
                return(NotFound(error));
            }

            if (charityResponse.Information == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"As infomações da entidade beneficente não foram cadastradas.");
                return(BadRequest(error));
            }

            await CharitableInformationApplication.UpdateCharityInfoImage(id, charityDto, charityResponse.Information, Request);

            return(Ok());
        }
        public async Task <Guid> UpdateCharityInfoImage(Guid charitableEntityId, CharityInfoImageUpdateDto charityInfoDto,
                                                        CharityInfoResponseDto charityInfoResponseDto, HttpRequest request)
        {
            var charitableInformation = await Repository.GetByIdAsync(charityInfoResponseDto.Id);

            var    imageName = charityInfoDto.Name.ToLower();
            string oldImage  = string.Empty;

            var shortId = charitableInformation.Id.ToString().Split('-')[0];
            Tuple <string, string> result = null;

            switch (imageName)
            {
            case "picture":
                result = await UploadImage(charityInfoDto.Photo, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_picture{new FileInfo(charityInfoDto.Photo.FileName).Extension}"));

                oldImage = charitableInformation.PicturePath;

                charitableInformation.PicturePath = result.Item1;
                charitableInformation.PictureUrl  = result.Item2;
                break;

            case "image01":
                result = await UploadImage(charityInfoDto.Photo, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_photo1{new FileInfo(charityInfoDto.Photo.FileName).Extension}"));

                oldImage = charitableInformation.Photo01?.ImagePath;

                charitableInformation.Photo01 = new Image()
                {
                    ImagePath = result.Item1,
                    ImageUrl  = result.Item2,
                    Title     = charitableInformation?.Photo01?.Title
                };
                break;

            case "image02":
                result = await UploadImage(charityInfoDto.Photo, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_photo2{new FileInfo(charityInfoDto.Photo.FileName).Extension}"));

                oldImage = charitableInformation.Photo02?.ImagePath;

                charitableInformation.Photo02 = new Image()
                {
                    ImagePath = result.Item1,
                    ImageUrl  = result.Item2,
                    Title     = charitableInformation?.Photo02?.Title
                };
                break;
            }

            charitableInformation.CharitableEntityId = charitableEntityId;
            charitableInformation.Id = charityInfoResponseDto.Id;

            try
            {
                this.Repository.Udate(charitableInformation);
                await this.Repository.SaveAsync();
            }
            catch (Exception)
            {
                try
                {
                    deleteImage(imageName, charitableInformation);
                }
                catch (Exception) { }

                throw;
            }
            finally
            {
                try
                {
                    deleteImage(oldImage);
                }
                catch (Exception) { }
            }

            return(charitableInformation.Id);
        }