Esempio n. 1
0
 private bool EmailExists(string email)
 {
     using (var ctx = new THQEntities())
     {
         var query = from u in ctx.Users
                     where u.Email == email
                     select u;
         return(query.Any());
     }
 }
Esempio n. 2
0
 public Task <Category[]> GetCategories()
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             return ctx.Categories.ToArray <Category>();
         }
     }));
 }
Esempio n. 3
0
 public Task <User[]> GetUsers()
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             User[] users = ctx.Users
                            .ToArray <User>();
             return users;
         }
     }));
 }
Esempio n. 4
0
        public Task <Tutorial> AddTutorial(Tutorial tutorial, int categoryId, string ip, string username)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    if (string.IsNullOrEmpty(tutorial.Title))
                    {
                        throw new THQArgumentException(Strings.title);
                    }
                    if (string.IsNullOrEmpty(tutorial.Description))
                    {
                        throw new THQArgumentException(Strings.description);
                    }
                    if (string.IsNullOrEmpty(tutorial.Url))
                    {
                        throw new THQArgumentException(Strings.url);
                    }
                    Category category = ctx.Categories
                                        .Where(c => c.Id == categoryId)
                                        .FirstOrDefault <Category>();
                    if (category == null)
                    {
                        throw new THQNotFoundException(Strings.category);
                    }
                    User user = ctx.Users
                                .Where(u => u.UserName == username)
                                .FirstOrDefault <User>();
                    if (user == null)
                    {
                        throw new THQNotFoundException(Strings.user);
                    }

                    Tutorial newTutorial = new Tutorial();
                    newTutorial.Created = DateTime.Now;
                    newTutorial.LastModified = DateTime.MinValue;
                    newTutorial.Title = tutorial.Title;
                    newTutorial.Description = tutorial.Description;
                    newTutorial.Url = tutorial.Url;
                    newTutorial.Status = TutorialStatus.NewQueue;
                    newTutorial.Category = ctx.Categories.Attach(category);
                    newTutorial.Ip = ip;
                    newTutorial.User = ctx.Users.Attach(user);
                    ctx.Tutorials.Add(newTutorial);
                    ctx.SaveChanges();
                    return newTutorial;
                }
            }));
        }
Esempio n. 5
0
        public Task <User> RegisterUser(User user, string password, string passwordRepeat, string ip)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    if (string.IsNullOrEmpty(password) || password != passwordRepeat)
                    {
                        throw new THQArgumentException(Strings.password);
                    }
                    if (string.IsNullOrEmpty(user.Email) || !user.Email.IsValidEmail())
                    {
                        throw new THQArgumentException(Strings.email);
                    }
                    if (string.IsNullOrEmpty(user.UserName))
                    {
                        throw new THQArgumentException(Strings.password);
                    }
                    if (ctx.Users.Where(u => u.UserName == user.UserName).Any())
                    {
                        throw new THQConflictException(Strings.username);
                    }
                    if (this.EmailExists(user.Email))
                    {
                        throw new THQConflictException(Strings.email);
                    }

                    User newUser = new User();
                    newUser.Created = DateTime.Now;
                    newUser.LastModified = DateTime.MinValue;
                    newUser.LastLogin = DateTime.MinValue;
                    newUser.Location = user.Location;
                    newUser.Name = user.Name;
                    newUser.UserName = user.UserName;
                    newUser.UserRole = UserRole.RegularUser;
                    newUser.Website = user.Website;
                    newUser.Email = user.Email;
                    newUser.Ip = ip;
                    newUser.PasswordHash = this._passwordHasher.HashPassword(password);
                    newUser.Activated = false;
                    newUser.ActivationCode = Guid.NewGuid().ToString();
                    ctx.Users.Add(newUser);
                    ctx.SaveChanges();
                    return newUser;
                }
            }));
        }
Esempio n. 6
0
 public Task <User> GetUser(string username)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             User user = ctx.Users
                         .Where(u => u.UserName == username)
                         .FirstOrDefault <User>();
             if (user == null)
             {
                 throw new THQNotFoundException(Strings.user);
             }
             return user;
         }
     }));
 }
