Exemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            HistoryBAL booksLogic = new HistoryBAL();

            BooksHistory.DataSource = booksLogic.GetData();
            BooksHistory.DataBind();
        }
Exemplo n.º 2
0
        public int Redone(int current, DateTime time)
        {
            int step = current;

            try
            {
                step--;

                List <BooksHistory> history;

                using (LibContext context = new LibContext())
                {
                    history = context.BooksHistory.Where(c => c.OperationDate <= time).ToList();
                    history.Reverse();
                    GenericRepository <Books> generic = new GenericRepository <Books>(context);

                    if (step < history.Count && step >= 0)
                    {
                        BooksHistory pacient   = history[step];
                        string       operation = pacient.Operation;

                        context.Database.ExecuteSqlCommand("DISABLE TRIGGER BooksHistory ON Books");
                        context.Database.ExecuteSqlCommand("DISABLE TRIGGER BooksInsert ON Books");

                        if (operation == "inserted")
                        {
                            Books entity = new Books
                            {
                                Id         = pacient.Id,
                                Title      = pacient.CurrentTitle,
                                Author     = pacient.CurrentAuthor.HasValue ? pacient.CurrentAuthor.Value : 0,
                                Department = pacient.CurrentDepartment
                            };

                            using (var scope = context.Database.BeginTransaction())
                            {
                                context.Books.Add(entity);
                                context.SaveChanges();
                                scope.Commit();
                            }
                        }
                        else if (operation == "updated")
                        {
                            Books entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault();
                            if (entity != null)
                            {
                                entity.Title      = pacient.CurrentTitle;
                                entity.Author     = pacient.CurrentAuthor.HasValue ? pacient.CurrentAuthor.Value : 0;
                                entity.Department = pacient.CurrentDepartment;

                                generic.Update(entity);
                            }
                        }
                        else if (operation == "deleted")
                        {
                            Books entity = generic.Get(c => c.Id == pacient.Id).FirstOrDefault();
                            if (entity != null)
                            {
                                generic.Remove(entity);
                            }
                        }

                        context.Database.ExecuteSqlCommand("ENABLE TRIGGER BooksHistory ON Books");
                        context.Database.ExecuteSqlCommand("ENABLE TRIGGER BooksInsert ON Books");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return(step);
        }