예제 #1
0
        public HttpResponseMessage UploadImage()
        {
            string imageName   = null;
            var    httpRequest = HttpContext.Current.Request;

            //uploading

            var postedFile = httpRequest.Files["ImageName"];

            //create custome filename

            imageName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
            imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
            var filePath = HttpContext.Current.Server.MapPath("/CarsImages/" + imageName);

            postedFile.SaveAs(filePath);

            //save it to DB
            using (CarStoreDataEntities db = new CarStoreDataEntities())
            {
                CarsImage images = new CarsImage()
                {
                    CarModel  = httpRequest["CarModel"],
                    ImageName = imageName
                };
                db.CarsImages.Add(images);
                db.SaveChanges();
            }
            return(Request.CreateResponse(HttpStatusCode.Created));
        }
예제 #2
0
        public async Task <IHttpActionResult> PutCarsImage(int id, CarsImage carsImage)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != carsImage.CarId)
            {
                return(BadRequest());
            }

            db.Entry(carsImage).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarsImageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IResult Update(CarsImage carImage, IFormFile file)
        {
            var oldPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\wwwroot")) + _carsImageDal.Get(p => p.Id == carImage.CarId).ImagePath;

            carImage.ImagePath = FileHelper.Update(oldPath, file);
            carImage.Date      = DateTime.Now;
            _carsImageDal.Update(carImage);
            return(new SuccessResult());
        }
예제 #4
0
        public IResult Update(IFormFile file, CarsImage carImage)
        {
            IResult result = BusinessRules.Run();

            if (result.Success)
            {
                return(result);
            }
            return(null);
        }
        public IActionResult Add([FromForm(Name = ("Image"))] IFormFile file, [FromForm] CarsImage carImage)
        {
            var result = _carImagesService.Add(carImage, file);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
예제 #6
0
        public IResult Delete(CarsImage carImage)
        {
            IResult result = BusinessRules.Run(CheckIfCarsImageLimit(carImage.Id));

            if (result.Success)
            {
                return(result);
            }
            _carsImageDal.Delete(carImage);
            return(new SuccessResult());
        }
예제 #7
0
        public async Task <IHttpActionResult> GetCarsImage(int id)
        {
            CarsImage carsImage = await db.CarsImages.FindAsync(id);

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

            return(Ok(carsImage));
        }
예제 #8
0
        public async Task <IHttpActionResult> PostCarsImage(CarsImage carsImage)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.CarsImages.Add(carsImage);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = carsImage.CarId }, carsImage));
        }
예제 #9
0
        public IResult Add(IFormFile file, CarsImage carsImage)
        {
            IResult result = BusinessRules.Run(CheckIfCarsImageLimit(carsImage.CarId));

            if (result != null)
            {
                return(result);
            }
            carsImage.ImagePath = FileHelper.Add(file);
            carsImage.Date      = DateTime.Now;
            _carsImageDal.Add(carsImage);
            return(new SuccessResult(CarMessages.CarsAdded));
        }
예제 #10
0
        public IResult Delete(CarsImage carImages)
        {
            var     oldpath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\wwwroot")) + _carsImageDal.GetAll(p => p.Id == carImages.Id);
            IResult result  = BusinessRules.Run(FileHelper.Delete(oldpath));

            if (result != null)
            {
                return(result);
            }

            _carsImageDal.Delete(carImages);
            return(new SuccessResult(Messages.CarNotAvailable));
        }
예제 #11
0
        public async Task <IHttpActionResult> DeleteCarsImage(int id)
        {
            CarsImage carsImage = await db.CarsImages.FindAsync(id);

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

            db.CarsImages.Remove(carsImage);
            await db.SaveChangesAsync();

            return(Ok(carsImage));
        }
예제 #12
0
        public IResult Update(IFormFile image, CarsImage carImage)
        {
            var isImage = _carImageDal.Get(c => c.CarId == carImage.CarId);

            var updatedFile = FileHelper.Update(image, isImage.ImagePath);

            if (!updatedFile.Success)
            {
                return(new ErrorResult(updatedFile.Message));
            }
            carImage.ImagePath = updatedFile.Message;
            _carImageDal.Update(carImage);
            return(new SuccessResult("Car image updated"));
        }
예제 #13
0
        private IResult CarImageDelete(CarsImage carImage)
        {
            var newPath    = carImage.ImagePath.Replace('/', '\\');
            var secondPath = "C:" + newPath + ".";

            try
            {
                File.Delete(secondPath);
            }
            catch (Exception exception)
            {
                return(new ErrorResult(exception.Message));
            }

            return(new SuccessResult());
        }
예제 #14
0
        public IResult Add(IFormFile file, CarsImage carImage, int carId)
        {
            IResult result = BusinessRules.Run(CheckImageLimitExceeded(carId));

            if (result != null)
            {
                return(result);
            }
            var imageResult = FileHelper.Add(file);

            if (!imageResult.Success)
            {
                return(new ErrorResult(imageResult.Message));
            }
            carImage.ImagePath = imageResult.Message;
            carImage.Date      = DateTime.Now;
            _carImageDal.Add(carImage);
            return(new SuccessResult(Messages.imageAdd));
        }
예제 #15
0
 //[CacheRemoveAspect("ICarImageService.GetCarImageDetails")]
 public IResult Delete(CarsImage carImage)
 {
     _carImageDal.Delete(carImage);
     return(new SuccessResult());
 }
예제 #16
0
        public IActionResult Add([FromForm(Name = ("file"))] IFormFile file, [FromForm(Name = ("carId"))] int id, [FromForm] CarsImage carImage)
        {
            var result = _CarImageService.Add(file, carImage, id);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }