From 9f238cc47fd5b9ec546b37986371e73d6ed3c626 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 25 Mar 2017 15:59:50 -0400 Subject: [PATCH] Avatar system :smiley: --- Project-Unite/Controllers/ManageController.cs | 61 +++++++++++++++++++ Project-Unite/Models/IdentityModels.cs | 18 ++++++ Project-Unite/Project-Unite.csproj | 2 + Project-Unite/Views/Manage/Index.cshtml | 2 +- Project-Unite/Views/Manage/ListAvatars.cshtml | 22 +++++++ .../Views/Manage/UploadAvatar.cshtml | 16 +++++ 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 Project-Unite/Views/Manage/ListAvatars.cshtml create mode 100644 Project-Unite/Views/Manage/UploadAvatar.cshtml diff --git a/Project-Unite/Controllers/ManageController.cs b/Project-Unite/Controllers/ManageController.cs index 4543307..6ade427 100644 --- a/Project-Unite/Controllers/ManageController.cs +++ b/Project-Unite/Controllers/ManageController.cs @@ -20,6 +20,67 @@ namespace Project_Unite.Controllers { } + public ActionResult SetAvatar(string id) + { + var db = new ApplicationDbContext(); + var usr = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId()); + var avtr = db.UserAvatars.FirstOrDefault(x => x.Id == id); + usr.AvatarUrl = avtr.AvatarUrl; + db.SaveChanges(); + return RedirectToAction("Index"); + } + + public ActionResult UploadAvatar() + { + var model = new UploadImageViewModel(); + return View(model); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult UploadAvatar(UploadImageViewModel model) + { + string[] allowedTypes = new[] { ".png", ".jpg", ".bmp", ".jpeg", ".gif" }; + + bool containsAllowedType = !string.IsNullOrWhiteSpace(allowedTypes.FirstOrDefault(x => model.Image.FileName.EndsWith(x))); + if (containsAllowedType == false) + ModelState.AddModelError("UploadImageViewModel", new Exception("File type not allowed.")); + + if (ModelState.IsValid == true) + { + var db = new ApplicationDbContext(); + var usr = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId()); + string avatarRoot = $"~/Uploads/{usr.DisplayName}/Avatars"; + string serverPath = Server.MapPath(avatarRoot); + if (!System.IO.Directory.Exists(serverPath)) + System.IO.Directory.CreateDirectory(serverPath); + + avatarRoot += "/" + model.Image.FileName; + serverPath += "\\" + model.Image.FileName; + + model.Image.SaveAs(serverPath); + + var avatar = new Avatar(); + avatar.Id = Guid.NewGuid().ToString(); + avatar.AvatarUrl = avatarRoot.Remove(0, 1); + avatar.UserId = usr.Id; + avatar.UploadedAt = DateTime.Now; + usr.AvatarUrl = avatar.AvatarUrl; + db.UserAvatars.Add(avatar); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + + return View(model); + } + + public ActionResult ListAvatars() + { + var avatars = new ApplicationDbContext().UserAvatars.Where(x => x.UserId == User.Identity.GetUserId()); + return View(avatars); + } + public ManageController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) { UserManager = userManager; diff --git a/Project-Unite/Models/IdentityModels.cs b/Project-Unite/Models/IdentityModels.cs index cb4a712..c08e4c7 100644 --- a/Project-Unite/Models/IdentityModels.cs +++ b/Project-Unite/Models/IdentityModels.cs @@ -6,6 +6,7 @@ using System.Data.Entity.Infrastructure; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using System.Web; using System.Web.Mvc; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; @@ -19,6 +20,12 @@ namespace Project_Unite.Models public string Followed { get; set; } } + public class UploadImageViewModel + { + [Required(ErrorMessage = "Please select an image to upload.")] + public HttpPostedFileBase Image { get; set; } + } + // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { @@ -189,11 +196,14 @@ namespace Project_Unite.Models ((IObjectContextAdapter)this).ObjectContext.DeleteObject(obj); } + + public static ApplicationDbContext Create() { return new ApplicationDbContext(); } + public DbSet UserAvatars { get; set; } public DbSet Skins { get; set; } public DbSet Configs { get; set; } public DbSet ShiftoriumUpgrades { get; set; } @@ -259,4 +269,12 @@ namespace Project_Unite.Models public string UserId { get; set; } public string EngineStoryId { get; set; } } + + public class Avatar + { + public string Id { get; set; } + public string UserId { get; set; } + public string AvatarUrl { get; set; } + public DateTime UploadedAt { get; set; } + } } \ No newline at end of file diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 09c07a5..34ecf81 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -525,6 +525,8 @@ + + diff --git a/Project-Unite/Views/Manage/Index.cshtml b/Project-Unite/Views/Manage/Index.cshtml index 45d097a..75204eb 100644 --- a/Project-Unite/Views/Manage/Index.cshtml +++ b/Project-Unite/Views/Manage/Index.cshtml @@ -27,7 +27,7 @@
- +
Full name:Full name: @using(Html.BeginForm("ChangeFullName", "Manage")) { diff --git a/Project-Unite/Views/Manage/ListAvatars.cshtml b/Project-Unite/Views/Manage/ListAvatars.cshtml new file mode 100644 index 0000000..d03ea88 --- /dev/null +++ b/Project-Unite/Views/Manage/ListAvatars.cshtml @@ -0,0 +1,22 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Change avatar"; +} + +

Change avatar

+ +

You can choose a new avatar on this page - you can set an existing avatar that you've uploaded or upload a new one.

+ +
+
+ +
+ + @foreach(var avatar in Model.OrderByDescending(x=>x.UploadedAt)) + { +
+ +
+ } +
diff --git a/Project-Unite/Views/Manage/UploadAvatar.cshtml b/Project-Unite/Views/Manage/UploadAvatar.cshtml new file mode 100644 index 0000000..81e40c4 --- /dev/null +++ b/Project-Unite/Views/Manage/UploadAvatar.cshtml @@ -0,0 +1,16 @@ +@model Project_Unite.Models.UploadImageViewModel +@{ + ViewBag.Title = "Upload avatar"; +} + +

Upload an avatar

+ +

Let's put a face on your profile, shall we? Upload an image from your local computer and we'll display it as your avatar.

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + @Html.ValidationSummary() +

File to upload: @Html.TextBoxFor(Model=>Model.Image, new { @class = "form-control", type = "file" })

+ +} \ No newline at end of file