Exemplo n.º 1
0
        public IActionResult Create(ToyViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Toy.Producer = _producerRepository.GetProducerByID(model.Producer);
                model.Toy.Category = _categoryRepository.GetCategoryById(model.Category);

                _toyRepository.AddToy(model.Toy);

                if (model.Images != null)
                {
                    var helper = new ImageHelper(_environment);

                    foreach (var image in model.Images)
                    {
                        var imageName = helper.AddImage(image);
                        var gallery   = new Gallery()
                        {
                            FileName = imageName, Toy = model.Toy
                        };

                        _galleryRepository.AddImage(gallery);
                    }
                }

                _toyRepository.Save();
                return(RedirectToAction("Index"));
            }
            else
            {
                model.Categories = FormHelper.GetFormCategories(_context.Categories.ToArray());
                model.Producers  = FormHelper.GetFormProducers(_context.Producers.ToArray());
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            var imageEntity = _mapper.Map <Entities.Image>(imageForCreation);

            var webRootPath = _hostingEnvironment.WebRootPath;

            // create the jpg filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            imageEntity.FileName = fileName;
            imageEntity.Id       = Guid.NewGuid();

            _galleryRepository.AddImage(imageEntity);

            _galleryRepository.Save();

            var imageToReturn = _mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }
Exemplo n.º 3
0
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            if (imageForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Automapper maps only the Title in our configuration
            var imageEntity = Mapper.Map <Entities.Image>(imageForCreation);

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _hostingEnvironment.WebRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            // ownerId should be set - can't save image in starter solution, will
            // be fixed during the course
            //imageEntity.OwnerId = ...;

            //set the ownerId on the imageEntity
            var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            imageEntity.OwnerId = ownerId;

            // add and save.
            _galleryRepository.AddImage(imageEntity);

            if (!_galleryRepository.Save())
            {
                throw new Exception($"Adding an image failed on save.");
            }

            var imageToReturn = Mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create(ImageForCreation imageForCreation)
        {
            var file = imageForCreation.File;

            if (file == null)
            {
                return(BadRequest("Null file"));
            }

            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }

            /* if (file.Length > photoSettings.MaxBytes)
             * {
             *   return BadRequest("Max file size exceeded");
             * }
             * if (!photoSettings.IsSupported(file.FileName))
             * {
             *   return BadRequest("Invalid file type.");
             * }
             */
            var uploadFolderPath = Path.Combine(hostingEnvironment.WebRootPath, "uploads");

            if (!Directory.Exists(uploadFolderPath))
            {
                Directory.CreateDirectory(uploadFolderPath);
            }

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(imageForCreation.File.FileName);

            var filePath = Path.Combine(uploadFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await imageForCreation.File.CopyToAsync(stream);
            }

            var image = new Image(imageForCreation.Title, fileName);

            await galleryRepository.AddImage(image);

            var imageToReturn = Mapper.Map <ImageDto>(image);

            if (!(await galleryRepository.Save()))
            {
                return(BadRequest("An error ocurred while saving the data into the database."));
            }

            return(CreatedAtRoute("GetImage",
                                  new { id = image.Id },
                                  CreateLinksForImage(imageToReturn)));
        }
Exemplo n.º 5
0
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            if (imageForCreation == null)
            {
                return(BadRequest());
            }

            // Introduction to model validation in ASP.NET Core MVC
            // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation
            if (ModelState.IsValid == false)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Automapper maps only the Title in our configuration
            var imageEntity = Mapper.Map <Entities.Image>(imageForCreation);

            imageEntity.OwnerId = Guid.NewGuid().ToString(); //TODO:Fix Added Random Owner ID for quick fix purpose

            // Create an iamge from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = hostingEnvironment.WebRootPath;

            // create the filename
            var filename = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{filename}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = filename;

            // add and save
            galleryRepository.AddImage(imageEntity);

            if (galleryRepository.Save() == false)
            {
                throw new Exception("Adding an image failed on save.");
            }

            var imageToReturn = Mapper.Map <Image>(imageEntity);

            // 201 Created
            // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
            return(CreatedAtRoute("GetImage", new { id = imageToReturn.Id }, imageToReturn));
        }
Exemplo n.º 6
0
        public IActionResult PostImage([FromBody] ImageForCreation imageForCreation)
        {
            if (imageForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // Automapper maps only the Title in our configuration
            var imageEntity = Mapper.Map <Entities.Image>(imageForCreation);

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _env.WebRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            _repo.AddImage(imageEntity);

            if (!_repo.Save())
            {
                throw new Exception($"Adding an image failed on save.");
            }

            var imageToReturn = Mapper.Map <Model.Image>(imageEntity);

            return(CreatedAtRoute("GetImage", new { id = imageToReturn.Id }, imageToReturn));
        }
Exemplo n.º 7
0
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            // Automapper maps only the Title in our configuration
            var imageEntity = _mapper.Map <Entities.Image>(imageForCreation);

            var userId = User.Claims.FirstOrDefault(i => i.Type == "sub")?.Value;

            imageEntity.OwnerId = userId;

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _hostingEnvironment.WebRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            // ownerId should be set - can't save image in starter solution, will
            // be fixed during the course
            //imageEntity.OwnerId = ...;

            // add and save.
            _galleryRepository.AddImage(imageEntity);

            _galleryRepository.Save();

            var imageToReturn = _mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            if (imageForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var imageEntity = Mapper.Map <Entities.Image>(imageForCreation);

            var webRootPath = _hostingEnvironment.WebRootPath;

            string fileName = Guid.NewGuid().ToString() + ".jpg";

            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            imageEntity.FileName = fileName;

            var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            imageEntity.OwnerId = ownerId;

            _galleryRepository.AddImage(imageEntity);

            if (!_galleryRepository.Save())
            {
                throw new Exception($"Adding an image failed on save.");
            }

            var imageToReturn = Mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }
Exemplo n.º 9
0
        public IActionResult CreateImage([FromBody] ImageForCreation imageForCreation)
        {
            // Automapper maps only the Title in our configuration
            var imageEntity = _mapper.Map <Entities.Image>(imageForCreation);

            // Create an image from the passed-in bytes (Base64), and
            // set the filename on the image

            // get this environment's web root path (the path
            // from which static content, like an image, is served)
            var webRootPath = _hostingEnvironment.WebRootPath;

            // create the filename
            string fileName = Guid.NewGuid().ToString() + ".jpg";

            // the full file path
            var filePath = Path.Combine($"{webRootPath}/images/{fileName}");

            // write bytes and auto-close stream
            System.IO.File.WriteAllBytes(filePath, imageForCreation.Bytes);

            // fill out the filename
            imageEntity.FileName = fileName;

            //seems like we could create a custom controller class and hang this value off of it for all to use...
            var ownerId = User.Claims.FirstOrDefault(c => c.Type == "sub")?.Value;

            //a good reason to have DTOs separate from ViewModels...ownerId couldn't be spoofed
            imageEntity.OwnerId = ownerId;

            // add and save.
            _galleryRepository.AddImage(imageEntity);

            _galleryRepository.Save();

            var imageToReturn = _mapper.Map <Image>(imageEntity);

            return(CreatedAtRoute("GetImage",
                                  new { id = imageToReturn.Id },
                                  imageToReturn));
        }