/// <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);
        }
示例#2
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);
        }
示例#3
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();
            }
        }