Exemplo n.º 1
0
        public async Task InsertNewImage()
        {
            //Arrange
            Image image = new Image();

            image.Id       = 0;
            image.RecipeId = 1;
            image.FileName = "image1";

            //Act
            var addResult = await imageRepository.AddAsync(image);

            //Assert
            Assert.IsNotNull(addResult);
            Assert.AreEqual(image.FileName, addResult.FileName);
            Assert.AreEqual(image.RecipeId, addResult.RecipeId);
        }
        public async Task ImageRepo_Add_AddsOneImage()
        {
            var repository = new ImageRepository(_context);

            var expected = new Image {
                ImageId    = 6,
                Caption    = "Caption",
                Name       = "Image6",
                Link       = "/image6",
                CategoryId = 1,
                UserRefId  = "user"
            };
            await repository.AddAsync(expected);

            _context.SaveChanges();

            var result = (await repository.GetAllAsync()).Last();

            Assert.Equal(expected.ImageId, result.ImageId);
            Assert.Equal(expected.Name, result.Name);
        }
        public async Task <ActionResult <ImageFile> > PostImage([FromBody] Image image)
        {
            ImageFile imageFile = null;

            try
            {
                var library = await LibraryRepository.GetBySourceAsync(image.ImageFile.LibraryFullName);

                if (library == null)
                {
                    return(BadRequest("Library doesn't exist"));
                }

                var userId = User?.Identity.Name;
                if (!library.Owners.Where(x => x.ToString() == userId).Any())
                {
                    return(Unauthorized());
                }

                imageFile = await ImageRepository.AddAsync(image);

                library.Images.Add(imageFile);
                await LibraryRepository.UpdateAsync(library);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }
            catch (ContentNotFoundException e)
            {
                return(NotFound(new { message = e.Message }));
            }
            catch (Exception e)
            {
                Logger.LogError(e, e.Message);
                return(StatusCode(500));
            }

            return(CreatedAtAction("GetImage", new { name = imageFile.Name }, imageFile));
        }