コード例 #1
0
        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));
            }
        }
コード例 #2
0
        private HeadphonesDto MappingHeadphonesViewModelToHeadphonesDto(HeadphonesViewModel headphonesViewModel)
        {
            var mapper        = new MapperConfiguration(cfg => cfg.CreateMap <HeadphonesViewModel, HeadphonesDto>()).CreateMapper();
            var headphonesDto = mapper.Map <HeadphonesViewModel, HeadphonesDto>(headphonesViewModel);

            return(headphonesDto);
        }
コード例 #3
0
        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));
            }
        }
コード例 #4
0
 public JsonResult Delete(HeadphonesViewModel headphoneModel)
 {
     try
     {
         _genericService.Delete(headphoneModel.ID);
         return(Json(new { Result = "OK" }));
     }
     catch (Exception ex)
     {
         return(Json(new { Result = "ERROR", Message = ex.Message }));
     }
 }
コード例 #5
0
 public JsonResult Edit(HeadphonesViewModel headphoneModel)
 {
     try
     {
         _genericService.Update(_mapper.Map <HeadphonesViewModel, HeadphonesDTO>(headphoneModel));
         return(Json(new { Result = "OK", Record = headphoneModel }));
     }
     catch (Exception ex)
     {
         return(Json(new { Result = "ERROR", Message = ex.Message }));
     }
 }
コード例 #6
0
 public JsonResult Create(HeadphonesViewModel headphoneModel)
 {
     try
     {
         var id = _genericService.Save(_mapper.Map <HeadphonesViewModel, HeadphonesDTO>(headphoneModel));
         headphoneModel.ID = id;
         return(Json(new { Result = "OK", Record = headphoneModel }));
     }
     catch (Exception ex)
     {
         return(Json(new { Result = "ERROR", Message = ex.Message }));
     }
 }