예제 #1
0
        public async Task <UpsertModel> UpsertExpense(UpsertMode mode, ExpenseViewModel model)
        {
            var upsert = new UpsertModel();

            try {
                Activity activity;
                string   title;
                System.Text.StringBuilder builder;

                // Record previous values for comparison
                double amount   = (Expense != null ? Expense.Amount : 0);
                string by       = (Expense != null ? Expense.By : string.Empty);
                string desc     = (Expense != null ? Expense.Description : string.Empty);
                int    category = (Expense != null ? Expense.Category : 0);
                int    changes  = 0;

                // Apply changes
                Expense = model.ParseAsEntity(Expense);

                builder = new System.Text.StringBuilder();

                if (model.ExpenseId == 0)
                {
                    db.Expenses.Add(Expense);

                    title = "Expense Created";
                    builder.Append(string.Format("A Expense record has been created for: {0}", model.By)).AppendLine();
                }
                else
                {
                    db.Entry(Expense).State = System.Data.Entity.EntityState.Modified;

                    title = "Expense Updated";
                    builder.Append("The following changes have been made to the Expense details");

                    if (mode == UpsertMode.Admin)
                    {
                        builder.Append(" (by the Admin)");
                    }

                    builder.Append(":").AppendLine();
                }

                if (amount != Expense.Amount)
                {
                    builder.AppendLine().AppendFormat("Amount: from '{0}' to '{1}'", amount, Expense.Amount);
                    changes += 1;
                }
                if (by != Expense.By)
                {
                    builder.AppendLine().AppendFormat("Expense By: from '{0}' to '{1}'", by, Expense.By);
                    changes += 1;
                }
                if (desc != Expense.Description)
                {
                    builder.AppendLine().AppendFormat("Description: from '{0}' to '{1}'", desc, Expense.Description);
                    changes += 1;
                }
                if (category != Expense.Category)
                {
                    var categoryValue = category == 0 ? "" : Expense.Categories()[category];
                    builder.AppendLine().AppendFormat("Category: from '{0}' to '{1}'", categoryValue, Expense.Categories()[model.Category]);
                    changes += 1;
                }

                await db.SaveChangesAsync();

                ExpenseId = Expense.ExpenseId;

                // Save activity now so we have a ExpenseId. Not ideal, but hey
                activity        = CreateActivity(title, builder.ToString());
                activity.UserId = ServiceUserId;

                await db.SaveChangesAsync();

                if (model.ExpenseId == 0)
                {
                    upsert.ErrorMsg = "Expense record created successfully";
                }
                else
                {
                    upsert.ErrorMsg = "Expense record updated successfully";
                }

                upsert.RecordId = Expense.ExpenseId.ToString();
            }
            catch (Exception ex) {
                upsert.ErrorMsg = ex.Message;
            }

            return(upsert);
        }