public void UpdateWallMessage(int id, string message) { Db db = new Db(); WallDTO wall = db.Wall.Find(id); wall.Message = message; wall.DateEdited = DateTime.Now; db.SaveChanges(); }
public void UpdateWallMessage(int id, string message) { //init db Db db = new Db(); //Update Wall WallDTO wall = db.Walls.Where(x => x.Id == id).FirstOrDefault(); wall.Message = message; wall.DateEdited = DateTime.Now; db.SaveChanges(); }
public void UpdateWallMessage(int id, string message) { //Init DB Db db = new Db(); //Get user id WallDTO wall = db.Walls.Find(id); wall.Message = message; wall.DateEdited = DateTime.Now; db.SaveChanges(); }
public static WallViewModel ToViewModel(this WallDTO wallDTO) { var viewModel = Mapper.Map <WallViewModel>(wallDTO); if (viewModel.Documents != null) { foreach (var wallFile in wallDTO.Documents) { viewModel.Documents.Add(new WallFileViewModel { Id = wallFile.Id, FileId = wallFile.FileId, WallId = wallFile.WallId, FileName = wallFile.File.Name, UIdFileName = wallFile.File.UIdFileName, FileUrl = GetFilePath(wallFile.File.UIdFileName) }); } } return(viewModel); }
public WallVM(WallDTO row) { Id = row.Id; Message = row.Message; DateEdited = row.DateEdited; }
public ActionResult CreateAccount(UserVM model, HttpPostedFileBase file) { //Init Db Db db = new Db(); //Check Model state if (!ModelState.IsValid) { ModelState.AddModelError("", "Please make sure all the fields are entered and correct"); return(View("Index", model)); } //Make sure username is unique if (db.Users.Any(x => x.Username == model.Username)) { ModelState.AddModelError("", "Sorry, the username already exists"); return(View("Index", model)); } //Create user Dto UserDTO user = new UserDTO(); user.FirstName = model.FirstName; user.LastName = model.LastName; user.Username = model.Username; user.EmailAddress = model.EmailAddress; user.Password = model.Password; //Add to DTO db.Users.Add(user); //Save DB db.SaveChanges(); //Get inserted ID int id = user.Id; //Login user FormsAuthentication.SetAuthCookie(model.Username, false); //Set Upload folder var uploadDir = new DirectoryInfo(string.Format("{0}{1}", Server.MapPath(@"\"), "Uploads")); //Check if file was uploaded if (file != null && file.ContentLength > 0) { //Get and verify extension string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/png" && ext != "image/gof" && ext != "image/x-png" && ext != "image/pjpeg") { ModelState.AddModelError("", "The image was not uploaded, wrong image extension"); return(View("Index", model)); } //Set image name string imgName = user.Id + ".jpg"; //Set Image path var path = string.Format("{0}\\{1}", uploadDir, imgName); //Save the image file.SaveAs(path); } //Add to wall WallDTO wall = new WallDTO() { Id = id, Message = "", DateEdited = DateTime.Now }; db.Walls.Add(wall); db.SaveChanges(); //Redirect return(Redirect("~/" + model.Username)); }
public ActionResult Username(string username = "") { Db db = new Db(); //Check if user exists if (!db.Users.Any(x => x.Username.Equals(username))) { return(Redirect("~/")); } ViewBag.username = username; //Get logged in user's username string user = User.Identity.Name; //Viewbag's user full name UserDTO userDTO = db.Users.Where(x => x.Username.Equals(user)).FirstOrDefault(); ViewBag.fullname = userDTO.FirstName + " " + userDTO.LastName; //Get viewing full name, the one signed in! UserDTO userDTO2 = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); ViewBag.viewingFullName = userDTO2.FirstName + " " + userDTO2.LastName; //Get username's image ViewBag.usernameimg = userDTO2.Id + ".jpg"; //Check if user viewing is same as user logged in string userType = "guest"; if (userDTO == userDTO2) { userType = "owner"; } //Check if both users have pending friendship or not friends if (userType == "guest") { FriendsDTO f1 = db.Friends.Where(x => x.User1.Equals(userDTO.Id) && x.User2.Equals(userDTO2.Id)).FirstOrDefault(); FriendsDTO f2 = db.Friends.Where(x => x.User2.Equals(userDTO.Id) && x.User1.Equals(userDTO2.Id)).FirstOrDefault(); if (f1 == null && f2 == null) { ViewBag.notfriends = "true"; } if (f1 != null) { if (!f1.IsActive) { ViewBag.notfriends = "pending"; } } if (f2 != null) { if (!f2.IsActive) { ViewBag.notfriends = "pending"; } } } //Get friend requests count - Viewbag var friendCount = db.Friends.Count(x => x.User2 == userDTO.Id && x.IsActive == false); if (friendCount > 0) { ViewBag.friendcount = friendCount; } //Get Friend Count - ViewBag int userId = userDTO.Id; var friendCount2 = db.Friends.Count(x => x.User1 == userId && x.IsActive == true || x.User2 == userId && x.IsActive == true); ViewBag.fCount = friendCount2; //View bag user ID ViewBag.userId = userId; ViewBag.usertype = userType; //Get Message Count var messageCount = db.Messages.Count(x => x.To == userId && x.Read == false); //Viewbag message count ViewBag.msgCount = messageCount; //Viewbag user wall WallDTO wall = new WallDTO(); ViewBag.wallMessage = db.Walls.Where(x => x.Id == userId).Select(x => x.Message).FirstOrDefault(); //View bag friend walls List <int> friendIds1 = db.Friends.Where(x => x.User1 == userId && x.IsActive == true) .ToArray().Select(x => x.User2).ToList(); List <int> friendIds2 = db.Friends.Where(x => x.User2 == userId && x.IsActive == true) .ToArray().Select(x => x.User1).ToList(); List <int> allFriendIds = friendIds1.Concat(friendIds2).ToList(); List <WallVM> allWalls = db.Walls.Where(x => allFriendIds.Contains(x.Id)) .ToArray().OrderByDescending(x => x.DateEdited) .Select(x => new WallVM(x)).ToList(); ViewBag.walls = allWalls; return(View()); }
public ActionResult CreateAccount(UserVM model, HttpPostedFileBase file) { Db db = new Db(); if (!ModelState.IsValid) { return(View("Index", model)); } if (db.Users.Any(x => x.Username.Equals(model.Username))) { ModelState.AddModelError("", "Username " + model.Username + " is taken."); model.Username = ""; return(View("Index", model)); } UserDTO userDTO = new UserDTO() { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, Username = model.Username, Password = model.Password }; db.Users.Add(userDTO); db.SaveChanges(); int userId = userDTO.Id; FormsAuthentication.SetAuthCookie(model.Username, false); var uploadsDir = new DirectoryInfo(string.Format("{0}Uploads", Server.MapPath(@"\"))); if (file != null && file.ContentLength > 0) { string ext = file.ContentType.ToLower(); if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "image/png") { ModelState.AddModelError("", "The image was not uploaded - wrong image extension."); return(View("Index", model)); } string imageName = userId + ".jpg"; var path = string.Format("{0}\\{1}", uploadsDir, imageName); file.SaveAs(path); } WallDTO wall = new WallDTO(); wall.Id = userId; wall.Message = ""; wall.DateEdited = DateTime.Now; db.Wall.Add(wall); db.SaveChanges(); // Redirect return(Redirect("~/" + model.Username)); }
public ActionResult Username(string username = "") { Db db = new Db(); if (!db.Users.Any(x => x.Username.Equals(username))) { return(Redirect("~/")); } ViewBag.Username = username; string user = User.Identity.Name; UserDTO userDTO = db.Users.Where(x => x.Username.Equals(user)).FirstOrDefault(); ViewBag.FullName = userDTO.FirstName + " " + userDTO.LastName; int userId = userDTO.Id; ViewBag.UserId = userId; UserDTO userDTO2 = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); ViewBag.ViewingFullName = userDTO2.FirstName + " " + userDTO2.LastName; ViewBag.UsernameImage = userDTO2.Id + ".jpg"; string userType = "guest"; if (username.Equals(user)) { userType = "owner"; } ViewBag.UserType = userType; if (userType == "guest") { UserDTO u1 = db.Users.Where(x => x.Username.Equals(user)).FirstOrDefault(); int id1 = u1.Id; UserDTO u2 = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); int id2 = u2.Id; FriendDTO f1 = db.Friends.Where(x => x.User1 == id1 && x.User2 == id2).FirstOrDefault(); FriendDTO f2 = db.Friends.Where(x => x.User2 == id1 && x.User1 == id2).FirstOrDefault(); if (f1 == null && f2 == null) { ViewBag.NotFriends = "True"; } if (f1 != null) { if (!f1.Active) { ViewBag.NotFriends = "Pending"; } } if (f2 != null) { if (!f2.Active) { ViewBag.NotFriends = "Pending"; } } } var friendCount = db.Friends.Count(x => x.User2 == userId && x.Active == false); if (friendCount > 0) { ViewBag.FRCount = friendCount; } UserDTO uDTO = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); int usernameId = uDTO.Id; var friendCount2 = db.Friends.Count(x => x.User2 == usernameId && x.Active == true || x.User1 == usernameId && x.Active == true); ViewBag.FCount = friendCount2; var messageCount = db.Messages.Count(x => x.To == userId && x.Read == false); ViewBag.MsgCount = messageCount; WallDTO wall = new WallDTO(); ViewBag.WallMessage = db.Wall.Where(x => x.Id == userId).Select(x => x.Message).FirstOrDefault(); List <int> friendIds1 = db.Friends.Where(x => x.User1 == userId && x.Active == true).ToArray().Select(x => x.User2).ToList(); List <int> friendIds2 = db.Friends.Where(x => x.User2 == userId && x.Active == true).ToArray().Select(x => x.User1).ToList(); List <int> allFriendsIds = friendIds1.Concat(friendIds2).ToList(); List <WallVM> walls = db.Wall.Where(x => allFriendsIds.Contains(x.Id)).ToArray().OrderByDescending(x => x.DateEdited).Select(x => new WallVM(x)).ToList(); ViewBag.Walls = walls; // Return return(View()); }
public ActionResult CreateAccount(UserVM model, HttpPostedFileBase file) { // Init db Db db = new Db(); // Check model state if (!ModelState.IsValid) { return(View("Index", model)); } // Make sure username is unique if (db.Users.Any(x => x.Username.Equals(model.Username))) { ModelState.AddModelError("", "Username " + model.Username + " is taken."); model.Username = ""; return(View("Index", model)); } // Create UserDTO UserDTO userDTO = new UserDTO() { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, Username = model.Username, Password = model.Password, University = model.University }; #region Password Hashing userDTO.Password = Crypto.Hash(userDTO.Password); #endregion // Add to DTO db.Users.Add(userDTO); // Save db.SaveChanges(); // Get inserted id int userId = userDTO.Id; // Login user FormsAuthentication.SetAuthCookie(model.Username, false); // Set uploads dir var uploadsDir = new DirectoryInfo(string.Format("{0}Uploads", Server.MapPath(@"\"))); // Check if a file was uploaded if (file != null && file.ContentLength > 0) { // Get extension string ext = file.ContentType.ToLower(); // Verify extension if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/x-png" && ext != "image/png") { ModelState.AddModelError("", "The image was not uploaded - wrong image extension."); return(View("Index", model)); } // Set image name string imageName = userId + ".jpg"; // Set image path var path = string.Format("{0}\\{1}", uploadsDir, imageName); // Save image file.SaveAs(path); } // Add to wall WallDTO wall = new WallDTO(); wall.Id = userId; wall.Message = ""; wall.DateEdited = DateTime.Now; db.Wall.Add(wall); db.SaveChanges(); // Redirect return(Redirect("~/" + model.Username)); }
public ActionResult Username(string username = "") { // Init db Db db = new Db(); // Check if user exists //if (!db.Users.Any(x => x.Username.Equals(username))) //{ // return Redirect("~/"); //} // ViewBag username ViewBag.Username = username; // Get logged in user's username string user = User.Identity.Name; // Viewbag user's full name UserDTO userDTO = db.Users.Where(x => x.Username.Equals(user)).FirstOrDefault(); ViewBag.FullName = userDTO.FirstName + " " + userDTO.LastName; // Get user's id int userId = userDTO.Id; // ViewBag user id ViewBag.UserId = userId; // Get viewing full name UserDTO userDTO2 = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); ViewBag.ViewingFullName = userDTO2.FirstName + " " + userDTO2.LastName; // Get username's image ViewBag.UsernameImage = userDTO2.Id + ".jpg"; // Viewbag user type string userType = "guest"; if (username.Equals(user)) { userType = "owner"; } ViewBag.UserType = userType; // Check if they are friends if (userType == "guest") { UserDTO u1 = db.Users.Where(x => x.Username.Equals(user)).FirstOrDefault(); int id1 = u1.Id; UserDTO u2 = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); int id2 = u2.Id; FriendDTO f1 = db.Friends.Where(x => x.User1 == id1 && x.User2 == id2).FirstOrDefault(); FriendDTO f2 = db.Friends.Where(x => x.User2 == id1 && x.User1 == id2).FirstOrDefault(); if (f1 == null && f2 == null) { ViewBag.NotFriends = "True"; } if (f1 != null) { if (!f1.Active) { ViewBag.NotFriends = "Pending"; } } if (f2 != null) { if (!f2.Active) { ViewBag.NotFriends = "Pending"; } } } // Viewbag request count var friendCount = db.Friends.Count(x => x.User2 == userId && x.Active == false); if (friendCount > 0) { ViewBag.FRCount = friendCount; } // Viewbag friend count UserDTO uDTO = db.Users.Where(x => x.Username.Equals(username)).FirstOrDefault(); int usernameId = uDTO.Id; var friendCount2 = db.Friends.Count(x => x.User2 == usernameId && x.Active == true || x.User1 == usernameId && x.Active == true); ViewBag.FCount = friendCount2; // Viewbag message count var messageCount = db.Messages.Count(x => x.To == userId && x.Read == false); ViewBag.MsgCount = messageCount; // Viewbag user wall WallDTO wall = new WallDTO(); ViewBag.WallMessage = db.Wall.Where(x => x.Id == userId).Select(x => x.Message).FirstOrDefault(); // Viewbag friend walls List <int> friendIds1 = db.Friends.Where(x => x.User1 == userId && x.Active == true).ToArray().Select(x => x.User2).ToList(); List <int> friendIds2 = db.Friends.Where(x => x.User2 == userId && x.Active == true).ToArray().Select(x => x.User1).ToList(); List <int> allFriendsIds = friendIds1.Concat(friendIds2).ToList(); List <WallVM> walls = db.Wall.Where(x => allFriendsIds.Contains(x.Id)).ToArray().OrderByDescending(x => x.DateEdited).Select(x => new WallVM(x)).ToList(); ViewBag.Walls = walls; // Return return(View()); }
public ActionResult Username(string userName = "") { //if (User.Identity.Name != userName) //{ // return Redirect("~/" + User.Identity.Name); //} ViewBag.Username = userName; var user = User.Identity.Name; //Init Db Db db = new Db(); //User Full Name UserDTO userDto = db.Users.Where(v => v.UserName.Equals(User.Identity.Name)).FirstOrDefault(); ViewBag.FullName = userDto.FirstName + " " + userDto.LastName; ViewBag.UserId = userDto.Id; var userId = userDto.Id; //Viewbag user walls List <int> friendIds1 = db.Friends.Where(v => v.User1 == userId && v.Active) .ToArray() .Select(x => x.User2) .ToList(); List <int> friendIds2 = db.Friends.Where(v => v.User2 == userId && v.Active) .ToArray() .Select(x => x.User1) .ToList(); List <int> allFriends = friendIds1.Concat(friendIds2).ToList(); List <WallVM> walls = db.Wall.Where(v => allFriends.Contains(v.Id)) .ToArray() .OrderByDescending(x => x.DateEdited) .Select(t => new WallVM(t)) .ToList(); ViewBag.Walls = walls; //Viewing User Full Name UserDTO userDto2 = db.Users.Where(v => v.UserName.Equals(userName)).FirstOrDefault(); ViewBag.ViewingFullName = userDto2.FirstName + " " + userDto2.LastName; var otherUserId = userDto2.Id; //Get Accepted Friend Count var FrCount = db.Friends.Count(v => v.User1 == otherUserId && v.Active == true || v.User2 == otherUserId && v.Active == true); ViewBag.FCount = FrCount; //Find Viewing User image ViewBag.UserImage = userDto2.Id + ".jpg"; //Count Message for the user var msgCount = db.Messages.Count(v => v.To == userId && v.Read == false); ViewBag.MsgCount = msgCount; //User type string userType = "guest"; //Check which user if (userName.Equals(user)) { userType = "owner"; } ViewBag.UserType = userType; //Check if they are friends if (userType == "guest") { int id1 = userDto.Id; int id2 = userDto2.Id; FriendDTO f1 = db.Friends.Where(v => v.User1 == id1 && v.User2 == id2).FirstOrDefault(); FriendDTO f2 = db.Friends.Where(v => v.User1 == id2 && v.User2 == id1).FirstOrDefault(); if (f1 == null && f2 == null) { ViewBag.NotFriends = "True"; } if (f1 != null) { if (!f1.Active) { ViewBag.NotFriends = "Pending"; } } if (f2 != null) { if (!f2.Active) { ViewBag.NotFriends = "Pending"; } } } //Wall message WallDTO wallDto = new WallDTO(); ViewBag.WallMessage = db.Wall.Where(v => v.Id == userId).Select(t => t.Message).FirstOrDefault(); //Get Friends count var friendCount = db.Friends.Count(v => v.User2 == userId && v.Active == false); if (friendCount > 0) { ViewBag.FRCount = friendCount; } return(View()); }
public ActionResult CreateAccount(UserVM model, HttpPostedFileBase file) { //Initialize the database var db = new Db(); // Check model state if (!ModelState.IsValid) { return(View("Index", model)); } //Ensure the proposed username is unique if (db.Users.Any(x => x.Username.Equals(model.Username))) { ModelState.AddModelError("", "Username " + model.Username + " is already in use."); model.Username = ""; return(View("Index", model)); } //Create the UserDTO var userDTO = new UserDTO() { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, Username = model.Username, //Password is stored as plain text. Add encryption soon. Password = model.Password }; //Add the new UserDTO to the DTO db.Users.Add(userDTO); //Save the new account information to the database db.SaveChanges(); //Get newly-generated ID var userId = userDTO.Id; //Login the new user FormsAuthentication.SetAuthCookie(model.Username, false); //Set uploads directory for the new user var imageUploadsDir = new DirectoryInfo(string.Format("{0}ImageUploads", Server.MapPath(@"\"))); //Check if image file was uploaded during creation if (file == null || file.ContentLength <= 0) { return(Redirect("~/" + model.Username)); } //Get the file extension of uploaded image var ext = file.ContentType.ToLower(); //Verify the extension as acceptable if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/pjpeg" && ext != "image/gif" && ext != "image/png" && ext != "image/x-png") { ModelState.AddModelError("", "The image was not uploaded, due to an unrecognized extension. Please upload a .jpg, .jpeg, .pjpeg, .png, .x-png, or .gif only."); return(View("Index", model)); } //Set the name of the uploaded image file var imageName = userId + ".jpg"; //Set the path for the image var path = string.Format("{0}\\{1}", imageUploadsDir, imageName); //Save the image file.SaveAs(path); //Add to wall var wall = new WallDTO(); wall.Id = userId; wall.Message = ""; wall.DateEdited = DateTime.Now; db.Wall.Add(wall); db.SaveChanges(); //Redirect return(Redirect("~/" + model.Username)); }
public ActionResult Username(string username = "") { //Initialize the database var db = new Db(); //Check if the User exists within the user database. if (!db.Users.Any(x => x.Username.Equals(username))) { return(Redirect("~/")); } //ViewBag Username ViewBag.Username = username; //Get the username of the user who is currently logged in var user = User.Identity.Name; //Viewbag user's full name var userDTO = db.Users.FirstOrDefault(x => x.Username.Equals(user)); ViewBag.FullName = userDTO.FirstName + " " + userDTO.LastName; // Get user's id var userId = userDTO.Id; // ViewBag user id ViewBag.UserId = userId; //Get viewing full name var userDTO2 = db.Users.FirstOrDefault(x => x.Username.Equals(username)); ViewBag.ViewingFullName = userDTO2.FirstName + " " + userDTO2.LastName; //Get User's Profile Picture ViewBag.ProfilePicture = userDTO2.Id + ".jpg"; //Viewbag user type var userType = "guest"; if (username.Equals(user)) { userType = "owner"; } ViewBag.UserType = userType; //Check if they are friends if (userType == "guest") { var u1 = db.Users.FirstOrDefault(x => x.Username.Equals(user)); var id1 = u1.Id; var u2 = db.Users.FirstOrDefault(x => x.Username.Equals(username)); var id2 = u2.Id; var f1 = db.Friends.FirstOrDefault(x => x.User1 == id1 && x.User2 == id2); var f2 = db.Friends.FirstOrDefault(x => x.User2 == id1 && x.User1 == id2); if (f1 == null && f2 == null) { ViewBag.NotFriends = "True"; } if (f1 != null) { if (!f1.Active) { ViewBag.NotFriends = "Pending"; } } if (f2 != null) { if (!f2.Active) { ViewBag.NotFriends = "Pending"; } } } //Get friend request count var friendCount = db.Friends.Count(x => x.User2 == userId && x.Active == false); if (friendCount > 0) { ViewBag.FRCount = friendCount; } //Get viewbag friend count var uDTO = db.Users.FirstOrDefault(x => x.Username.Equals(username)); var usernameId = uDTO.Id; var friendCount2 = db.Friends.Count(x => x.User2 == usernameId && x.Active == true || x.User1 == usernameId && x.Active == true); ViewBag.FCount = friendCount2; //Get viewbag message count var messageCount = db.Messages.Count(x => x.To == userId && x.Read == false); ViewBag.MsgCount = messageCount; //Get viewbag user wall var wall = new WallDTO(); ViewBag.WallMessage = db.Wall.Where(x => x.Id == userId).Select(x => x.Message).FirstOrDefault(); //ViewBag friend walls var friendIds1 = db.Friends.Where(x => x.User1 == userId && x.Active == true).ToArray() .Select(x => x.User2).ToList(); var friendIds2 = db.Friends.Where(x => x.User2 == userId && x.Active == true).ToArray() .Select(x => x.User1).ToList(); var allFriendIds = friendIds1.Concat(friendIds2).ToList(); var walls = db.Wall.Where(x => allFriendIds.Contains(x.Id)).ToArray().OrderByDescending(x => x.DateEdited).Select(x => new WallVM(x)).ToList(); ViewBag.Walls = walls; //Return return(View()); }
public WallVM(WallDTO wall) { Id = wall.Id; Message = wall.Message; DateEdited = wall.DateEdited; }