Esempio n. 7
0
        public Task UpdateUser(User user, string username, string password = "", string passwordRepeat = "")
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    User currUser = ctx.Users
                                    .Where(u => u.UserName == username)
                                    .FirstOrDefault <User>();
                    if (currUser == null)
                    {
                        throw new THQNotFoundException(Strings.user);
                    }

                    currUser.LastModified = DateTime.Now;

                    if (!string.IsNullOrEmpty(password))
                    {
                        if (password != passwordRepeat)
                        {
                            throw new THQArgumentException(Strings.password);
                        }
                        currUser.PasswordHash = this._passwordHasher.HashPassword(password);
                    }
                    else if (!string.IsNullOrEmpty(user.Email) && user.Email != currUser.Email)
                    {
                        if (string.IsNullOrEmpty(user.Email) || !user.Email.IsValidEmail())
                        {
                            throw new THQArgumentException(Strings.email);
                        }
                        if (this.EmailExists(user.Email))
                        {
                            throw new THQConflictException(Strings.email);
                        }
                        currUser.Email = user.Email;
                    }
                    else
                    {
                        currUser.Name = user.Name;
                        currUser.Location = user.Location;
                        currUser.Website = user.Website;
                    }
                    ctx.SaveChanges();
                }
            }));
        }
Esempio n. 8
0
 public Task <Category> GetCategory(int categoryId)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             Category category = ctx.Categories
                                 .Where(c => c.Id == categoryId)
                                 .FirstOrDefault <Category>();
             if (category == null)
             {
                 throw new THQNotFoundException(Strings.category);
             }
             return category;
         }
     }));
 }
Esempio n. 9
0
        public Task <Click> AddClick(int tutorialId, string username, string ip)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    User user = null;
                    if (!string.IsNullOrEmpty(username))
                    {
                        user = ctx.Users
                               .Where(u => u.UserName == username)
                               .FirstOrDefault <User>();
                        if (user == null)
                        {
                            throw new THQNotFoundException(Strings.user);
                        }
                    }

                    Tutorial tutorial = ctx.Tutorials
                                        .Where(t => t.Id == tutorialId)
                                        .Include(t => t.User)
                                        .Include(t => t.Category)
                                        .FirstOrDefault <Tutorial>();
                    if (tutorial == null)
                    {
                        throw new THQNotFoundException(Strings.tutorial);
                    }

                    Click click = new Click()
                    {
                        Created = DateTime.Now,
                        LastModified = DateTime.MinValue,
                        Ip = ip
                    };
                    click.User = user;

                    tutorial.NumClicks += 1;
                    click.Tutorial = tutorial;

                    ctx.Clicks.Add(click);
                    ctx.SaveChanges();
                    return click;
                }
            }));
        }
Esempio n. 10
0
 private bool UserCanVote(string username, int tutorialId)
 {
     using (var ctx = new THQEntities())
     {
         DateTime yesterday = DateTime.Now.AddDays(-1);
         User     user      = (from u in ctx.Users
                               where u.UserName == username
                               select u).FirstOrDefault <User>();
         var query = (from v in ctx.Votes
                      where v.User.UserName == username &&
                      v.Tutorial.Id == tutorialId
                      select v).Include(v => v.User).Include(v => v.Tutorial);
         Vote[] votes = query
                        .Where(v => v.Created > yesterday)
                        .ToArray <Vote>();
         return(votes.Length == 0);
     }
 }
Esempio n. 11
0
 public Task <Comment[]> GetComments(int tutorialId)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             Tutorial tutorial = ctx.Tutorials
                                 .Where(t => t.Id == tutorialId)
                                 .Include(t => t.Comments)
                                 .FirstOrDefault <Tutorial>();
             if (tutorial == null)
             {
                 throw new THQNotFoundException(Strings.tutorial);
             }
             return tutorial.Comments.ToArray <Comment>();
         }
     }));
 }
