public ActionResult AddPublication()
        {
            var model = new AddPublicationViewModel();

            this.categoryService.GetAll().ToList().ForEach(x =>
            {
                model.Categories.Add(new SelectListItem()
                {
                    Value = x.Id.ToString(),
                    Text  = x.Name,
                });
            });

            return(this.View(model));
        }
        public ActionResult AddPublication(AddPublicationViewModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (var item in model.files)
                {
                    if (item.ContentType != "image/jpg" && item.ContentType != "image/png" &&
                        item.ContentType != "image/bmp" && item.ContentType != "image/gif" &&
                        item.ContentType != "image/jpeg")
                    {
                        ModelState.AddModelError("wrong type", "един или няколко файла са с непозволено разширение");
                        return(this.View(model));
                    }
                }

                string categoryName    = this.categoryService.GetCategoryNameById(model.CategoryId);
                string subCategoryName = this.subCategoryService.GetSubCategoryNameById(model.SubCategoryId);

                Guid DetailsId     = Guid.NewGuid();
                Guid publicationId = Guid.NewGuid();
                Guid cityId        = this.userService.GetCityIdByUserId(User.Identity.GetUserId());
                Guid RegionId      = this.userService.GetRegionIdByUserId(User.Identity.GetUserId());

                if (categoryName == "Електроника")
                {
                    DetailsId = this.publicationDetailsService.AddElectronicDetails(model.Electronic.Condition,
                                                                                    model.Electronic.Delivery, model.Electronic.IsContacting, model.Electronic.Price);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationIdInElectronics(publicationId, DetailsId);
                }
                else if (categoryName == "Животни")
                {
                    DetailsId = this.publicationDetailsService.AddAnimalsDetails(model.Animal.delivery,
                                                                                 model.Animal.IsContracting, model.Animal.Price);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationIdInAnimals(publicationId, DetailsId);
                }
                else if (categoryName == "за бебето и детето" && (subCategoryName == "Обувки" || subCategoryName == "Дрехи"))
                {
                    DetailsId = this.publicationDetailsService.AddChildDetails(model.children.Condition,
                                                                               model.children.Delivery, model.children.IsContacting, model.children.Number != null ? int.Parse(model.children.Number) : 0, model.children.Price, model.children.SuitableFor);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationInChild(publicationId, DetailsId);
                }
                else if (categoryName == "Услуги")
                {
                    DetailsId = this.publicationDetailsService.AddServiceDetails(model.Service.IsPrivate);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationInService(publicationId, DetailsId);
                }
                else if (categoryName == "Екскурзии и почивки")
                {
                    DetailsId = this.publicationDetailsService.AddHolidayDetails(
                        model.holiday.IsContacting, model.holiday.Price);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationInHoliday(publicationId, DetailsId);
                }
                else if (categoryName == "Мода" &&
                         (subCategoryName == "Мъжки Дрехи" || subCategoryName == "Женски Дрехи" || subCategoryName == "Мъжки Обувки" || subCategoryName == "Женски Обувки"))
                {
                    if ((subCategoryName == "Мъжки Дрехи" || subCategoryName == "Женски Дрехи"))
                    {
                        DetailsId = this.publicationDetailsService.AddClothesDetails(model.clothes.Condition,
                                                                                     model.clothes.Delivery, model.clothes.IsContacting, model.clothes.Price);

                        publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                    model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                        this.publicationDetailsService.UpdatePublicationInClothes(publicationId, DetailsId);
                    }
                    else
                    {
                        DetailsId = this.publicationDetailsService.AddShoesDetails(model.shoe.Condition,
                                                                                   model.shoe.Delivery, model.shoe.IsContacting, model.shoe.Price, model.shoe.Number);

                        publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                    model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                        this.publicationDetailsService.UpdatePublicationInShoes(publicationId, DetailsId);
                    }
                }
                else
                {
                    DetailsId = this.publicationDetailsService.AddInitialDetails(model.Initial.Condition,
                                                                                 model.Initial.Delivery, model.Initial.IsContacting, model.Initial.Price);

                    publicationId = this.publicationService.Add(Guid.Parse(this.User.Identity.GetUserId()), model.CategoryId,
                                                                model.SubCategoryId, model.Title, model.Description, model.PhoneNumber, DetailsId, cityId, RegionId);

                    this.publicationDetailsService.UpdatePublicationInInitial(publicationId, DetailsId);
                }

                Directory.CreateDirectory(Server.MapPath("/") + "/Content" + "/Images/" + User.Identity.Name + "/" + publicationId);

                var isMain = true;
                foreach (var file in model.files)
                {
                    file.SaveAs(Server.MapPath("/") + "/Content" + "/Images/" + User.Identity.Name + "/" + publicationId + "/" + file.FileName);
                    this.imageService.Add(file.FileName, publicationId, isMain);

                    isMain = false;
                }

                return(this.RedirectToAction("Index", "Home"));
            }

            return(this.View(model));
        }