public async Task <ServiceResponse <GetLostDogDto> > AddLostDog(UploadLostDogDto lostDogDto, IFormFile picture)
        {
            var serviceResponse = new ServiceResponse <GetLostDogDto>();
            var lostDog         = mapper.Map <LostDog>(lostDogDto);

            byte[] data;

            if (picture is null)
            {
                serviceResponse.Successful = false;
                serviceResponse.StatusCode = StatusCodes.Status400BadRequest;
                serviceResponse.Message    = "No picture was provided!";
            }
            else
            {
                var pictureValidationResult = securityService.IsPictureValid(picture);

                if (pictureValidationResult.Successful)
                {
                    using (var ms = new MemoryStream())
                    {
                        picture.CopyTo(ms);
                        data = ms.ToArray();
                    }
                    lostDog.Picture = new PictureDog()
                    {
                        FileName = picture.FileName,
                        FileType = picture.ContentType,
                        Data     = data
                    };
                    var repoResponse = await lostDogDataRepository.AddLostDog(lostDog);

                    serviceResponse = mapper.Map <RepositoryResponse <LostDog>, ServiceResponse <GetLostDogDto> >(repoResponse);
                    if (!serviceResponse.Successful)
                    {
                        serviceResponse.StatusCode = StatusCodes.Status400BadRequest;
                    }
                }
                else
                {
                    serviceResponse.Successful = false;
                    serviceResponse.StatusCode = StatusCodes.Status400BadRequest;
                    serviceResponse.Message    = pictureValidationResult.Message;
                }
            }

            return(serviceResponse);
        }
Пример #2
0
        public async void AddingLostDogSuccessfulForValidDogInfo()
        {
            var saveDog = new LostDog()
            {
                Breed       = "dogdog",
                Age         = 5,
                Size        = "Large, very large",
                Color       = "Orange but a bit yellow and green dots",
                SpecialMark = "tattoo of you on the neck",
                Name        = "Cat",
                Picture     = new PictureDog()
                {
                    FileName = "photo",
                    FileType = "png",
                    Data     = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
                },
                HairLength = "Long",
                EarsType   = "Short",
                TailLength = "None",
                Behaviors  = new List <DogBehavior>()
                {
                    new DogBehavior()
                    {
                        Behavior = "Angry"
                    }
                },
                Location = new LocationDog()
                {
                    City = "Biała", District = "Lol ther's none"
                },
                DateLost = new DateTime(2021, 3, 20),
                OwnerId  = 1,
                Comments = new List <LostDogComment>()
            };
            var result = await lostDogRepository.AddLostDog(saveDog);

            Assert.True(result.Successful);
        }