public static int addNewUrl(string longUrl, Guid guid) { try { if (pingUrl(longUrl) == HttpStatusCode.OK.ToString()) { string s = string.Empty; using (var db = new edShortenerModel()) { var newUrl = new ShortedUrl(); newUrl.created = DateTime.Now; newUrl.longUrl = longUrl; newUrl.shortedUrl1 = cryptDecrypt.Base64Encode(longUrl); newUrl.pageStatus = 200; newUrl.lastStatusChange = DateTime.Now; newUrl.Fk_idUsers = guid; db.ShortedUrls.Add(newUrl); return(db.SaveChanges()); } } else { return(0); } } catch (Exception e) { return(0); } }
// GET: Url/Active public ActionResult Active(string id) { string ActiveUserId = UserData.GetActiveUser().Id; using (ApplicationDbContext Db = new ApplicationDbContext()) { try { ShortedUrl Url = Db.ShortedUrls.Where(x => x.Code == id && x.UserId == ActiveUserId).FirstOrDefault(); if (Url == null) { return(HttpNotFound()); } Url.IsActive = true; Db.SaveChanges(); return(RedirectToAction("Index", "Home", new { m = "Active", c = Url.Code })); } catch (Exception e) { return(RedirectToAction("Index", "Home", new { m = "Error" })); } } }
public ActionResult Index(string id) { using (ApplicationDbContext Db = new ApplicationDbContext()) { ShortedUrl Url = Db.ShortedUrls.Where(x => x.Code == id && x.IsActive == true).FirstOrDefault(); if (Url == null) { return(HttpNotFound()); } Url.Click += 1; Db.SaveChanges(); return(Redirect(Url.Url)); } }
public ActionResult Edit() { string Code = Request["code"]; string NewCode = Request["newCode"]; string ActiveUserId = UserData.GetActiveUser().Id; using (ApplicationDbContext Db = new ApplicationDbContext()) { try { ShortedUrl Url = Db.ShortedUrls.Where(x => x.Code == Code && x.UserId == ActiveUserId).FirstOrDefault(); if (Url == null) { return(Json(JsonConvert.SerializeObject(new { title = "Uyarı!", text = "Böyle bir kayıt bulunamadı.", icon = "warning" }))); } Url.Code = NewCode.Replace(" ", "_").Replace("\"", "").Trim(); Db.SaveChanges(); return(Json(JsonConvert.SerializeObject(new { title = "Başarılı", text = "Kayıt düzenlendi.", icon = "success" }))); } catch (Exception e) { return(Json(JsonConvert.SerializeObject(new { title = "Hata!", text = "Beklenmeyen bir hata oluştu. Hata: " + e.HResult + " Hata mesajı: " + e.Message, icon = "error" }))); } } }
public ActionResult Index(UrlResults?m, string c) { ViewBag.StatusMessage = m == UrlResults.Success ? (string.IsNullOrEmpty(c) ? "" : c + " kodlu ") + "URL başarıyla eklendi.,success" : m == UrlResults.Active ? (string.IsNullOrEmpty(c) ? "" : c + " kodlu ") + "URL aktifleştirildi.,success" : m == UrlResults.Passive ? (string.IsNullOrEmpty(c) ? "" : c + " kodlu ") + "URL pasifleştirildi.,success" : m == UrlResults.Error ? "Hata oluştu.,danger" : ""; using (ApplicationDbContext Db = new ApplicationDbContext()) { List <ShortedUrl> Urls = new List <ShortedUrl>(); ShortedUrl Url = null; if (User.Identity.IsAuthenticated) { ApplicationUser ActiveUser = UserData.GetActiveUser(); Urls = Db.ShortedUrls.Where(x => x.UserId == ActiveUser.Id).OrderByDescending(x => x.Date).ToList(); } else { if (!string.IsNullOrEmpty(c)) { Url = Db.ShortedUrls.Where(x => x.Code == c).FirstOrDefault(); } } HomeViewModel ViewModel = new HomeViewModel() { Urls = Urls, Url = Url }; return(View(ViewModel)); } }
public ActionResult Index(HomeViewModel Model) { string ActiveUserId = null; bool IsAuthenticated = User.Identity.IsAuthenticated; if (IsAuthenticated) { ActiveUserId = UserData.GetActiveUser().Id; } using (ApplicationDbContext Db = new ApplicationDbContext()) { HomeViewModel ViewModel = new HomeViewModel() { LongUrl = Model.LongUrl }; if (IsAuthenticated) { ViewModel.Urls = Db.ShortedUrls.Where(x => x.UserId == ActiveUserId).OrderByDescending(x => x.Date).ToList(); } if (Model.LongUrl.Contains(Request["HTTP_HOST"])) { ModelState.AddModelError("already_error", "Kısaltılmış bir URL'i tekrar kısaltamazsınız."); return(View(ViewModel)); } if (!ModelState.IsValid) { return(View(ViewModel)); } try { if (IsAuthenticated && ViewModel.Urls.Where(x => x.Url == Model.LongUrl).Count() > 0) { ModelState.AddModelError("count_error", "Bu URL kaydını daha önce eklediniz."); return(View(ViewModel)); } ShortedUrl NewUrl = new ShortedUrl() { Code = Cipher.CreateUrlCode(), Date = DateTime.Now, UserId = ActiveUserId, Url = Model.LongUrl, Click = 0, IsActive = true }; Db.ShortedUrls.Add(NewUrl); Db.SaveChanges(); return(RedirectToAction("Index", new { m = "Success", c = NewUrl.Code })); } catch (Exception e) { ModelState.AddModelError("exception_error", "Beklenmeyen bir hata oluştu. Hata: " + e.HResult + " Hata mesajı: " + e.Message); return(View(ViewModel)); } } }
public HomeModule() { if (Context != null) { this.RequiresAuthentication(); } Get["test2"] = parameters => { try { this.RequiresClaims(new[] { "Admin" }); string s = "a"; return(s); } catch (Exception e) { return(e.Message); } }; Get["/"] = parameters => { //var currentUser = "******"; //try //{ // currentUser = Context.CurrentUser.UserName; //} //catch(Exception e) //{ //} var currentUser = Context.CurrentUser != null ? Context.CurrentUser.UserName : string.Empty; var guid = UserFunctions.getGuidByName(currentUser); if (guid != null) { Objects.User u = new Objects.User(); u.userUrlList = TableFunctions.getUserList("all", guid); return(View["Index", u]); } else { return("error"); } }; #region table index Get["/table/{date}"] = parameters => { var currentUser = Context.CurrentUser.UserName; var guid = UserFunctions.getGuidByName(currentUser); if (guid != null) { UserUrlList list = TableFunctions.getUserList(parameters.date, guid); return(View["_TableUrl", list]); } else { return("error"); } }; Get["/tableDetails/{urlObjectId}"] = parameters => { int urlObjId = parameters.urlObjectId; UrlObject urlObj = TableFunctions.getUrlTableDetails(urlObjId); if (urlObj != null) { return(View["_ShortedUrlInfo", urlObj]); } else { return("error"); } }; #endregion #region user #region logIN Get["/login"] = parameter => { dynamic model = new ExpandoObject(); model.Errored = Request.Query.error.HasValue; return(View["logIn", model]); }; Post["/login"] = parameter => { string name = Request.Form["userName"]; string pass = Request.Form["userPass"]; string remember = Request.Form["rememberMe"]; Objects.User user = UserFunctions.logInUser(name, pass); if (user != null) { DateTime?expiry = null; if (!string.IsNullOrEmpty(remember)) { expiry = DateTime.Now.AddDays(7); } return(this.LoginAndRedirect(user.id, expiry)); } else { return(Context.GetRedirect("~/login?error=true&username="******"/logout"] = parameter => { return(this.LoginAndRedirect(UserFunctions.getDummyCurrentUser().guid)); }; #endregion #region register Get["/register"] = parameter => { dynamic model = new ExpandoObject(); model.Errored = Request.Query.error.HasValue; return(View["register", model]); }; Post["/register"] = parameter => { string name = Request.Form["userName"]; string pass = Request.Form["userPass"]; string pass2 = Request.Form["userPass"]; string remember = Request.Form["rememberMe"]; Objects.User user = UserFunctions.registerUser(name, pass, pass2); if (user != null) { DateTime?expiry = null; if (!string.IsNullOrEmpty(remember)) { expiry = DateTime.Now.AddDays(7); } return(this.LoginAndRedirect(user.id, expiry)); } else { return(Context.GetRedirect("~/register?error=true&username="******"profile"] = parameters => { var currentUser = Context.CurrentUser.UserName; var guid = UserFunctions.getGuidByName(currentUser); if (guid != null) { Objects.User u = UserFunctions.getFullUserByGuid(guid); u.userUrlList = TableFunctions.getUserList("all", guid); return(View["profile", u]); } else { return("error"); } }; Post["/profile"] = parameter => { Objects.User user = this.Bind(); var changePass = Request.Form["changePass"]; var currentUser = Context.CurrentUser.UserName; var guid = UserFunctions.getGuidByName(currentUser); user.id = guid; user.name = currentUser; if (changePass == "1") { if (!string.IsNullOrEmpty(user.newPass1) && user.newPass1 == user.newPass2) { if (UserFunctions.verifyOldPass(user)) { if (UserFunctions.updateProfile(user, true) > 0) { return(View["profile", user]); } else { user.error = "Error Ocurred while updating profile"; return(View["profile", user]); } } else { user.error = "Old password was incorrect"; return(View["profile", user]); } } else { user.error = "Password does not match"; return(View["profile", user]); } } else { if (UserFunctions.updateProfile(user, false) > 0) { return(View["profile", user]); } else { user.error = "Error Ocurred while updating profile"; return(View["profile", user]); } } }; #endregion #endregion #region short Get["/goTo/{toShort}"] = parameters => { string toShort = parameters.toShort; UrlObject urlObject = ShortFunctions.getUrlObjectIdByShorted(toShort); if (urlObject != null && urlObject.status.ToString() == "200") { if (ShortFunctions.insertNewClick(urlObject.idShortedUrl) > 0) { return(Response.AsRedirect(urlObject.longUrl)); } return(View["error", new errorObj { errorMsg = "No se ha podido acceder a la url " + urlObject.shortedUrl + "error desconocdio" }]); } else { return(View["error", new errorObj { errorMsg = "Url Caida desde " + urlObject.lastStatusCHanged }]); } }; Post["/addUrl"] = parameters => { string urlToAdd = Request.Form["urlToShort"]; var currentUser = Context.CurrentUser.UserName; if (!string.IsNullOrEmpty(currentUser)) { var guid = UserFunctions.getGuidByName(currentUser); ShortedUrl newUrl = ShortFunctions.urlAlreadyShorted(urlToAdd, guid); if (newUrl == null) { int result = ShortFunctions.addNewUrl(urlToAdd, guid); if (result != 0) { return(Response.AsRedirect("/")); } else { return(View["error", new errorObj { errorMsg = "No se ha podido crear la url " + urlToAdd }]); } } return(Response.AsRedirect("/tableDetails/" + newUrl.idShortedUrl)); } return("error"); }; #endregion #region csv Get["/csv"] = parameters => { return(View["csv", null]); }; Post["/csv"] = parameters => { string urlsToAdd = Request.Form["urlList"]; string[] urlsArr = urlsToAdd.Split(','); var currentUser = Context.CurrentUser.UserName; var guid = UserFunctions.getGuidByName(currentUser); if (guid != null) { csvResponse result = ShortFunctions.addArrUrls(urlsArr, guid); return(View["csv", result]); } return(0); }; #endregion Get["/details/{url}"] = parameters => { return(View["register"]); }; }