public IActionResult Ad(Ad ad) { var userId = User.GetUserId(); ad.UserId = userId; if (!ModelState.IsValid) { var vm = new AdViewModel { Ad = ad, Heading = ad.Id == 0 ? "Dodawanie nowego ogłoszenia" : "Edycja ogłoszenia", Categories = _categoryService.GetCategories() }; return(View("Ad", vm)); } if (ad.Id == 0) { _adService.Add(ad); } else { _adService.Update(ad); } return(RedirectToAction("Ads", "Ad")); }
public IActionResult AddAd([FromBody] AddAdModel model) { var(id, success) = _adService.Add(model); if (success) { return(Ok(id)); } else { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
private void AddingAd() { _adService.Add(new Ad { Ilan_Adi = AddNameBox.Text, Ilan_Fiyat = Convert.ToDecimal(AddPriceBox.Text), Ilan_Km = Convert.ToDecimal(AddKmBox.Text), Ilan_Tarih = AddDateTimePicker.Value, Ilan_ArabaID = Convert.ToInt32(dgwCars.SelectedCells[0].Value.ToString()), Ilan_SehirID = Convert.ToInt32(AddCityBox.SelectedValue) }); // Debug.WriteLine(""+AddDateTimePicker.Value+Convert.ToInt32(dgwCars.SelectedCells[0].Value.ToString())); MessageBox.Show("İlan Eklendi."); }
public Ad Save(Ad ad) { var request = ad.ToRequest(); if (ad.ID > 0) { _adService.Update(ad.ID, request); } else { _adService.Add(request); } return(ad); }
public IActionResult Create([FromForm] AdCreateModel entity) { if (ModelState.IsValid) { string currentUser = HttpContext?.User?.Identity?.Name; if (!String.IsNullOrEmpty(currentUser)) { try { AuditedEntityMapper <AdCreateModel> .FillCreateAuditedEntityFields(entity, currentUser, CountryId); entity.OwnerId = CurrentUserId; #region File Map //need to map files in controller because aspnet core assembly is present in project string[] result; if (!String.IsNullOrEmpty(entity.SerializedAdDetailsPictures)) { result = Newtonsoft.Json.JsonConvert.DeserializeObject <string[]>(entity.SerializedAdDetailsPictures); entity.MainPictureFile = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(entity.MainPictureFile); EntityFormMap(result, entity); } else { MapFiles(entity); } // #endregion bool statusResult = _adService.Add(entity); if (statusResult) { return(RedirectToAction(nameof(Index)).WithSuccess(LOCALIZATION_SUCCESS_DEFAULT)); } else { return(RedirectToAction(nameof(Index)).WithError(LOCALIZATION_ERROR_DEFAULT)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(RedirectToAction(nameof(Index)).WithError(ex.Message)); } } else { _logger.LogError(LOCALIZATION_ERROR_USER_MUST_LOGIN); return(NotFound().WithError(LOCALIZATION_ERROR_USER_MUST_LOGIN)); } } ViewBag.Cities = _cityService.GetAllAsLookup(CountryId); ViewBag.Categories = _categoryService.GetAllAsLookup(CountryId); #region Preserve pictures if model validation fails string serializedString = String.Empty; string mainPictureSerializedString = string.Empty; if (entity.Files != null && entity.Files.Count() > 0) { MapFiles(entity); serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(entity.FilesAsListOfByteArray); mainPictureSerializedString = Newtonsoft.Json.JsonConvert.SerializeObject(entity.FilesAsListOfByteArray.First()); } if (!String.IsNullOrEmpty(entity.SerializedAdDetailsPictures)) { string[] convertResult = Newtonsoft.Json.JsonConvert.DeserializeObject <string[]>(entity.SerializedAdDetailsPictures); EntityFormMap(convertResult, entity); serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(entity.FilesAsListOfByteArray); mainPictureSerializedString = Newtonsoft.Json.JsonConvert.SerializeObject(entity.FilesAsListOfByteArray.First()); } ViewBag.SerializedPictures = serializedString; ViewBag.MainPictureSerializedString = mainPictureSerializedString; #endregion return(View(entity).WithWarning(LOCALIZATION_WARNING_INVALID_MODELSTATE)); }
public ActionResult Post(AdRequest request) { var ad = _service.Add(request); return(CreatedAtAction(nameof(Get), new { id = ad.ID }, ad)); }