Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo changes done by last database save

When I press a button, jQuery uses ajax to delete an item, but it is possible for me to make another function that can "undo" the deletion that just took place?

My delete controller

    [HttpPost]
    public void DeleteProject(int Id)
    {
        var Item = from a in db.Project
                   where a.ProjectId.Equals(Id)
                   select a;

        Project SelectedProject = Item.FirstOrDefault();
        ICollection<ProjectProfile> Profiles = SelectedProject.ProjectProfile;
        ICollection<Member> Members = SelectedProject.Member;

        db.Member.RemoveRange(Members);
        db.ProjectProfile.RemoveRange(Profiles);

        db.Project.Remove(SelectedProject);
        db.SaveChanges();

        RedirectToAction("Index", "Projects");
    }
like image 327
Chris Hansen Avatar asked May 04 '15 19:05

Chris Hansen


1 Answers

There's a few way you could do this. Overall, it's a bit too broad to offer a clear-cut solution, but here's some ideas:

  • Instead of deleting immediately, you could create a task that will run in a certain amount of time that will do the delete. Say, 10 seconds. For 10 seconds, you show an "undo" button that will cancel that task from running the SaveChanges().
  • You could cache the item you're deleting in a temporary table and restore it
  • You could add an IsDeleted bit to your table and use that to signify that the record has been deleted which can be flipped at will to "un-delete".
like image 199
DLeh Avatar answered Oct 01 '22 16:10

DLeh