Skip to content

Commit 0943d14

Browse files
committed
Almost get avatar
1 parent 9c87540 commit 0943d14

39 files changed

+234
-54
lines changed

EPAM.MyBlog.DAL.DB/DAL.cs

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public Entities.User Login(string Name)
2828
command.Parameters.Add(new SqlParameter("@Name", Name));
2929
con.Open();
3030
var reader = command.ExecuteReader();
31-
return new Entities.User()
32-
{
33-
Name = (string)reader["Login"],
34-
Password = (string)reader["Password"],
35-
Email = (string)reader["E_mail"],
36-
Role_Id = (int)reader["Role_Id"]
37-
};
31+
return new Entities.User()
32+
{
33+
Name = (string)reader["Login"],
34+
Password = (string)reader["Password"],
35+
Email = (string)reader["E_mail"],
36+
Role_Id = (int)reader["Role_Id"]
37+
};
3838
}
3939

4040
}
@@ -58,7 +58,7 @@ public bool Registration(Entities.User user, int role = 3)
5858
}
5959
}
6060

61-
public byte[] CheckLogPas(string Name)
61+
public byte[] CheckLogPas(string Name)
6262
{
6363

6464
using (SqlConnection con = new SqlConnection(ConnectionString))
@@ -74,7 +74,7 @@ public byte[] CheckLogPas(string Name)
7474
count++;
7575
Pass = (byte[])reader["Password"];
7676
}
77-
if(count<1)
77+
if (count < 1)
7878
{
7979
return Pass = null;
8080
}
@@ -166,7 +166,7 @@ public bool AddPost(Entities.PostText post, string login)
166166
con.Open();
167167
var reader = command.ExecuteReader();
168168

169-
while (reader.Read())
169+
while (reader.Read())
170170
{
171171
yield return new Entities.PresentPost()
172172
{
@@ -186,16 +186,16 @@ public Entities.PostText GetPostById(Guid Id)
186186
SqlCommand command = new SqlCommand("SELECT Post_Title, Post_Text, User_Name, Time FROM dbo.Posts WHERE Post_Id = @Id", con);
187187
command.Parameters.Add(new SqlParameter("@Id", Id));
188188
con.Open();
189-
Entities.PostText post = new Entities.PostText() {Id = Id };
189+
Entities.PostText post = new Entities.PostText() { Id = Id };
190190
int count = 0;
191191
var reader = command.ExecuteReader();
192192

193193
while (reader.Read())
194194
{
195-
post.Title = (string)reader["Post_Title"];
196-
post.Text = (string)reader["Post_Text"];
197-
post.Author = (string)reader["User_Name"];
198-
post.Time = (DateTime)reader["Time"];
195+
post.Title = (string)reader["Post_Title"];
196+
post.Text = (string)reader["Post_Text"];
197+
post.Author = (string)reader["User_Name"];
198+
post.Time = (DateTime)reader["Time"];
199199
count++;
200200
}
201201
if (count < 0)
@@ -249,7 +249,7 @@ public bool DeletePostById(Guid Id)
249249
return false;
250250
}
251251
}
252-
252+
253253
}
254254

255255
#endregion
@@ -271,7 +271,7 @@ public string[] GetAllRoles()
271271
}
272272
return roles;
273273
}
274-
274+
275275
}
276276

277277
public int GetRoleForUser(string username)
@@ -315,7 +315,7 @@ public bool AddComment(Entities.Comment comment)
315315
{
316316
return false;
317317
}
318-
}
318+
}
319319
}
320320

