Exemplo n.º 1
0
 private Url CheckIfUserShortenedThisUrl(string url, int id)
 {
     using (var context = new UrlsDataContext(_connectionString))
     {
         return(context.Urls.Where(u => u.UserId == id).FirstOrDefault(u => u.RealUrl == url));
     }
 }
Exemplo n.º 2
0
 public IEnumerable <Url> GetUrlsByUser(int id)
 {
     using (var context = new UrlsDataContext(_connectionString))
     {
         return(context.Urls.Where(u => u.UserId == id).ToList());
     }
 }
Exemplo n.º 3
0
 public User GetByEmail(string email)
 {
     using (var context = new UrlsDataContext(_connectionString))
     {
         return(context.Users.FirstOrDefault(u => u.Email == email));
     }
 }
Exemplo n.º 4
0
 public string GetRealUrl(string url)
 {
     using (var context = new UrlsDataContext(_connectionString))
     {
         var currentUrl = context.Urls.FirstOrDefault(u => u.ShortUrl == url);
         if (currentUrl == null)
         {
             return(null);
         }
         context.ExecuteCommand("UPDATE Urls set TimesUsed = TimesUsed + 1 Where id = {0}", currentUrl.Id);
         return(currentUrl.RealUrl);
     }
 }
Exemplo n.º 5
0
        public void NewUser(User user, string password)
        {
            var salt = PasswordHelper.GenerateSalt();
            var hash = PasswordHelper.HashPassword(password, salt);

            user.PasswordHash = hash;
            user.PasswordSalt = salt;
            using (var context = new UrlsDataContext(_connectionString))
            {
                context.Users.InsertOnSubmit(user);
                context.SubmitChanges();
            }
        }
Exemplo n.º 6
0
        public Url ShortenUrl(string url, int id)
        {
            var newUrl = CheckIfUserShortenedThisUrl(url, id);

            if (newUrl != null)
            {
                return(newUrl);
            }
            using (var context = new UrlsDataContext(_connectionString))
            {
                newUrl = new Url
                {
                    RealUrl  = url,
                    ShortUrl = ShortId.Generate(true, false),
                    UserId   = id,
                };
                context.Urls.InsertOnSubmit(newUrl);
                context.SubmitChanges();
                return(newUrl);
            }
        }