public IHttpActionResult UpdateBudget(BudgetModel budgetModel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (budgetModel.BudgetId == 0) { return BadRequest(); } var billDbs = db.Bills.Where(o => o.BudgetId == budgetModel.BudgetId).ToList(); var budgetDb = db.Budgets.Where(o => o.BudgetId == budgetModel.BudgetId).FirstOrDefault(); db.Bills.RemoveRange(billDbs); db.Budgets.Remove(budgetDb); List<Bill> bills = new List<Bill>(); foreach (var bill in budgetModel.Bills) { bills.Add(new Bill() { AmountPaid = bill.AmountPaid, BillId = bill.BillId, BillStatus = bill.BillStatus, BudgetId = bill.BudgetId, CustomBillId = bill.CustomBillId, PaymentDate = bill.PaymentDate, PaymentTypeId = bill.PaymentTypeId }); } var budget = new Budget() { BudgetId = budgetModel.BudgetId, DateReceived = budgetModel.DateReceived, ReceivedAmount = budgetModel.ReceivedAmount, UserId = budgetModel.UserId, Bills = bills }; db.Budgets.Add(budget); db.Bills.AddRange(bills); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!BudgetExists(budgetModel.BudgetId)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostBudget(BudgetModel budgetModel) { if (!ModelState.IsValid) { return NotFound(); } List<Bill> bills = new List<Bill>(); foreach (var billModel in budgetModel.Bills) { var bill = new Bill() { AmountPaid = billModel.AmountPaid, BillId = billModel.BillId, BillStatus = billModel.BillStatus, CustomBillId = billModel.CustomBillId, PaymentTypeId = billModel.PaymentTypeId, PaymentDate = billModel.PaymentDate }; bills.Add(bill); } var budget = new Budget() { BudgetId = budgetModel.BudgetId, UserId = budgetModel.UserId, DateReceived = budgetModel.DateReceived, ReceivedAmount = budgetModel.ReceivedAmount, Bills = bills }; db.Budgets.Add(budget); db.SaveChanges(); var budgetData = GetBudget(budget.UserId); return Ok(budgetData); }