/// <summary>
        /// Gets the largest id used in the database.
        /// TODO: Make this more performant (hits DB twice).
        /// </summary>
        /// <returns>The largest id or 0.</returns>
        public int GetLatestId()
        {
            using (var db = new Model.BudgetContext())
            {
                if (!db.Transactions.Any())
                {
                    return(0);
                }

                return(db.Transactions.Select(t => t.Id).Max());
            }
        }
        /// <summary>
        /// Adds a new transaction to the database.
        /// </summary>
        /// <param name="transaction">The transaction to add.</param>
        /// <returns>The added transaction.</returns>
        public Transaction AddTransaction(Transaction transaction)
        {
            if (transaction == null)
            {
                throw new System.ArgumentNullException(nameof(transaction));
            }

            using (var db = new Model.BudgetContext())
            {
                db.Transactions.Add(Create(transaction));

                db.SaveChanges();
            }

            return(transaction);
        }
Exemple #3
0
        /// <summary>
        /// Adds a plan to the database.
        /// </summary>
        /// <param name="plan">The plan.</param>
        /// <returns>The plan that was added.</returns>
        public Plan AddPlan(Plan plan)
        {
            if (plan == null)
            {
                throw new ArgumentNullException(nameof(plan));
            }

            using (var db = new Model.BudgetContext())
            {
                db.Plans.Add(Create(plan));

                db.SaveChanges();
            }

            return(plan);
        }
Exemple #4
0
        /// <summary>
        /// Updates a plan in the database.
        /// TODO: Examine performance (are includes needed here?).
        /// </summary>
        /// <param name="plan">The plan to update.</param>
        public void UpdatePlan(Plan plan)
        {
            if (plan == null)
            {
                throw new ArgumentNullException(nameof(plan));
            }

            var dbPlan = Create(plan);

            using (var db = new Model.BudgetContext())
            {
                var old = db.Plans.SingleOrDefault(o => o.Id == plan.Id);
                if (old == null)
                {
                    throw new ArgumentException("The plan was not found.");
                }

                old.Income = Create(plan.Income);
                old.Goals  = Create(plan.Goals).ToList();

                db.SaveChanges();
            }
        }