Skip to content

Commit c368341

Browse files
committed
Favorite list
1 parent 6905680 commit c368341

20 files changed

+119
-2
lines changed

EPAM.MyBlog.DAL.DB/DAL.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,5 +895,46 @@ public void DeleteUser(List<string> names)
895895
}
896896

897897

898+
899+
public IEnumerable<Entities.PresentPost> GetAllFavorite(string name)
900+
{
901+
using (SqlConnection con = new SqlConnection(ConnectionString))
902+
{
903+
SqlCommand command = new SqlCommand("SELECT Posts.Post_Title, Posts.Post_Id FROM dbo.Posts INNER JOIN dbo.Favorite ON Favorite.Post_Id = Posts.Post_Id WHERE Login = @Login ORDER BY Time DESC", con);
904+
command.Parameters.Add(new SqlParameter("@Login", name));
905+
con.Open();
906+
var reader = command.ExecuteReader();
907+
908+
while (reader.Read())
909+
{
910+
yield return new Entities.PresentPost()
911+
{
912+
Id = new Guid((string)reader["Post_Id"]),
913+
Title = (string)reader["Post_Title"]
914+
};
915+
916+
}
917+
}
918+
}
919+
920+
public bool DeletePostFromFav(string name, Guid id)
921+
{
922+
using (SqlConnection con = new SqlConnection(ConnectionString))
923+
{
924+
SqlCommand command = new SqlCommand("DELETE FROM dbo.Favorite WHERE Post_Id = @Id AND Login = @Login", con);
925+
command.Parameters.Add(new SqlParameter("@Id", id));
926+
command.Parameters.Add(new SqlParameter("@Login", name));
927+
con.Open();
928+
int count = command.ExecuteNonQuery();
929+
if (count > 0)
930+
{
931+
return true;
932+
}
933+
else
934+
{
935+
return false;
936+
}
937+
}
938+
}
898939
}
899940
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

EPAM.MyBlog.UI.Web/Controllers/PostController.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public ActionResult Index()
1717
return View();
1818
}
1919

20+
21+
public ActionResult Favorite()
22+
{
23+
return View(PresentPostModel.GetAllFavorite(User.Identity.Name));
24+
}
25+
2026
public ActionResult NewPost()
2127
{
2228
return View();
@@ -98,7 +104,6 @@ public ActionResult Edit(PostModel post)
98104
public ActionResult Delete(Guid Id)
99105
{
100106
var post = PostModel.GetPostById(Id);
101-
//ViewData["Post_Id"] = Id;
102107
ViewData["Title"] = post.Title;
103108
return View();
104109
}
@@ -120,6 +125,30 @@ public ActionResult Delete(Guid id, ConfirmModel model)
120125
return View();
121126
}
122127

128+
public ActionResult DeleteFavorite(Guid Id)
129+
{
130+
var post = PostModel.GetPostById(Id);
131+
ViewData["Title"] = post.Title;
132+
return View();
133+
}
134+
135+
[HttpPost]
136+
public ActionResult DeleteFavorite(Guid id, ConfirmModel model)
137+
{
138+
if (ModelState.IsValid)
139+
{
140+
if (PostModel.DeleteFavorite(User.Identity.Name, id))
141+
{
142+
return RedirectToAction("Favorite", "Post");
143+
}
144+
else
145+
{
146+
return View();
147+
}
148+
}
149+
return View();
150+
}
151+
123152
public ActionResult Comments(Guid id)
124153
{
125154
//

EPAM.MyBlog.UI.Web/EPAM.MyBlog.UI.Web.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@
297297
<Content Include="Views\Search\Result.cshtml" />
298298
<Content Include="Views\Admin\Create.cshtml" />
299299
<Content Include="Views\Search\Tags.cshtml" />
300+
<Content Include="Views\Post\Favorite.cshtml" />
301+
<Content Include="Views\Post\DeleteFavorite.cshtml" />
300302
</ItemGroup>
301303
<ItemGroup>
302304
<Folder Include="App_Data\" />

EPAM.MyBlog.UI.Web/Models/PostModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,10 @@ internal static bool AddFavorite(string name, Guid Id)
100100
{
101101
return GetDAL.dal.AddFavorite(name, Id);
102102
}
103+
104+
internal static bool DeleteFavorite(string name, Guid id)
105+
{
106+
return GetDAL.dal.DeletePostFromFav(name, id);
107+
}
103108
}
104109
}

EPAM.MyBlog.UI.Web/Models/PresentPostModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ internal static IEnumerable<PresentPostModel> GetAllPostsTitle()
4848
}
4949
return TitleList;
5050
}
51-
51+
52+
53+
internal static IEnumerable<PresentPostModel> GetAllFavorite(string name)
54+
{
55+
var list = GetDAL.dal.GetAllFavorite(name).ToList();
56+
List<PresentPostModel> TitleList = new List<PresentPostModel>();
57+
foreach (var item in list)
58+
{
59+
TitleList.Add((PresentPostModel)item);
60+
}
61+
return TitleList;
62+
}
5263
}
5364
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@model EPAM.MyBlog.UI.Web.Models.ConfirmModel
2+
3+
@{
4+
ViewBag.Title = "Delete";
5+
}
6+
7+
<h2>Вы действиетльно хотите удалить запись @ViewData["Title"]</h2>
8+
@using (Html.BeginForm())
9+
{
10+
<button name="Confirm" value ="true">Да</button>
11+
<button name="Confirm" value ="false">Нет</button>
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@model IEnumerable<EPAM.MyBlog.UI.Web.Models.PresentPostModel>
2+
3+
4+
5+
@foreach (var item in Model)
6+
{
7+
<div class="row list">
8+
<div class="col-sm-8 col-md-8 col-lg-8 col-sx-8">
9+
@Html.ActionLink(@item.Title, "Posts", new { id=item.Id })
10+
</div>
11+
<div class="col-sm-4 col-md-4 col-lg-4 col-sx-4">
12+
@Html.ActionLink("Удалить", "DeleteFavorite", new { id=item.Id }, new { @class="btn btn-default"})
13+
</div>
14+
</div>
15+
16+
}
17+
1.5 KB
Binary file not shown.
Binary file not shown.
512 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
512 Bytes
Binary file not shown.
Binary file not shown.

EPAM.MyBlog.v11.suo

18 KB
Binary file not shown.

0 commit comments

Comments
 (0)