public void Delete(int recordId, string removerEmail) { using (IdeasContext context = new IdeasContext()) { Record record = context.Records.Include(r => r.Author).First(r => r.Id == recordId); User remover = context.Users.First(u => u.Email == removerEmail); User author = record.Author; if ((author.Equals(remover) && remover.Type.Equals(UserType.Admin)) || (author.Equals(remover) && record.Status.Equals(RecordStatus.RemovedByAdmin)) || (remover.Type.Equals(UserType.Admin) && record.Status.Equals(RecordStatus.RemovedByUser))) { context.Records.Remove(record); context.SaveChanges(); return; } if (remover.Type.Equals(UserType.User)) { record.Status = RecordStatus.RemovedByUser; } else { record.Status = RecordStatus.RemovedByAdmin; } context.SaveChanges(); } }
public IHttpActionResult PutIdea(int id, Idea idea) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != idea.Id) { return(BadRequest()); } db.Entry(idea).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!IdeaExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IActionResult Post(string idea) { User UserChecker = _context.Users.SingleOrDefault(x => x.Id == HttpContext.Session.GetInt32("UserId")); Idea NewIdea = new Idea { Content = idea, User = UserChecker, CreatedAt = DateTime.Now }; _context.Ideas.Add(NewIdea); _context.SaveChanges(); return(RedirectToAction("ideaboard")); }
public IActionResult Register(RegViewModel model) { if (HttpContext.Session.GetInt32("UserId") != null) { return(RedirectToAction("Home", "Ideas")); } if (ModelState.IsValid) { User ExistingUser = _context.Users.SingleOrDefault(u => u.Email == model.Email); if (ExistingUser != null) { ViewBag.Message = "Email is already in use."; return(View("Index", model)); } User NewUser = new User { Name = model.Name, Alias = model.Alias, Email = model.Email, Password = model.Password }; PasswordHasher <User> Hasher = new PasswordHasher <User>(); NewUser.Password = Hasher.HashPassword(NewUser, model.Password); _context.Add(NewUser); _context.SaveChanges(); int UserId = _context.Users.Last().UserId; HttpContext.Session.SetInt32("UserId", UserId); return(RedirectToAction("Home", "Ideas")); } return(View("Index", model)); }
public IActionResult Register(UserViewModel user) { System.Console.WriteLine("############# in register controller"); System.Console.WriteLine("############# " + ModelState.IsValid); System.Console.WriteLine("############# " + user.Password + " should equal " + user.PasswordConfirm); User AliasChecker = _context.Users.SingleOrDefault(x => x.Alias == user.Alias); if (AliasChecker != null) { ViewBag.AliasError = "User already exists."; } else if (ModelState.IsValid && AliasChecker == null) { User newUser = new User { FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, Alias = user.Alias, Password = user.Password, CreatedAt = DateTime.Now }; _context.Users.Add(newUser); _context.SaveChanges(); User UserChecker = _context.Users.SingleOrDefault(x => x.Email == newUser.Email); HttpContext.Session.SetInt32("LoggedIn", 1); HttpContext.Session.SetInt32("UserId", UserChecker.Id); System.Console.WriteLine("############ Redirecting to idea board..."); return(RedirectToAction("ideaboard", "Ideas")); } return(View("register")); }
public void Save(User user) { using (IdeasContext context = new IdeasContext()) { context.Users.Add(user); context.SaveChanges(); } }
public void RestoreRecord(int recordId) { using (IdeasContext context = new IdeasContext()) { Record record = context.Records.First(r => r.Id == recordId); record.Status = RecordStatus.Normal; context.SaveChanges(); } }
public void ConfirmDeletion(int recordId) { using (IdeasContext context = new IdeasContext()) { Record record = context.Records.First(r => r.Id == recordId); context.Records.Remove(record); context.SaveChanges(); } }
public void Update(Record record) { using (IdeasContext context = new IdeasContext()) { record.UpdateDate = DateTime.Now; context.Entry(record).State = EntityState.Modified; context.SaveChanges(); } }
public void Save(Record record, string authorEmail) { using (IdeasContext context = new IdeasContext()) { User user = context.Users.FirstOrDefault(u => u.Email == authorEmail); user.Records.Add(record); context.SaveChanges(); } }
public IActionResult AddIdea(string desc) { Idea idea = new Idea { Description = desc == null ? "" : desc.Trim(), UserId = (int)HttpContext.Session.GetInt32("userId") }; if (TryValidateModel(idea)) { _context.Ideas.Add(idea); _context.SaveChanges(); } else { TempData["error"] = "Description must be at least 5 characters."; } return(RedirectToAction("Dashboard")); }
public async Task <IActionResult> Delete(int id) { User user = (User)RouteData.Values["User"]; Idea idea = await _context .Idea .Where(i => i.UserId == user.Id) .Where(i => !i.Approved) .FirstOrDefaultAsync(i => i.Id == id); if (idea != null) { _context.Idea.Remove(idea); _context.SaveChanges(); } return(RedirectToAction(nameof(MyIdeas))); }
public IHttpActionResult PutIdea(int id, Idea idea) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != idea.Id) { return(BadRequest()); } //Se asigna este operadopr para evitar que otros usuarios //puedan modificar los post de otros usuarios //obtenemos usuario var userId = User.Identity.GetUserId(); if (userId != idea.UserId) { return(StatusCode(HttpStatusCode.Conflict)); } db.Entry(idea).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!IdeaExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IActionResult AddNew(IdeaViewModel model) { if (!LoggedIn()) { return(RedirectToAction("Index", "Home")); } else { if (ModelState.IsValid) { Idea NewIdea = new Idea { Description = model.Description, UserId = (int)HttpContext.Session.GetInt32("UserId") }; _context.Add(NewIdea); _context.SaveChanges(); return(RedirectToAction("Home")); } ViewBag.User = _context.Users.Single(u => u.UserId == HttpContext.Session.GetInt32("UserId")); return(View("Home", model)); } }
public async Task <IActionResult> Delete(int id, string returnUrl) { User user = (User)RouteData.Values["User"]; Comment comment = await _context .Comment .Where(i => i.UserId == user.Id) .FirstOrDefaultAsync(i => i.Id == id); if (comment != null) { _context.Comment.Remove(comment); _context.SaveChanges(); } return(Redirect(returnUrl)); }
public IActionResult Register(RegisterViewModel rvm) { if (ModelState.IsValid) { //check for existing user with name/alias List <User> existingEm = _context.Users.Where(u => u.Email == rvm.Email).ToList(); List <User> existingAl = _context.Users.Where(u => u.Alias == rvm.Alias).ToList(); //if email or alias is not unique if (existingEm.Count > 0 || existingAl.Count > 0) { if (existingEm.Count > 0) { ModelState.AddModelError("UniqueEmail", "That email has already been registered"); } if (existingAl.Count > 0) { ModelState.AddModelError("UniqueAlias", "That alias has already been registered"); } } //add the user to the db else { User newU = new User { Name = rvm.Name, Alias = rvm.Alias, Email = rvm.Email, Password = rvm.Password }; _context.Users.Add(newU); _context.SaveChanges(); //set session variables HttpContext.Session.SetInt32("userId", newU.UserId); HttpContext.Session.SetString("alias", newU.Alias); return(RedirectToAction("Dashboard", "Idea")); } } return(View("Index")); }
public void Save() { db.SaveChanges(); }