public IActionResult OnPost() { if (ModelState.IsValid) { var dbAd = _adsService.GetAdById(Ad.Id); if (dbAd == null) // create new { //get current user var identityClaim = _httpContext.HttpContext.User; var user = _userService.GetUserAsync(identityClaim).Result; Ad.Owner = user; var success = _adsService.CreateAdAsync(Ad); return(Redirect("MyAds")); //todo: add success message } else // update { _adsService.UpdateAdd(Ad); //todo: add success message return(Redirect("Index")); } } return(Page()); }
public void OnGet(string adId) { var AdId = Guid.Parse(adId); Ad = _adsService.GetAdById(AdId); Ad.Comments = Ad.Comments.OrderByDescending(a => a.DateAdded).ToList(); var identityClaim = _httpContext.HttpContext.User; var user = _userService.GetUserAsync(identityClaim).Result; ViewData["AlreadyApplied"] = _dbContext.Applicants .Any(ap => ap.UserId == user.Id && ap.AdId == AdId); ViewData["UserOwnsTheAd"] = Ad.Owner.Id == user.Id; }