public bool Insert(Advertisement advertisement) { try { _AdvertisementsRepository.Create(advertisement); return(true); } catch (Exception) { return(false); } }
public Advertisement Create(int userId, string text, string title, byte[] image, DateTime?created = null) { var advertisement = new Advertisement() { AuthorId = userId, Text = text, Title = title, Image = image, DateCreated = created ?? DateTime.Now }; _advertisements.Create(advertisement); _context.SaveChanges(); return(advertisement); }
public ActionResult Create(CreateAdvertisementViewModel adv) { if (!ModelState.IsValid) { foreach (var key in ModelState.Keys) { var errors = ModelState[key].Errors.ToList(); if (errors.Count == 0) { continue; } foreach (var error in errors) { ModelState.AddModelError(key, error.ErrorMessage); } } ModelState.AddModelError("", "The form is invalid! Please try again"); InitCreateAdvertismentViewModelOptions(adv); return(View(adv)); } var rootFolder = ConfigurationManager.AppSettings["advertismentsRootFolder"]; var imagesFolder = Path.Combine(rootFolder, Guid.NewGuid().ToString()); var fullPath = Path.Combine(imagesFolder, adv.Image.FileName); var advertisement = new Advertisement { CreatedDate = DateTime.Now, ExpirationDate = DateTime.Today.AddDays(30), IsActive = false, Region = new Region { Id = adv.RegionId }, Vehicl = new Vehicl { Brand = new Brand { Id = adv.BrandId }, Engine = new Engine { Volume = adv.EngineVolume, Fuels = adv.Fuels.Select(x => new Fuel { Id = x }) }, TransmissionType = new TransmissionType { Id = adv.TransmissionTypeId }, VehiclType = new VehiclType { Id = adv.VehiclTypeId } }, User = UserManager.FindByLogin(User.Identity.Name), ImagePath = fullPath, Price = adv.Price, Currency = new Currency { Id = adv.CurrencyId } }; _contentProvider.Upload(fullPath, adv.Image.InputStream); _advertisementRepository.Create(advertisement); return(RedirectToAction("Index", "Home")); }