summaryrefslogtreecommitdiff
path: root/Project-Unite/Controllers/BlogController.cs
blob: 5d964e2888c0549d0e1533032a05ec76680ae245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;

namespace Project_Unite.Controllers
{
    public class BlogController : Controller
    {
        // GET: Blog
        public ActionResult Index()
        {
            return View(new ApplicationDbContext().BlogPosts);
        }

        public ActionResult ViewBlog(string id)
        {
            var db = new ApplicationDbContext();
            var blog = db.BlogPosts.FirstOrDefault(x => x.Id == id);
            if (blog == null)
                return new HttpStatusCodeResult(404);
            return View(blog);
        }

        [Authorize]
        public ActionResult DislikePost(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.BlogPosts.FirstOrDefault(x => x.Id == id);
            var uid = User.Identity.GetUserId();
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                return RedirectToAction("Index", new { id = id, triedtolikeowntopic = true });
            var like = db.Likes.Where(x => x.Topic == topic.Id).FirstOrDefault(x => x.User == uid);
            if (like != null)
            {
                if (like.IsDislike == false)
                {
                    like.IsDislike = true;
                }
                else
                {
                    db.Likes.Remove(like);
                }
            }
            else
            {
                like = new Models.Like();
                like.Id = Guid.NewGuid().ToString();
                like.User = User.Identity.GetUserId();
                like.Topic = topic.Id;
                like.LikedAt = DateTime.Now;
                like.IsDislike = true;
                db.Likes.Add(like);
            }
            db.SaveChanges();
            return RedirectToAction("Index", new { id = id });
        }

        [Authorize]
        public ActionResult LikePost(string id)
        {
            var db = new ApplicationDbContext();
            var topic = db.BlogPosts.FirstOrDefault(x => x.Id == id);
            var uid = User.Identity.GetUserId();
            if (topic == null)
                return new HttpStatusCodeResult(404);
            if (topic.AuthorId == User.Identity.GetUserId())
                return RedirectToAction("Index", new { id = id, triedtolikeowntopic = true });
            var like = db.Likes.Where(x => x.Topic == topic.Id).FirstOrDefault(x => x.User == uid);
            if (like != null)
            {
                if (like.IsDislike == true)
                {
                    like.IsDislike = false;
                }
                else
                {
                    db.Likes.Remove(like);
                }
            }
            else
            {
                like = new Models.Like();
                like.Id = Guid.NewGuid().ToString();
                like.User = User.Identity.GetUserId();
                like.Topic = topic.Id;
                like.LikedAt = DateTime.Now;
                like.IsDislike = false;
                db.Likes.Add(like);
            }
            db.SaveChanges();
            return RedirectToAction("Index", new { id = id });
        }


        [ValidateInput(false)]
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ViewBlog(string id, string comment)
        {
            var db = new ApplicationDbContext();
            var blog = db.BlogPosts.FirstOrDefault(x => x.Id == id);
            if (blog == null)
                return new HttpStatusCodeResult(404);
            if (string.IsNullOrWhiteSpace(comment))
            {
                ViewBag.Error = "You must enter a comment with actual text in it.";
                return View(blog);
            }
            if(comment.Length < 20)
            {
                ViewBag.Error = "Your comment must have at least 20 characters in it.";
                return View(blog);
            }
            var post = new ForumPost();
            post.AuthorId = User.Identity.GetUserId();
            post.Body = comment;
            post.Id = Guid.NewGuid().ToString();
            post.Parent = id;
            post.PostedAt = DateTime.Now;
            db.ForumPosts.Add(post);
            db.SaveChanges();

            return View(blog);
        }

        [RequiresDeveloper]
        [Authorize]
        public ActionResult PostBlog()
        {
            var model = new PostBlogViewModel();
            return View(model);
        }

        [Authorize]
        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult PostBlog(PostBlogViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            var db = new ApplicationDbContext();
            var blog = new BlogPost();
            blog.AuthorId = User.Identity.GetUserId();
            blog.Contents = model.Contents;
            blog.Name = model.Name;
            blog.Id = model.Name.ToLower();
            string allowed = "-_abcdefghijklmnopqrstuvwxyz1234567890";
            foreach(var c in blog.Id.ToCharArray())
            {
                if (!allowed.Contains(c))
                    blog.Id = blog.Id.Replace(c, '_');
            }
            blog.Id += "_" + db.BlogPosts.Count().ToString();
            blog.PostedAt = DateTime.Now;
            db.BlogPosts.Add(blog);
            db.SaveChanges();
            return RedirectToAction("ViewBlog", new { id = blog.Id });
        }
    }
}