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)); }
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()); }
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)); }
public IResult Delete(CarsImage carImage) { IResult result = BusinessRules.Run(CheckIfCarsImageLimit(carImage.Id)); if (result.Success) { return(result); } _carsImageDal.Delete(carImage); return(new SuccessResult()); }
public async Task <IHttpActionResult> GetCarsImage(int id) { CarsImage carsImage = await db.CarsImages.FindAsync(id); if (carsImage == null) { return(NotFound()); } return(Ok(carsImage)); }
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)); }
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)); }
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)); }
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)); }
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")); }
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()); }
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)); }
//[CacheRemoveAspect("ICarImageService.GetCarImageDetails")] public IResult Delete(CarsImage carImage) { _carImageDal.Delete(carImage); return(new SuccessResult()); }
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)); }