Exemplo n.º 1
0
        public async Task <ActionResult> CreateCharityInfo(Guid id, [FromForm] CharityInfoCreateDto charityDto)
        {
            // Check if the donor already exists
            var charityResponse = await CharitableEntityApplication.GetCharity((c) => c.Id.Equals(id), true);

            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 informações da entidade beneficente já foram cadastradas.");
                return(BadRequest(error));
            }
            else
            {
                if (charityResponse.Status.ToUpper() != ApproverStatus.APPROVED.ToString() ||
                    charityResponse.Active == false)
                {
                    ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"Não foi possível cadastrar as informações da entidade benefiecente porque a entidade não está ativa ou aprovada.");
                    return(BadRequest(error));
                }
            }

            await CharitableInformationApplication.CreateCharityInfo(id, charityDto, Request);

            return(CreatedAtRoute("GetCharityById", new { id }, null));;
        }
        public async Task <Guid> CreateCharityInfo(Guid charitableEntityId, CharityInfoCreateDto charityInfoDto, HttpRequest request)
        {
            var charitableInfo = this.Mapper.Map <CharitableInformation>(charityInfoDto);

            charitableInfo.CharitableEntityId = charitableEntityId;
            charitableInfo.Id = Guid.NewGuid();

            charitableInfo.PicturePath = null;
            charitableInfo.PictureUrl  = null;
            charitableInfo.Photo01     = null;
            charitableInfo.Photo02     = null;

            var shortId = charitableInfo.Id.ToString().Split('-')[0];

            var picture = await UploadImage(charityInfoDto.Picture, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_picture{new FileInfo(charityInfoDto.Picture.FileName).Extension}"));

            var photo01 = await UploadImage(charityInfoDto.Photo01, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_photo1{new FileInfo(charityInfoDto.Picture.FileName).Extension}"));

            var photo02 = await UploadImage(charityInfoDto.Photo02, request, string.Format($"{shortId}_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_photo2{new FileInfo(charityInfoDto.Picture.FileName).Extension}"));

            charitableInfo.PicturePath = picture.Item1;
            charitableInfo.PictureUrl  = picture.Item2;

            charitableInfo.Photo01 = new Image()
            {
                ImagePath = photo01.Item1,
                ImageUrl  = photo01.Item2,
                Title     = charityInfoDto.TitlePhoto01
            };

            charitableInfo.Photo02 = new Image()
            {
                ImagePath = photo02.Item1,
                ImageUrl  = photo02.Item2,
                Title     = charityInfoDto.TitlePhoto02
            };

            try
            {
                await this.Repository.AddAsync(charitableInfo);

                await this.Repository.SaveAsync();
            }
            catch (Exception)
            {
                try
                {
                    deleteAllImages(charitableInfo);
                }
                catch (Exception) {}

                throw;
            }

            return(charitableInfo.Id);
        }