Ejemplo n.º 1
0
        public ActionResult SignUp(AuthUserModel signupUser)
        {
            User user;

            using (var cx = new ViralContext())
            {
                if (cx.Users.Any(u => u.Username == signupUser.Username))
                {
                    return(View("PartialSignup", signupUser));
                }

                user = cx.Users.Add(new User
                {
                    Username       = signupUser.Username,
                    Password       = signupUser.Password,
                    Email          = signupUser.Email,
                    AvatarPath     = signupUser.AvatarPath,
                    RegisteredTime = DateTime.UtcNow
                });

                cx.SaveChanges();
            }

            FormsAuthentication.SetAuthCookie(user.Id.ToString(), true);
            return(RedirectToAction("Index", "Home", user));
        }
Ejemplo n.º 2
0
 public static void Get(int id)
 {
     using (var cx = new ViralContext())
     {
         cx.Users.Find(id);
     }
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            using (var cx = new ViralContext())
            {
                var user = new User
                {
                    Email          = "*****@*****.**",
                    Username       = "******",
                    Password       = "******",
                    RegisteredTime = DateTime.UtcNow
                };

                var post = new Post
                {
                    Title       = "Funny title",
                    Text        = "Funny text",
                    PostedTime  = DateTime.UtcNow,
                    ContentPath = "url"
                };

                cx.Users.Add(user);
                user.PostedPosts = new List <Post> {
                    post
                };
                cx.Posts.Add(post);
                cx.SaveChanges();

                /*var user1 = cx.Users.Find(1);
                 * var user2 = cx.Users.Find(2);
                 * var posts = cx.Posts.Where(p => p.Author.Id == 2).ToList();*/
            }

            Console.WriteLine("Finished");
            Console.ReadKey();
        }
Ejemplo n.º 4
0
 public static void Add(User user)
 {
     using (var cx = new ViralContext())
     {
         cx.Users.Add(user);
         cx.SaveChanges();
     }
 }
Ejemplo n.º 5
0
 private bool Authenticate(AuthUserModel authUser, out User user)
 {
     using (var cx = new ViralContext())
     {
         user = cx.Users.SingleOrDefault(u => authUser.Username.Equals(u.Username, StringComparison.OrdinalIgnoreCase) && u.Password == authUser.Password);
         return(user != null);
     }
 }
Ejemplo n.º 6
0
        private ActionResult Rate(int?id, RatingType type)
        {
            using (var cx = new ViralContext())
            {
                var existingRating = cx.Ratings.SingleOrDefault(r => r.User.Id == CurrentUserId && r.Comment.Id == id);
                if (existingRating != null)
                {
                    existingRating.RatedTime = DateTime.UtcNow;
                    existingRating.Type      = type;

                    cx.Entry(existingRating).State = EntityState.Modified;
                }
                else
                {
                    var comment = cx.Comments
                                  .Where(p => p.Id == id)
                                  .Include(p => p.Ratings)
                                  .SingleOrDefault();

                    if (comment == null)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    var user = cx.Users
                               .Where(u => u.Id == CurrentUserId)
                               .Include(u => u.Ratings)
                               .Single();

                    var rating = new Rating
                    {
                        RatedTime = DateTime.UtcNow,
                        Type      = type
                    };

                    if (user.Ratings == null)
                    {
                        user.Ratings = new List <Rating>();
                    }
                    user.Ratings.Add(rating);

                    if (comment.Ratings == null)
                    {
                        comment.Ratings = new List <Rating>();
                    }
                    comment.Ratings.Add(rating);

                    cx.Entry(user).State    = EntityState.Modified;
                    cx.Entry(comment).State = EntityState.Modified;
                }

                cx.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }
Ejemplo n.º 7
0
        public ActionResult UserProfile(int id)
        {
            User model;

            using (var cx = new ViralContext())
            {
                model = cx.Users.Find(id);
            }

            return(View("Index", model));
        }
Ejemplo n.º 8
0
        public ActionResult Edit(int id)
        {
            Post post;

            using (var cx = new ViralContext())
            {
                post = cx.Posts.Find(id);
                cx.SaveChanges();
            }

            return(View(post));
        }
Ejemplo n.º 9
0
        public ActionResult Index(int id)
        {
            Post post;

            using (var cx = new ViralContext())
            {
                post = cx.Posts
                       .Where(p => p.Id == id)
                       .Where(p => !p.IsDeleted)
                       .Include(p => p.Ratings)
                       .Include(p => p.Comments)
                       .Include(p => p.Author)
                       .SingleOrDefault();
            }

            return(View(new PostModel(post)));
        }
Ejemplo n.º 10
0
        public ActionResult GetByPost(int postId)
        {
            var posts = new List <PostModel>();

            using (var cx = new ViralContext())
            {
                foreach (var post in cx.Posts
                         .Where(p => !p.IsDeleted)
                         .Include(p => p.Ratings)
                         .Include(p => p.Comments)
                         .Include(p => p.Author))
                {
                    posts.Add(new PostModel(post));
                }
            }

            return(Json(posts.ToJson()));
        }
Ejemplo n.º 11
0
        public ActionResult GetTop(int pageIndex, int pageSize)
        {
            var posts = new List <PostModel>();

            using (var cx = new ViralContext())
            {
                foreach (var post in cx.Posts
                         .Where(p => !p.IsDeleted)
                         .Include(p => p.Ratings)
                         .Include(p => p.Comments)
                         .Include(p => p.Author))
                {
                    posts.Add(new PostModel(post));
                }
            }

            return(Json(posts, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 12
0
        public ActionResult Delete(int?id)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                var savedComment = cx.Comments.SingleOrDefault(c => c.Id == id && c.Author.Id == currentUser.Id);
                if (savedComment == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                savedComment.IsDeleted       = true;
                cx.Entry(savedComment).State = EntityState.Modified;
                cx.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }
Ejemplo n.º 13
0
        public ActionResult Edit(Comment comment)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                var savedComment = cx.Comments.SingleOrDefault(c => c.Id == comment.Id && c.Author.Id == currentUser.Id);
                if (savedComment == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                comment.PostedTime = savedComment.PostedTime;
                cx.Comments.AddOrUpdate(comment);
                cx.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }
Ejemplo n.º 14
0
        public ActionResult Edit(Post post)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                var savedPost = cx.Posts.SingleOrDefault(p => p.Id == post.Id && p.Author.Id == currentUser.Id);
                if (savedPost == null)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                post.LastEditTime = savedPost.LastEditTime;
                cx.Posts.AddOrUpdate(post);
                cx.SaveChanges();

                return(Index(post.Id));
            }
        }
Ejemplo n.º 15
0
        public ActionResult Create(Comment comment)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                comment.PostedTime = DateTime.UtcNow;

                if (currentUser.PostedComments == null)
                {
                    currentUser.PostedComments = new List <Comment>();
                }
                currentUser.PostedComments.Add(comment);

                cx.Entry(currentUser).State = EntityState.Modified;
                cx.Entry(comment).State     = EntityState.Added;

                cx.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 16
0
        public ActionResult Create(Post post)
        {
            var currentUser = CurrentUser;

            using (var cx = new ViralContext())
            {
                post.PostedTime   = DateTime.UtcNow;
                post.LastEditTime = post.PostedTime;

                if (currentUser.PostedPosts == null)
                {
                    currentUser.PostedPosts = new List <Post>();
                }
                currentUser.PostedPosts.Add(post);

                cx.Entry(currentUser).State = EntityState.Modified;
                cx.Entry(post).State        = EntityState.Added;

                cx.SaveChanges();
            }

            return(View("Create"));
        }