Esempio n. 12
0
        public Task <Comment> AddComment(Comment comment, int tutorialId, string ip, string username)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    if (string.IsNullOrEmpty(comment.Content))
                    {
                        throw new THQArgumentException(Strings.content);
                    }

                    Tutorial tutorial = ctx.Tutorials
                                        .Where(t => t.Id == tutorialId)
                                        .Include(t => t.Category)
                                        .Include(t => t.User)
                                        .FirstOrDefault <Tutorial>();
                    if (tutorial == null)
                    {
                        throw new THQNotFoundException(Strings.tutorial);
                    }

                    User user = ctx.Users
                                .Where(u => u.UserName == username)
                                .FirstOrDefault <User>();
                    if (user == null)
                    {
                        throw new THQNotFoundException(Strings.user);
                    }

                    Comment newComment = new Comment();
                    newComment.Created = DateTime.Now;
                    newComment.LastModified = DateTime.MinValue;
                    newComment.Content = comment.Content;
                    newComment.Ip = ip;
                    newComment.User = user;
                    newComment.Tutorial = tutorial;
                    tutorial.NumComments += 1;

                    ctx.Comments.Add(newComment);
                    ctx.SaveChanges();
                    return newComment;
                }
            }));
        }
Esempio n. 13
0
 public Task <Tutorial> GetTutorial(int tutorialId)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             Tutorial tutorial = ctx.Tutorials
                                 .Where(t => t.Id == tutorialId)
                                 .Include(t => t.Category)
                                 .Include(t => t.User)
                                 .FirstOrDefault <Tutorial>();
             if (tutorial == null)
             {
                 throw new THQNotFoundException(Strings.tutorial);
             }
             return tutorial;
         }
     }));
 }
Esempio n. 14
0
        public Task DeleteComment(int commentId)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    Comment comment = ctx.Comments
                                      .Where(c => c.Id == commentId)
                                      .FirstOrDefault <Comment>();
                    if (comment == null)
                    {
                        throw new THQNotFoundException(Strings.comment);
                    }

                    ctx.Comments.Remove(comment);
                    ctx.SaveChanges();
                }
            }));
        }
Esempio n. 15
0
        public Task UpdateTutorial(Tutorial tutorial, int tutorialId, int categoryId = 0)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    Tutorial currTutorial = ctx.Tutorials
                                            .Where(t => t.Id == tutorialId)
                                            .Include(t => t.User)
                                            .FirstOrDefault <Tutorial>();
                    if (currTutorial == null)
                    {
                        throw new THQNotFoundException(Strings.tutorial);
                    }

                    Category category = null;
                    if (categoryId != 0)
                    {
                        category = ctx.Categories
                                   .Where(c => c.Id == categoryId)
                                   .FirstOrDefault <Category>();
                        if (category == null && categoryId != null)
                        {
                            throw new THQNotFoundException(Strings.category);
                        }
                    }

                    currTutorial.LastModified = DateTime.Now;
                    currTutorial.Title = tutorial.Title;
                    currTutorial.Description = tutorial.Description;
                    currTutorial.Url = tutorial.Url;
                    currTutorial.Status = tutorial.Status;
                    if (category != null)
                    {
                        currTutorial.Category = category;
                    }
                    ctx.SaveChanges();
                }
            }));
        }
Esempio n. 16
0
 public Task ActivateUser(string username, string activationCode)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             if (string.IsNullOrEmpty(activationCode) && activationCode != "0")
             {
                 throw new THQArgumentException(Strings.activationCode);
             }
             User user = ctx.Users
                         .Where(u => u.UserName == username && u.ActivationCode == activationCode)
                         .FirstOrDefault <User>();
             if (user == null)
             {
                 throw new THQNotFoundException(Strings.user);
             }
             user.Activated = true;
             user.ActivationCode = "0";
             ctx.SaveChanges();
         }
     }));
 }
Esempio n. 17
0
 public Task <bool> ValidateUser(string username, string password)
 {
     return(Task.Run(() =>
     {
         using (var ctx = new THQEntities())
         {
             User user = ctx.Users
                         .Where(u => u.UserName == username)
                         .FirstOrDefault <User>();
             if (user != null)
             {
                 bool valid = this._passwordHasher.VerifyHashedPassword(user.PasswordHash, password) != PasswordVerificationResult.Failed;
                 if (valid)
                 {
                     user.LastLogin = DateTime.Now;
                     ctx.SaveChanges();
                 }
                 return valid;
             }
             return false;
         }
     }));
 }
