Example #1
0
        public ActionResult Edit(EditPerformerViewModel model, int[] selectedCategories, HttpPostedFileBase loadImage)
        {
            if (!(bool)Session["isPerformer"] || !(bool)Session["adminStatus"])
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (ReferenceEquals(selectedCategories, null))
            {
                return(RedirectToAction("Edit", new
                {
                    message = "At least one category is required",
                    company = model.Company,
                    info = model.Info,
                    phoneNumber = model.PhoneNumber
                }));
            }

            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();
            }

            ClientViewModelBLL client = _userService.FindById(User.Identity.GetUserId <int>());

            Mapper.Initialize(cfg => cfg.CreateMap <EditPerformerViewModel, ClientViewModelBLL>()
                              .ForMember("Name", opt => opt.MapFrom(c => client.Name))
                              .ForMember("CategoriesBll", opt => opt.MapFrom(c => c.Categories))
                              .ForMember("PictureId", opt => opt.MapFrom(c => _pictureService.FindByBytes(picture.Image).Value))
                              );
            Mapper.Map(model, client);
            _userService.Update(client, selectedCategories);
            _unitOfWork.Save();

            return(RedirectToAction("Details", new { id = client.Id }));
        }
        public ActionResult BecomePerformer(BecomePerformerViewModel model, int[] selectedCategories, HttpPostedFileBase loadImage)
        {
            if ((bool)Session["isPerformer"] && (bool)Session["adminStatus"])
            {
                return(View("~/Views/Error/Forbidden.cshtml"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (selectedCategories == null)
            {
                ModelState.AddModelError("", "At least one category is required");
                GetCategoriesList();
                return(View(model));
            }

            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();
            }

            ClientViewModelBLL client = _userService.FindById(User.Identity.GetUserId <int>());

            Mapper.Initialize(cfg => cfg.CreateMap <BecomePerformerViewModel, ClientViewModelBLL>()
                              .ForMember("RegistrationDate", opt => opt.MapFrom(c => DateTime.Today))
                              .ForMember("IsPerformer", opt => opt.MapFrom(c => true))
                              .ForMember("AdminStatus", opt => opt.MapFrom(c => false))
                              .ForMember("Rating", opt => opt.MapFrom(c => 0))
                              .ForMember("PictureId", opt => opt.MapFrom(c => _pictureService.FindByBytes(picture.Image).Value))
                              );
            Mapper.Map(model, client);
            _userService.Update(client, selectedCategories);
            _unitOfWork.Save();

            return(RedirectToAction("Index"));
        }
Example #3
0
        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));
        }