public void Update(CardDetails cardDetails) { var card = _dbContext.Cards.SingleOrDefault(x => x.Id == cardDetails.Id); card.Contents = cardDetails.Contents; card.Notes = cardDetails.Notes; card.ColumnId = cardDetails.Column; _dbContext.SaveChanges(); }
public InsertPostModel InsertPost(string text, IFormFile file) { try { string fileName = null; if (file != null) { string uploadsFolder = Path.Combine(_environment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + file.FileName; string filePath = Path.Combine(uploadsFolder, fileName); file.CopyTo(new FileStream(filePath, FileMode.Create)); } Post obj = new Post() { Text = text, PhotoPath = fileName, UserId = UserId, Time = DateTime.Now }; _dbcontext.Post.Add(obj); _dbcontext.SaveChanges(); var user = _dbcontext.User.Single(c => c.Id == obj.UserId); InsertPostModel model = new InsertPostModel() { Id = obj.Id, NameSurname = user.Name + " " + user.Surname, PPPath = user.PhotoPath, Time = obj.Time, Text = obj.Text, PhotoPath = obj.PhotoPath, LikeCount = _dbcontext.LikeDislike.Where(c => c.PostId == obj.Id && c.LikeOrDislike).Count(), DislikeCount = _dbcontext.LikeDislike.Where(c => c.PostId == obj.Id && !c.LikeOrDislike).Count(), CommentCount = obj.Comments.Count }; return(model); } catch (Exception e) { return(null); } }
public bool Friends(int targetId, int status) { try { if (status == 2) { _dbcontext.Friend.Add(new Model.Friend() { FromUserId = UserId, ToUserId = targetId, Status = status }); } else if (status == 1 || status == 0) { var obj = _dbcontext.Friend.Single(c => c.ToUserId == UserId && c.FromUserId == targetId && c.Status == 2); obj.Status = status; } else if (status == -1) { var obj = _dbcontext.Friend.Single(c => (c.ToUserId == UserId && c.FromUserId == targetId) || (c.ToUserId == targetId && c.FromUserId == UserId)); _dbcontext.Friend.Remove(obj); } _dbcontext.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
public IActionResult Register(RegisterModel registerModel) { try { if (_dbcontext.User.Where(c => c.Username == registerModel.Username).Count() == 0) { _dbcontext.User.Add(new Model.User() { Username = registerModel.Username, Password = registerModel.Password, Email = registerModel.Email, Name = registerModel.Name, Surname = registerModel.Surname, PhotoPath = "default-pp.jpg" }); _dbcontext.SaveChanges(); } return(RedirectToAction("Login", "Account")); } catch (Exception e) { return(null); } }
public void InsertMessage(string text, int targetId) { try { var message = new Message() { Text = text, FromUserId = UserId, ToUserId = targetId, Time = DateTime.Now }; _dbcontext.Add(message); _dbcontext.SaveChanges(); } catch (Exception e) { } }
public int SaveChanges() { try { return(context.SaveChanges()); } catch (Exception) { throw; } }
public IActionResult UpdateSettings(SettingsModel model) { try { string fileName = ""; var user = _dbcontext.User.Single(c => c.Id == UserId); if (model.Username != null && _dbcontext.User.Where(c => c.Username == model.Username).Count() == 0) { user.Username = model.Username; } if (model.Name != null) { user.Name = model.Name; } if (model.Surname != null) { user.Surname = model.Surname; } if (model.Password != null) { user.Password = model.Password; } if (model.Email != null) { user.Email = model.Email; } if (model.Photo != null) { string uploadsFolder = Path.Combine(_environment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, fileName); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); user.PhotoPath = fileName; } user.Location = model.Location; user.Bio = model.Bio; _dbcontext.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception e) { return(null); } }
public void AddCard(AddCard viewModel) { var board = _dbContext.Boards .Include(b => b.Columns) .SingleOrDefault(x => x.Id == viewModel.Id); if (board != null) { var firstColumn = board.Columns.FirstOrDefault(); var secondColumn = board.Columns.FirstOrDefault(); var thirdColumn = board.Columns.FirstOrDefault(); if (firstColumn == null || secondColumn == null || thirdColumn == null) { firstColumn = new Models.Column { Title = "Todo" }; secondColumn = new Models.Column { Title = "Doing" }; thirdColumn = new Models.Column { Title = "Done" }; board.Columns.Add(firstColumn); board.Columns.Add(secondColumn); board.Columns.Add(thirdColumn); } firstColumn.Cards.Add(new Models.Card { Contents = viewModel.Contents }); } _dbContext.SaveChanges(); }
public ActionResult CreateAccount(UserViewModel model, HttpPostedFileBase imgUpload) { //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("UserNameAlreadyTaken", $"Sorry, the username: {model.UserName} is already taken."); model.UserName = ""; return(View("Index", model)); } //Create user var user = new UserEntity { Id = model.Id, FirstName = model.FirstName, LastName = model.LastName, UserName = model.UserName, EmailAddress = model.EmailAddress, Password = model.Password }; //Add user to database _db.Users.Add(user); //Save database _db.SaveChanges(); //Get inserted id var userId = user.Id; //Index user and set cookie for username FormsAuthentication.SetAuthCookie(model.UserName, false); //Make uploads directory var uploadsDir = $"{Server.MapPath(@"\")}Uploads"; //Check if profile picture was uploaded, if not redirect to dashboard if (imgUpload == null || imgUpload.ContentLength <= 0) { return(Redirect("~/" + model.UserName)); } ; //If yes get the file extension var type = imgUpload.ContentType.ToLower(); //Verify the file extension if (type != "image/jpg" && type != "image/jpeg" && type != "image/pjpeg" && type != "image/x-png" && type != "image/png" && type != "image/gif") { ModelState.AddModelError("InvalidProfilePicture", "Could not upload profile picture. Please make sure the file is an image!"); return(View("Index", model)); } //Set name for the profile picture var imageName = userId + ".jpg"; //Set path for the image var path = $"{uploadsDir}\\{imageName}"; //Save image imgUpload.SaveAs(path); //then redirect to dashboard return(Redirect("~/" + model.UserName)); }
public void Save() { context.SaveChanges(); }