public ActionResult Create(CreateOrderViewModel order, HttpPostedFileBase loadImage) { if (ModelState.IsValid) { PictureViewModelBLL picture = null; if (!ReferenceEquals(loadImage, null)) { byte[] image; using (var binaryReader = new BinaryReader(loadImage.InputStream)) { image = binaryReader.ReadBytes(loadImage.ContentLength); } picture = new PictureViewModelBLL { Image = image }; _pictureService.Create(image); _unitOfWork.Save(); } Mapper.Initialize(cfg => cfg.CreateMap <CreateOrderViewModel, OrderViewModelBLL>() .ForMember("CategoryId", opt => opt.MapFrom(c => _categoryService.FindByName(c.Category).Id)) .ForMember("StatusId", opt => opt.MapFrom(c => 1)) .ForMember("AdminStatus", opt => opt.MapFrom(c => false)) .ForMember("UploadDate", opt => opt.MapFrom(c => DateTime.Now)) .ForMember("UserId", opt => opt.MapFrom(c => User.Identity.GetUserId <int>())) .ForMember("PictureId", opt => opt.MapFrom(c => _pictureService.FindByBytes(picture.Image).Value)) ); OrderViewModelBLL orderDto = Mapper.Map <CreateOrderViewModel, OrderViewModelBLL>(order); OperationDetails operationDetails = _orderService.Create(orderDto); _unitOfWork.Save(); if (operationDetails.Succedeed) { return(RedirectToAction("Index")); } ModelState.AddModelError(operationDetails.Property, operationDetails.Message); } ModelState.AddModelError("", "Creation error"); ViewBag.Category = new SelectList(_categoryService.GetAll(), "Name", "Name"); ViewBag.DefaultPath = $"data: image/png; base64, {Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath(DefaultImageName)))}"; return(View(order)); }
public OperationDetails Create(OrderViewModelBLL order) { Mapper.Initialize(cfg => cfg.CreateMap <OrderViewModelBLL, Order>()); _orderRepository.Create(Mapper.Map <OrderViewModelBLL, Order>(order)); return(new OperationDetails(true, "Creation succeeded", string.Empty)); }