Пример #1
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));
        }
        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));
        }
Пример #3
0
        public async Task <ActionResult> Post([FromForm] CatCreateViewModel value)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (value.File != null)
            {
                IFormFile file = value.File;

                List <string> imgErrors      = new List <string>();
                var           supportedTypes = new[] { "png", "jpg", "jpeg", "gif" };
                var           fileExt        = Path.GetExtension(file.FileName).Substring(1);
                if (file.Length == 0)
                {
                    imgErrors.Add("File is empty!");
                }

                if (Array.IndexOf(supportedTypes, fileExt) < 0)
                {
                    imgErrors.Add("File Extension Is InValid - Only Upload image File");
                }

                if (imgErrors.Count > 0)
                {
                    return(BadRequest(new { Image = imgErrors }));
                }

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

                _logger.LogDebug($"Save Image:{filePath}");
                _fileService.SaveFile(filePath, FormFileBytes(file));

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

                var result = await _catService.CreateCat(catModel);

                catModel.Id = result;

                return(CreatedAtRoute("GetById", new { Id = result }, catModel));
            }
            else
            {
                List <string> imgErrors = new List <string> {
                    "File is empty!"
                };
                return(BadRequest(new { errors = new { Image = imgErrors } }));
            }
        }
Пример #4
0
        public async Task <IActionResult> Create([Bind("Name,Age,ImageUrl,BreedId")] CreateCatViewModel catModel)
        {
            bool isCreated = await catService.CreateCat(catModel);

            if (!isCreated)
            {
                return(NotFound());
            }
            ViewData["BreedId"] = new SelectList(context.Breeds, "Id", "Name", catModel.BreedId);
            return(Redirect("/Cats/All"));
        }
Пример #5
0
 public ActionResult <Cat> Post([FromBody] Cat cat)
 {
     return(Ok(_catService.CreateCat(cat)));
 }