コード例 #1
0
        public IActionResult Post([FromBody] CatCreateModel value, IFormFile file)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            $"Bytes Exist:{value.Bytes != null}".ConsoleRed();
            $"File Exists:{file != null}".ConsoleRed();

            string fileName = $"{Guid.NewGuid()}.jpg";
            var    filePath = Path.Combine($"{Path.GetFullPath(ApplicationSettings.FileStorage.RequestPath)}/{fileName}");

            var catModel = new CatModel
            {
                Name        = value.Name,
                Description = value.Description,
                Photo       = fileName,
            };

            //_fileService.SaveFile(filePath, value.Bytes);
            // _catService.CreateCat(catModel);

            return(Ok());
        }
コード例 #2
0
        public async Task <ActionResult> Post([FromBody] CatCreateModel value)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            $"Bytes Exist:{value.Bytes != null}".ConsoleRed();

            string fileName = $"{Guid.NewGuid()}.jpg";
            var    filePath = Path.Combine(ApplicationSettings.FileStorage.PhysicalFilePath, fileName);

            _logger.LogInformation("Save Image: {FilePath}", filePath);

            var catModel = new CatModel
            {
                Name        = value.Name,
                Description = value.Description,
                Photo       = fileName,
            };

            _fileService.SaveFile(filePath, value.Bytes);
            var result = await _catService.CreateCat(catModel);

            catModel.Id = result;

            return(CreatedAtRoute("GetById", new { Id = result }, catModel));
        }
コード例 #3
0
        public IActionResult Post([FromBody] CatCreateModel value)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            $"Bytes Exist:{value.Bytes != null}".ConsoleRed();

            string fileName       = $"{Guid.NewGuid()}.jpg";
            string imageDirectory = ApplicationSettings.FileStorage.FilePath;
            var    filePath       = Path.Combine(_env.ContentRootPath, imageDirectory, fileName);

            var catModel = new CatModel
            {
                Name        = value.Name,
                Description = value.Description,
                Photo       = fileName,
            };

            _fileService.SaveFile(filePath, value.Bytes);
            var result = _catService.CreateCat(catModel);

            catModel.Id = result;

            return(CreatedAtRoute("GetById", new { Id = result }, catModel));
        }
コード例 #4
0
        public void Create_Cat_Invalid_ModelState()
        {
            // Arrange
            var cat = new CatCreateModel {
                Name = "Cat", Bytes = CreateSpecialByteArray(7000)
            };

            var controller = GetCatsController();

            controller.ModelState.AddModelError("Description", "Required");

            //Act
            var sut = controller.Post(cat);

            //Assert
            var badRequestResult = Assert.IsType <UnprocessableEntityObjectResult>(sut);

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }
コード例 #5
0
        public void Create_Cat()
        {
            // Arrange
            var name        = "Fun Cat";
            var description = "The Fun Cat";
            var id          = 9999;

            var cat = new CatCreateModel {
                Name = name, Description = description, Bytes = CreateSpecialByteArray(7000)
            };
            var catModel = new CatModel {
                Name = "Cat", Description = "Cat"
            };
            var mockCatService = new Mock <ICatService>();

            mockCatService
            .Setup(mr => mr.CreateCat(catModel))
            .Returns((long)id);

            var mockFileService = new Mock <IFileService>();

            mockFileService
            .Setup(mr => mr.SaveFile(It.IsAny <string>(), cat.Bytes))
            .Verifiable();

            var controller = GetCatsController(mockCatService.Object, mockFileService.Object);

            // Act
            var sut = controller.Post(cat);

            // Assert
            Assert.IsType <CreatedAtRouteResult>(sut);
            Assert.Equal($"Name:{name}|Description:{description}", cat.ToString());

            var objectResult = sut as CreatedAtRouteResult;

            Assert.NotNull(objectResult);
            Assert.True(objectResult.StatusCode == 201);
        }