Beispiel #1
0
        public IActionResult GetVehicle(int id)
        {
            var vehicle = _vehicles.GetById(id);

            if (vehicle == null)
            {
                return(NotFound());
            }

            var result = Mapper.Map <VehicleResource>(vehicle);

            return(Ok(result));
        }
Beispiel #2
0
        public IActionResult Upload(int vehicleId, IFormFile file)
        {
            var vehicle = _vehicles.GetById(vehicleId, withIncludings: false);

            if (vehicle == null)
            {
                return(NotFound("Vehicle not found."));
            }

            if (file == null)
            {
                return(BadRequest("Null file."));
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file."));
            }
            if (file.Length > _photoSettings.MaxBytes)
            {
                return(BadRequest("File bigger than 10 MB."));
            }
            if (!_photoSettings.IsSupported(file.FileName))
            {
                return(BadRequest("Invalid file type."));
            }

            var uploadsFolderPath = Path.Combine(_host.WebRootPath, "Uploads");

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

            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                file.CopyTo(stream);
            }

            var photo = new Photo {
                FileName = fileName
            };

            vehicle.Photos.Add(photo);
            _vehicles.Save();

            var result = Mapper.Map <PhotoResource>(photo);

            return(Ok(result));
        }