Exemplo n.º 1
0
        public bool DeleteRange(IEnumerable <int> ids)
        {
            if (!ids.Any())
            {
                return(true);
            }

            using (var context = new MoneyStatsContext())
            {
                var objs         = this.Get();
                var toDeleteList = objs.Where(x => ids.Contains(x.Id));

                if (toDeleteList.ToList().Count == 0)
                {
                    // TODO log or throw
                    return(false);
                }

                foreach (var toDeleteItem in toDeleteList)
                {
                    toDeleteItem.ModifiedDate = DateTime.Now;
                    toDeleteItem.State        = 0;
                }
                context.SaveChanges();
                return(true);
            }
        }
Exemplo n.º 2
0
 public TEntity Select(int id)
 {
     using (var context = new MoneyStatsContext())
     {
         return(context.Set <TEntity>().ToList().SingleOrDefault(x => x.Id == id && x.IsActive));
     }
 }
Exemplo n.º 3
0
        public bool UpdateMany(List <TEntity> entities)
        {
            using (var context = new MoneyStatsContext())
            {
                // Set each
                var i = 0;
                foreach (var entity in entities)
                {
                    var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == entity.Id);
                    if (obj != null)
                    {
                        // Update values
                        context.Entry(obj).CurrentValues.SetValues(entity);

                        // Update modified date
                        obj.ModifiedDate = DateTime.Now;

                        i++;
                    }
                }

                // Save changes
                if (i > 0)
                {
                    context.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
Exemplo n.º 4
0
 public IEnumerable <TEntity> ForceGet()
 {
     using (var context = new MoneyStatsContext())
     {
         return(context.Set <TEntity>().ToList());
     }
 }
Exemplo n.º 5
0
        public List <Rule> GetWithEntities()
        {
            using (var context = new MoneyStatsContext())
            {
                // Rules
                var rules = context.Rules.Where(rule => rule.State == 1).ToList();
                foreach (var rule in rules)
                {
                    // AndConditionGroups
                    rule.AndConditionGroups = context.AndConditionGroups.Where(x => x.RuleId == rule.Id).ToList();
                    foreach (var andConditionGroup in rule.AndConditionGroups)
                    {
                        // Conditions
                        andConditionGroup.Conditions = context.Conditions.Where(x => x.AndConditionGroupId == andConditionGroup.Id).ToList();
                    }

                    // RuleActions
                    rule.RuleActions = context.RuleActions.Where(x => x.RuleId == rule.Id).ToList();
                    foreach (var ruleAction in rule.RuleActions)
                    {
                        // [Tag]
                        if (ruleAction.TagId != null)
                        {
                            ruleAction.Tag = context.Tags.SingleOrDefault(x => x.Id == ruleAction.TagId);
                        }
                    }
                }

                return(rules);
            }
        }
Exemplo n.º 6
0
 public IEnumerable <TEntity> Get()
 {
     using (var context = new MoneyStatsContext())
     {
         // TODO "context.Set<TEntity>().ToList().Where(x => x.IsActive);" works, but why do we need it?
         return(context.Set <TEntity>().ToList().Where(x => x.IsActive));
     }
 }
Exemplo n.º 7
0
 public void Save(List <Currency> currencies)
 {
     using (var context = new MoneyStatsContext())
     {
         context.Currency.AddRange(currencies);
         context.SaveChanges();
     }
 }
Exemplo n.º 8
0
 public Dictionary <string, int> GetTitleKeyedDictionary()
 {
     using (var context = new MoneyStatsContext())
     {
         return((from d in context.Currency
                 select new { d.Name, d.Id }).ToDictionary(k => k.Name, v => v.Id));
     }
 }
Exemplo n.º 9
0
 public void Save(List <Tag> tags)
 {
     using (var context = new MoneyStatsContext())
     {
         context.Tag.AddRange(tags);
         context.SaveChanges();
     }
 }
Exemplo n.º 10
0
 public void Save(List <Transaction> transactions)
 {
     using (var context = new MoneyStatsContext())
     {
         context.Transaction.AddRange(transactions);
         context.SaveChanges();
     }
 }
Exemplo n.º 11
0
 public Dictionary <int, Tag> GetIdKeyedDictionary()
 {
     using (var context = new MoneyStatsContext())
     {
         return((from d in context.Tag
                 select new { d, d.Id }).ToDictionary(k => k.Id, v => v.d));
     }
 }
Exemplo n.º 12
0
 public void DeleteAll()
 {
     using (var context = new MoneyStatsContext())
     {
         context.Database.ExecuteSqlCommand("delete from dbo.[Transaction];DBCC CHECKIDENT ([Transaction], RESEED, 0);");
         context.SaveChanges();
     }
 }
Exemplo n.º 13
0
 public List <BankRow> GetOnIds(List <int> ids)
 {
     using (var context = new MoneyStatsContext())
     {
         return((from d in context.BankRows
                 where ids.Any(x => x == d.Id)
                 select d).ToList());
     }
 }
Exemplo n.º 14
0
        public TEntity Insert(TEntity entity)
        {
            using (var context = new MoneyStatsContext())
            {
                entity.SetNew();

                context.Set <TEntity>().Add(entity);
                context.SaveChanges();
                return(entity);
            }
        }
Exemplo n.º 15
0
 public List <Rule> GetOnIdsWithEntitiesInDepth(List <int> ids)
 {
     using (var context = new MoneyStatsContext())
     {
         return(context.Rules
                .Where(x => ids.Any(y => y == x.Id) && x.State == 1)
                .Include(x => x.RuleActions).ThenInclude(x => x.Tag)
                .Include(x => x.AndConditionGroups).ThenInclude(x => x.Conditions)
                .ToList());
     }
 }
Exemplo n.º 16
0
 public List <Currency> Get()
 {
     using (var context = new MoneyStatsContext())
     {
         return((from d in context.Currency
                 select new Currency()
         {
             Id = d.Id,
             Name = d.Name
         }).ToList());
     }
 }
Exemplo n.º 17
0
 public List <Tag> Get()
 {
     using (var context = new MoneyStatsContext())
     {
         return((from d in context.Tag
                 select new Tag()
         {
             Id = d.Id,
             Title = d.Title
         }).ToList());
     }
 }
Exemplo n.º 18
0
 /// <summary>
 /// Inserts all examples.
 /// Handles the order of which records can
 /// be inserted (foreign key dependencies).
 /// </summary>
 public void InsertAllExamples()
 {
     using (var db = new MoneyStatsContext())
     {
         this.AttachInsert(new TagRepository(), db);
         this.AttachInsert(new RuleRepository(), db);
         this.AttachInsert(new AndConditionGroupRepository(), db);
         this.AttachInsert(new ConditionRepository(), db);
         this.AttachInsert(new RuleActionRepository(), db);
         this.AttachInsert(new BankRowRepository(), db);
         this.AttachInsert(new TransactionRepository(), db);
         // ...
     }
     LineBreak();
 }
Exemplo n.º 19
0
        public List <BankRow> GetWithEntities()
        {
            using (var context = new MoneyStatsContext())
            {
                var list = (from d in context.BankRows.ToList()
                            join a in context.Transactions on d.GroupedTransactionId equals a.Id
                            where d.IsActive && d.GroupedTransactionId != null
                            select new BankRow()
                {
                    GroupedTransaction = a
                }).ToList();

                return(list);
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Deletes all records from every tables.
        /// Handles the order of which tables can
        /// be cleaned (foreign key dependencies).
        /// </summary>
        public void DeleteAllFromDatabase()
        {
            this.AreYouSure("You are about to DELETE all records! Do you want to continue?");

            using (var db = new MoneyStatsContext())
            {
                foreach (var tableName in TableDependencyOrder.List)
                {
                    db.Database.ExecuteSqlCommand("DELETE FROM [" + tableName + "]");
                    db.Database.ExecuteSqlCommand("DBCC CHECKIDENT([" + tableName + "], RESEED, 0)");

                    Console.WriteLine($"[{tableName}] DELETE and RESEED finished.");
                }
            }
            LineBreak();
        }
Exemplo n.º 21
0
        public void UpdateGroupedTransactionIds(List <BankRow> rows)
        {
            var list = rows.Where(x => x.GroupedTransaction != null).ToList();

            if (list.Count == 0)
            {
                return;
            }

            using (var context = new MoneyStatsContext())
            {
                // TODO this shouldn't work!
                list.ForEach(x => x.GroupedTransactionId = x.GroupedTransaction.Id);
                context.SaveChanges();
            }
        }
Exemplo n.º 22
0
        public bool Destroy(int id)
        {
            using (var context = new MoneyStatsContext())
            {
                var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == id);

                if (obj != null)
                {
                    context.Set <TEntity>().Remove(obj);
                    context.SaveChanges();
                    return(true);
                }

                // TODO log or throw
                return(false);
            }
        }
Exemplo n.º 23
0
        public bool Update(TEntity entity)
        {
            using (var context = new MoneyStatsContext())
            {
                var obj = context.Set <TEntity>().SingleOrDefault(x => x.Id == entity.Id);

                if (obj != null)
                {
                    context.Entry(obj).CurrentValues.SetValues(entity);
                    obj.ModifiedDate = DateTime.Now;
                    context.SaveChanges();
                    return(true);
                }

                // TODO log or throw
                return(false);
            }
        }
Exemplo n.º 24
0
 public List <TransactionTagConn> GetOnFilter(Expression <Func <TransactionTagConn, bool> > predicate)
 {
     using (var context = new MoneyStatsContext())
     {
         return(context.TransactionTagConn
                .Where(predicate)
                .Select(d => new TransactionTagConn()
         {
             Id = d.Id,
             TransactionId = d.TransactionId,
             TagId = d.TagId,
             Tag = d.Tag,
             Transaction = d.Transaction
         })
                .OrderBy(x => x.Transaction.AccountingDate)
                .ToList());
     }
 }
Exemplo n.º 25
0
        public bool Delete(int id)
        {
            using (var context = new MoneyStatsContext())
            {
                var obj = context.Set <TEntity>().ToList().SingleOrDefault(x => x.Id == id && x.IsActive);

                if (obj != null)
                {
                    obj.ModifiedDate = DateTime.Now;
                    obj.State        = 0;
                    context.SaveChanges();
                    return(true);
                }

                // TODO log or throw
                return(false);
            }
        }
Exemplo n.º 26
0
        void SaveNewRules(List <Rule> rules)
        {
            if (rules.Count == 0)
            {
                return;
            }

            using (var context = new MoneyStatsContext())
            {
                rules.ForEach(rule =>
                {
                    if (rule.Id > 0)
                    {
                        goto _continue;
                    }

                    // Add Rule
                    rule.SetAsNew();

                    // Add AndConditionGroups
                    rule.AndConditionGroups.ForEach(andConditionGroup =>
                    {
                        andConditionGroup.SetAsNew();
                        // Add Conditions
                        andConditionGroup.Conditions.ForEach(condition => condition.SetAsNew());
                    });

                    // Add RuleActions
                    rule.RuleActions.ForEach(action =>
                    {
                        action.SetAsNew();
                        action.TagId = action.Tag?.Id;
                        action.Tag   = null;
                    });

                    context.Rules.Add(rule);

                    _continue:;
                });

                context.SaveChanges();
            }
        }
Exemplo n.º 27
0
 public List <TransactionTagConn> GetWithEntities()
 {
     using (var context = new MoneyStatsContext())
     {
         var transactions = (
             from d in context.TransactionTagConn
             orderby d.TransactionId ascending
             select new TransactionTagConn()
         {
             Id = d.Id,
             TransactionId = d.TransactionId,
             TagId = d.TagId,
             Tag = d.Tag,
             Transaction = d.Transaction
         })
                            .ToList();
         return(transactions);
     }
 }
Exemplo n.º 28
0
        public bool DestroyRange(IEnumerable <int> ids)
        {
            using (var context = new MoneyStatsContext())
            {
                var objs = (from e in context.Set <TEntity>()
                            where ids.Contains(e.Id)
                            select e).ToList();

                if (objs.Count == 0)
                {
                    // TODO log or throw
                    return(false);
                }

                context.Set <TEntity>().RemoveRange(objs);
                context.SaveChanges();
                return(true);
            }
        }
Exemplo n.º 29
0
        public IEnumerable <TEntity> InsertRange(IEnumerable <TEntity> entities)
        {
            if (!entities.Any())
            {
                return(entities);
            }

            using (var context = new MoneyStatsContext())
            {
                foreach (var item in entities)
                {
                    item.SetNew();
                }

                context.Set <TEntity>().AddRange(entities);
                context.SaveChanges();
                return(entities);
            }
        }
Exemplo n.º 30
0
        public void DropAllTables()
        {
            this.AreYouSure("(!!!) You are about to DROP all tables! Do you want to continue?");

            using (var db = new MoneyStatsContext())
            {
                // Drop tables
                foreach (var tableName in TableDependencyOrder.List)
                {
                    db.Database.ExecuteSqlCommand("DROP TABLE [" + tableName + "];");
                    Console.WriteLine($"[{tableName}] was dropped.");
                }

                // Drop meta tables
                var migrationHistoryTableName = "__EFMigrationsHistory";
                db.Database.ExecuteSqlCommand("DROP TABLE [" + migrationHistoryTableName + "];");
                Console.WriteLine($"[{migrationHistoryTableName}] was dropped.");
            }
            LineBreak();
        }