Esempio n. 18
0
        public Task DeleteTutorial(int tutorialId)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    Tutorial tutorial = ctx.Tutorials
                                        .Where(t => t.Id == tutorialId)
                                        .Include(t => t.Category)
                                        .Include(t => t.User)
                                        .FirstOrDefault <Tutorial>();
                    if (tutorial == null)
                    {
                        throw new THQNotFoundException(Strings.tutorial);
                    }

                    if (tutorial.Status != TutorialStatus.Deleted)
                    {
                        tutorial.Status = TutorialStatus.Deleted;
                        ctx.SaveChanges();
                    }
                }
            }));
        }
Esempio n. 19
0
 public Task <Tutorial[]> GetTutorials(int howMany, int page, int categoryId = 0)
 {
     return(Task.Run(() =>
     {
         if (howMany > 50)
         {
             throw new THQNotFoundException(Errors.tooManyRequests);
         }
         using (var ctx = new THQEntities())
         {
             Tutorial[] tutorials = ctx.Tutorials
                                    .Include(t => t.Category)
                                    .Include(t => t.User)
                                    .ToArray <Tutorial>();
             var query = tutorials.OrderByDescending(t => t.Created).AsQueryable();
             if (categoryId != 0)
             {
                 query = query.Where(t => t.Category.Id == categoryId);
             }
             query = query.Skip(page * howMany).Take(howMany);
             return query.ToArray <Tutorial>();
         }
     }));
 }
Esempio n. 20
0
        protected override void Seed(THQEntities context)
        {
            var    passwordHasher = new PasswordHasher();
            string ip             = "91.72.231.108";

            Category cat1 = new Category
            {
                Id           = 1,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Title        = "ASP.NET",
                Description  = "ASP.NET is Microsofts programming framework. You can create (web) applications using C#, MVC, Web API and more.",
                Ip           = ip
            };
            Category cat2 = new Category
            {
                Id           = 2,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Title        = "PHP",
                Description  = "PHP is one of the most popular web based scripting languages. Popular frameworks include Zend, Symphony, Laravel and Yii.",
                Ip           = ip
            };

            User user1 = new User
            {
                Id             = 1,
                Created        = DateTime.Now,
                Email          = "*****@*****.**",
                LastLogin      = DateTime.Now,
                LastModified   = DateTime.MinValue,
                Location       = "Haren gn",
                Name           = "Duco Winterwerp",
                PasswordHash   = passwordHasher.HashPassword("password1"),
                UserName       = "******",
                UserRole       = UserRole.Administrator,
                Ip             = ip,
                Website        = "http://duco.cc",
                Activated      = true,
                ActivationCode = Guid.NewGuid().ToString()
            };
            User user2 = new User
            {
                Id             = 2,
                Created        = DateTime.Now,
                Email          = "*****@*****.**",
                LastLogin      = DateTime.Now,
                LastModified   = DateTime.MinValue,
                Location       = "Annen",
                Name           = "Gerrie Winterwerp",
                PasswordHash   = passwordHasher.HashPassword("password2"),
                UserName       = "******",
                UserRole       = UserRole.RegularUser,
                Ip             = ip,
                Website        = "http://gerr.ie",
                Activated      = true,
                ActivationCode = Guid.NewGuid().ToString()
            };

            Tutorial tut1 = new Tutorial
            {
                Id           = 1,
                Created      = DateTime.Now,
                Category     = cat1,
                Description  = "The complete ASP.NET tutorial",
                Ip           = ip,
                LastModified = DateTime.MinValue,
                Title        = "ASP.NET tutorial",
                Url          = "http://asp.net-tutorials.com/",
                User         = user1,
                Status       = TutorialStatus.Approved,
                NumClicks    = 1,
                NumComments  = 2,
                NumVotes     = 1,
                AvgRating    = 2
            };
            Tutorial tut2 = new Tutorial
            {
                Id           = 2,
                Created      = DateTime.Now,
                Category     = cat2,
                Description  = "Create a simple PHP view engine",
                Ip           = ip,
                LastModified = DateTime.MinValue,
                Title        = "Simple PHP view engine",
                Url          = "http://duco.cc/create-a-simple-template-view-engine-with-php/",
                User         = user2,
                Status       = TutorialStatus.Pending,
                NumClicks    = 1,
                NumComments  = 0,
                NumVotes     = 1,
                AvgRating    = 9
            };

            Comment com1 = new Comment
            {
                Id           = 1,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Content      = "Wow, great tutorial!",
                Tutorial     = tut1,
                User         = user1,
                Ip           = ip
            };
            Comment com2 = new Comment
            {
                Id           = 2,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Content      = "Wow, great tutorial to!",
                Tutorial     = tut1,
                User         = user1,
                Ip           = ip
            };

            Click cl1 = new Click
            {
                Id           = 1,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Ip           = ip,
                Tutorial     = tut1
            };
            Click cl2 = new Click
            {
                Id           = 2,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Ip           = ip,
                User         = user1,
                Tutorial     = tut2
            };

            Vote vo1 = new Vote
            {
                Id           = 1,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Ip           = ip,
                User         = user2,
                Tutorial     = tut1,
                Rating       = 2
            };
            Vote vo2 = new Vote
            {
                Id           = 2,
                Created      = DateTime.Now,
                LastModified = DateTime.MinValue,
                Ip           = ip,
                User         = user1,
                Tutorial     = tut2,
                Rating       = 9
            };

            context.Categories.AddOrUpdate(c => c.Title, cat1, cat2);
            context.Users.AddOrUpdate(u => u.UserName, user1, user2);
            context.Tutorials.AddOrUpdate(t => t.Title, tut1, tut2);
            context.Comments.AddOrUpdate(c => c.Content, com1, com2);
            context.Clicks.AddOrUpdate(c => c.Id, cl1, cl2);
            context.Votes.AddOrUpdate(v => v.Rating, vo1, vo2);

            context.SaveChanges();
        }
