private HeadphonesViewModel MappingHeadphonesDtoToHeadphonesViewModel(HeadphonesDto headphonesDto) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <HeadphonesDto, HeadphonesViewModel>()).CreateMapper(); var headphonesViewModel = mapper.Map <HeadphonesDto, HeadphonesViewModel>(headphonesDto); return(headphonesViewModel); }
public ActionResult CreateHeadphones(HeadphonesViewModel headphonesViewModel, HttpPostedFileBase uploadImage) { try { if (ModelState.IsValid && uploadImage != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(uploadImage.InputStream)) { imageData = binaryReader.ReadBytes(uploadImage.ContentLength); } HeadphonesDto headphonesDto = MappingHeadphonesViewModelToHeadphonesDto(headphonesViewModel); headphonesDto.Image = imageData; int headphonesId = headphonesService.Create(headphonesDto); return(RedirectToAction("CreateHeadphonesSuccess", new { headphonesId })); } return(View(headphonesViewModel)); } catch (ValidationException ex) { return(Content(ex.Message)); } }
public ActionResult EditHeadphones(HeadphonesViewModel headphonesViewModel, HttpPostedFileBase uploadImage) { try { HeadphonesDto headphonesDto = MappingHeadphonesViewModelToHeadphonesDto(headphonesViewModel); if (uploadImage != null) { byte[] imageData = null; using (var binaryReader = new BinaryReader(uploadImage.InputStream)) { imageData = binaryReader.ReadBytes(uploadImage.ContentLength); } headphonesDto.Image = imageData; } else { headphonesDto.Image = null; } headphonesService.Update(headphonesDto); HeadphonesDto headphonesDtoUpdated = headphonesService.GetHeadphones(headphonesDto.Id); var headphonesViewModelUpdated = MappingHeadphonesDtoToHeadphonesViewModel(headphonesDtoUpdated); return(View("CreateHeadphonesSuccess", headphonesViewModelUpdated)); } catch (ValidationException ex) { return(Content(ex.Message)); } }
private Headphones MappingHeadphonesDtoToHeadphones(HeadphonesDto headphonesDto) { var mapper = new MapperConfiguration(cfg => cfg.CreateMap <HeadphonesDto, Headphones>()).CreateMapper(); var headphones = mapper.Map <HeadphonesDto, Headphones>(headphonesDto); return(headphones); }
public void Update(HeadphonesDto headphonesDto) { Headphones headphones = Database.Headphones.Get(headphonesDto.Id); headphones.Company = headphonesDto.Company; headphones.Description = headphonesDto.Description; headphones.FrequencyRange = headphonesDto.FrequencyRange; headphones.Impedance = headphonesDto.Impedance; headphones.Microphone = headphonesDto.Microphone; headphones.Name = headphonesDto.Name; headphones.Type = headphonesDto.Type; headphones.TypeOfConnection = headphonesDto.TypeOfConnection; if (headphonesDto.Image != null) { headphones.Image = headphonesDto.Image; } if (headphonesDto.Price != 0) { headphones.Price = headphonesDto.Price; } Database.Headphones.Update(headphones); Database.Save(); }
public HeadphonesDto GetHeadphones(int headphonesId) { Headphones headphones = Database.Headphones.Get(headphonesId); HeadphonesDto headphonesDto = MappingHeadphonesToHeadphonesDto(headphones); return(headphonesDto); }
public int Create(HeadphonesDto headphonesDto) { Headphones headphones = MappingHeadphonesDtoToHeadphones(headphonesDto); Database.Headphones.Create(headphones); Database.Save(); return(headphones.Id); }