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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Project_Unite.Models;
namespace Project_Unite.Controllers
{
[Authorize]
public class ProfilesController : Controller
{
// GET: Profiles
public ActionResult Index()
{
return View();
}
public ActionResult ViewProfile(string id)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.DisplayName == id);
if (user == null)
return new HttpStatusCodeResult(404);
return View(user);
}
public ActionResult UnfollowUser(string id)
{
var db = new ApplicationDbContext();
if (db.Users.FirstOrDefault(x => x.Id == id) == null)
return new HttpStatusCodeResult(404);
if (!ACL.IsFollowed(User.Identity.Name, id))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var uid = User.Identity.GetUserId();
var userToFollow = db.Users.FirstOrDefault(x => x.Id == id);
var follow = db.Follows.FirstOrDefault(x=>x.Followed==userToFollow.Id&&x.Follower==uid);
if(follow != null)
db.Follows.Remove(follow);
db.SaveChanges();
return RedirectToAction("ViewProfile", new { id = userToFollow.DisplayName });
}
public ActionResult FollowUser(string id)
{
var db = new ApplicationDbContext();
if (db.Users.FirstOrDefault(x => x.Id == id) == null)
return new HttpStatusCodeResult(404);
if (ACL.IsFollowed(User.Identity.Name, id))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var uid = User.Identity.GetUserId();
var userToFollow = db.Users.FirstOrDefault(x => x.Id == id);
var follow = new UserFollow();
follow.Id = Guid.NewGuid().ToString();
follow.Followed = id;
follow.Follower = uid;
db.Follows.Add(follow);
db.SaveChanges();
return RedirectToAction("ViewProfile", new { id = userToFollow.DisplayName });
}
[Authorize]
public ActionResult DislikePost(string id)
{
var db = new ApplicationDbContext();
var topic = db.UserPosts.FirstOrDefault(x => x.Id == id);
var uid = User.Identity.GetUserId();
if (topic == null)
return new HttpStatusCodeResult(404);
if (topic.UserId == User.Identity.GetUserId())
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(uid) });
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);
}
string tid = topic.UserId;
db.SaveChanges();
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(tid) });
}
[Authorize]
public ActionResult LikePost(string id)
{
var db = new ApplicationDbContext();
var topic = db.UserPosts.FirstOrDefault(x => x.Id == id);
var uid = User.Identity.GetUserId();
if (topic == null)
return new HttpStatusCodeResult(404);
if (topic.UserId == User.Identity.GetUserId())
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(uid) });
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);
}
string tid = topic.UserId;
db.SaveChanges();
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(tid) });
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PostContent(UserPost model)
{
var db = new ApplicationDbContext();
model.Id = Guid.NewGuid().ToString();
model.PostedAt = DateTime.Now;
model.UserId = User.Identity.GetUserId();
db.UserPosts.Add(model);
db.SaveChanges();
NotificationDaemon.NotifyFollowers(User.Identity.GetUserId(), "New post from " + ACL.UserNameRaw(User.Identity.GetUserId()) + "!", ACL.UserNameRaw(User.Identity.GetUserId()) + " just posted something on their profile.", Url.Action("ViewProfile", new { id = ACL.UserNameRaw(User.Identity.GetUserId()) }));
return RedirectToAction("ViewProfile", "Profiles", new { id = ACL.UserNameRaw(User.Identity.GetUserId()) });
}
}
}
|