Esempio n. 21
0
        public Task <Vote> CastVote(int tutorialId, string username, int rating, string ip)
        {
            return(Task.Run(() =>
            {
                using (var ctx = new THQEntities())
                {
                    User user = ctx.Users
                                .Where(u => u.UserName == username)
                                .FirstOrDefault <User>();
                    if (user == null)
                    {
                        throw new THQNotFoundException(Strings.user);
                    }

                    Tutorial tutorial = ctx.Tutorials
                                        .Where(t => t.Id == tutorialId)
                                        .Include(t => t.User)
                                        .Include(t => t.Category)
                                        .FirstOrDefault <Tutorial>();
                    if (tutorial == null)
                    {
                        throw new THQNotFoundException(Strings.tutorial);
                    }

                    if (tutorial.User.Id == user.Id)
                    {
                        throw new THQArgumentException(Errors.cantVoteOwnTutorials);
                    }

                    if (!this.UserCanVote(username, tutorialId))
                    {
                        throw new THQArgumentException(Errors.voteLimit);
                    }

                    if (rating <= 0 || rating > 10)
                    {
                        throw new THQArgumentException(Errors.ratingRange);
                    }

                    Vote vote = new Vote()
                    {
                        Created = DateTime.Now,
                        LastModified = DateTime.MinValue,
                        Ip = ip,
                        Rating = rating,
                        Tutorial = tutorial,
                        User = user
                    };
                    ctx.Votes.Add(vote);

                    tutorial.NumVotes += 1;
                    if (tutorial.AvgRating == 0)
                    {
                        tutorial.AvgRating = rating;
                    }
                    double avgRating = ((tutorial.AvgRating * (double)(tutorial.NumVotes - 1)) + rating) / (double)tutorial.NumVotes;
                    tutorial.AvgRating = avgRating;

                    ctx.SaveChanges();
                    return vote;
                }
            }));
        }