321321
public IEnumerable<Entities.Comment> GetAllComments(Guid Post_ID)
@@ -412,7 +412,7 @@ public Entities.UserInfo GetUserInfo(string name)
412412
{
413413
using (SqlConnection con = new SqlConnection(ConnectionString))
414414
{
415-
SqlCommand command = new SqlCommand("SELECT Avatar, Type, Sex, Birthday, True_Name, About FROM dbo.UserAbout RIGHT JOIN Users ON Users.Login = UserAbout.Login Where Users.Login = @Login", con);
415+
SqlCommand command = new SqlCommand("SELECT Sex, Birthday, True_Name, About FROM dbo.UserAbout RIGHT JOIN Users ON Users.Login = UserAbout.Login Where Users.Login = @Login", con);
416416
command.Parameters.Add(new SqlParameter("@Login", name));
417417
con.Open();
418418
Entities.UserInfo info = new Entities.UserInfo() { Login = name };
@@ -421,23 +421,6 @@ public Entities.UserInfo GetUserInfo(string name)
421421

422422
while (reader.Read())
423423
{
424-
if (DBNull.Value.Equals(reader["Avatar"]))
425-
{
426-
info.Avatar = null;
427-
}
428-
else
429-
{
430-
info.Avatar = (byte[])reader["Avatar"];
431-
}
432-
433-
if (DBNull.Value.Equals(reader["Type"]))
434-
{
435-
info.MimeType = null;
436-
}
437-
else
438-
{
439-
info.MimeType = (string)reader["Type"];
440-
}
441424
if (DBNull.Value.Equals(reader["Sex"]))
442425
{
443426
info.Sex = null;
@@ -522,7 +505,7 @@ public bool SaveDate(Entities.UserInfo info)
522505
{
523506
using (SqlConnection con = new SqlConnection(ConnectionString))
524507
{
525-
508+
526509
SqlCommand command = new SqlCommand("UPDATE dbo.UserAbout SET Birthday = @Birthday WHERE Login = @Login", con);
527510
if (info.Birthday == null)
528511
{
@@ -585,5 +568,70 @@ public bool SaveAbout(Entities.UserInfo info)
585568
}
586569
}
587570
}
588-
}
571+
572+
public Entities.Avatar GetAvatarInfo(string name)
573+
{
574+
using (SqlConnection con = new SqlConnection(ConnectionString))
575+
{
576+
SqlCommand command = new SqlCommand("SELECT Avatar, Type FROM dbo.UserAbout RIGHT JOIN Users ON Users.Login = UserAbout.Login Where Users.Login = @Login", con);
577+
command.Parameters.Add(new SqlParameter("@Login", name));
578+
con.Open();
579+
Entities.Avatar info = new Entities.Avatar() { Login = name };
580+
int count = 0;
581+
var reader = command.ExecuteReader();
582+
583+
while (reader.Read())
584+
{
585+
if (DBNull.Value.Equals(reader["Avatar"]))
586+
{
587+
info.Pic = null;
588+
}
589+
else
590+
{
591+
info.Pic = (byte[])reader["Avatar"];
592+
}
593+
594+
if (DBNull.Value.Equals(reader["Type"]))
595+
{
596+
info.MimeType = null;
597+
}
598+
else
599+
{
600+
info.MimeType = (string)reader["Type"];
601+
}
602+
count++;
603+
}
604+
if (count < 0)
605+
{
606+
return info = null;
607+
}
608+
else
609+
{
610+
return info;
611+
}
612+
}
613+
}
614+
615+
616+
public bool AddAvatar(Entities.Avatar avatar)
617+
{
618+
using (SqlConnection con = new SqlConnection(ConnectionString))
619+
{
620+
SqlCommand command = new SqlCommand("UPDATE dbo.UserAbout SET Avatar = @Pic, Type = @Type WHERE Login = @Login", con);
621+
command.Parameters.Add(new SqlParameter("@Pic", avatar.Pic));
622+
command.Parameters.Add(new SqlParameter("@Type", avatar.MimeType));
623+
command.Parameters.Add(new SqlParameter("@Login", avatar.Login));
624+
con.Open();
625+
int count = command.ExecuteNonQuery();
626+
if (count > 0)
627+
{
628+
return true;
629+
}
630+
else
631+
{
632+
return false;
633+
}
634+
}
635+
}
636+
}
589637
}
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

EPAM.MyBlog.UI.Web/Content/css/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ p {
124124

125125
.list > div{
126126
margin-top: 20px;
127+
}
128+
129+
.table-condensed tbody
130+
{
131+
overflow:auto;
127132
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ public ActionResult DeleteAc(ConfirmModel model)
166166
return View();
167167
}
168168

169+
public ActionResult Avatar()
170+
{
171+
172+
var info = AvatarModel.GetInfo(User.Identity.Name);
173+
ViewData["path"] = "~/Content/img/" + User.Identity.Name + ".jpg";
174+
var file = File(info.Avatar, info.MimeType, User.Identity.Name + ".jpg");
175+
176+
return PartialView(info);
177+
}
169178

170179
public ActionResult AboutMe()
171180
{

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using EPAM.MyBlog.UI.Web.Models;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Web;
@@ -16,9 +17,32 @@ public ActionResult Index()
1617
return View();
1718
}
1819

19-
public ActionResult UploadAvatar(HttpPostedFile file)
20+
public ActionResult EditInfoAvatar()
2021
{
21-
return RedirectToAction("Index");
22+
return View();
23+
}
24+
25+
[HttpPost]
26+
[ValidateAntiForgeryToken]
27+
public ActionResult EditInfoAvatar(HttpPostedFileBase File)
28+
{
29+
var model = new AvatarModel();
30+
model.Login = User.Identity.Name;
31+
32+
if (ModelState.IsValid)
33+
{
34+
if (File != null)
35+
{
36+
model.MimeType = File.ContentType;
37+
model.Avatar = new byte[File.ContentLength];
38+
File.InputStream.Read(model.Avatar, 0, File.ContentLength);
39+
}
40+
41+
model.AddPhoto();
42+
return RedirectToAction("AboutMe", "Account");
43+
}
44+
return View();
45+
2246
}
2347
}
2448
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<Compile Include="Global.asax.cs">
121121
<DependentUpon>Global.asax</DependentUpon>
122122
</Compile>
123+
<Compile Include="Models\AvatarModel.cs" />
123124
<Compile Include="Models\UserAboutModel.cs" />
124125
<Compile Include="Models\CommentModel.cs" />
125126
<Compile Include="Models\ConfirmModel.cs" />
@@ -284,6 +285,8 @@
284285
<Content Include="Views\Account\EditDate.cshtml" />
285286
<Content Include="Views\Account\EditName.cshtml" />
286287
<Content Include="Views\Account\EditAbout.cshtml" />
288+
<Content Include="Views\Files\EditInfoAvatar.cshtml" />
289+
<Content Include="Views\Account\Avatar.cshtml" />
287290
</ItemGroup>
288291
<ItemGroup>
289292
<Folder Include="App_Data\" />

EPAM.MyBlog.UI.Web/Logs/Debug/2015.01.09.log

Whitespace-only changes.

EPAM.MyBlog.UI.Web/Logs/Errors/2015.01.09.log

Whitespace-only changes.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ namespace EPAM.MyBlog.UI.Web.Models
77
{
88
public class AvatarModel
99
{
10+
public string Login { get { return login; } set { login = value; } }
11+
public byte[] Avatar { get { return avatar; } set { avatar = value; } }
12+
public string MimeType { get { return type; } set { type = value; } }
1013

14+
private string login;
15+
private byte[] avatar;
16+
private string type;
17+
18+
public static explicit operator Entities.Avatar(AvatarModel Info)
19+
{
20+
return new Entities.Avatar() { Login = Info.Login, Pic = Info.Avatar, MimeType = Info.MimeType };
21+
}
22+
23+
public static explicit operator AvatarModel(Entities.Avatar Info)
24+
{
25+
return new AvatarModel() { Login = Info.Login, Avatar = Info.Pic, MimeType = Info.MimeType };
26+
}
27+
28+
29+
internal static AvatarModel GetInfo(string name)
30+
{
31+
var avatarinfo = (AvatarModel)GetDAL.dal.GetAvatarInfo(name);
32+
return avatarinfo;
33+
}
34+
35+
internal void AddPhoto()
36+
{
37+
var avatar = (Entities.Avatar)this;
38+
GetDAL.dal.AddAvatar(avatar);
39+
}
1140
}
1241
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace EPAM.MyBlog.UI.Web.Models
1111
public class UserAboutModel
1212
{
1313
public string Login { get { return login; } set { login = value; } }
14-
public byte[] Avatar { get { return avatar; } set { avatar = value; } }
15-
public string MimeType { get { return type; } set { type = value; } }
14+
1615
[Display(Name = "Пол")]
1716
public string Sex { get { return sex; } set { sex = value; } }
1817
[Display(Name = "День Рождения")]
@@ -24,21 +23,20 @@ public class UserAboutModel
2423
public string AboutMe { get { return info; } set { info = value; } }
2524

2625
private string login;
27-
private byte[] avatar;
28-
private string type;
26+
2927
private string sex;
3028
private Nullable<DateTime> birthday;
3129
private string name;
3230
private string info;
3331

3432
public static explicit operator Entities.UserInfo(UserAboutModel Info)
3533
{
36-
return new Entities.UserInfo() { Login = Info.Login, AboutMe = Info.AboutMe, Avatar = Info.Avatar, Birthday = Info.Birthday, MimeType = Info.MimeType, Name = Info.Name, Sex = Info.Sex };
34+
return new Entities.UserInfo() { Login = Info.Login, AboutMe = Info.AboutMe, Birthday = Info.Birthday, Name = Info.Name, Sex = Info.Sex };
3735
}
3836

3937
public static explicit operator UserAboutModel(Entities.UserInfo Info)
4038
{
41-
return new UserAboutModel() { Login = Info.Login, AboutMe = Info.AboutMe, Avatar = Info.Avatar, Birthday = Info.Birthday, MimeType = Info.MimeType, Name = Info.Name, Sex = Info.Sex };
39+
return new UserAboutModel() { Login = Info.Login, AboutMe = Info.AboutMe, Birthday = Info.Birthday, Name = Info.Name, Sex = Info.Sex };
4240
}
4341

4442

EPAM.MyBlog.UI.Web/Views/Account/AboutMe.cshtml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
}
4848
@Html.ActionLink("Изменить", "EditAbout", null, new { @class="btn btn-default"})
4949
</fieldset>
50-
<div>
51-
@Html.ActionLink("Изменить аватар", "EditInfoAvatar", null, new { @class="btn btn-default"})
52-
</div>
50+
51+
@Html.Action("Avatar")
52+
53+

0 commit comments

Comments
 (0)