public async Task<Expense> Save(string description, double cost, IEnumerable<User> splitees) { Expense expense = new Expense(); expense.Cost = cost.ToString(); expense.Description = description; expense.Users = new List<Payment>(); // Add your friends to the payment foreach (User user in splitees) { Payment payment = new Payment(); payment.OwedShare = Math.Round((cost / (splitees.Count() + 1)), 2).ToString(); // Split the cost evenly (+1 because we need to count the current user) payment.PaidShare = "0.0"; payment.User = user; expense.Users.Add(payment); } // Add you double remainingShare = cost - expense.Users.Select(payment => double.Parse(payment.OwedShare)).Aggregate((sum, next) => sum + next); expense.Users.Add(new Payment() { OwedShare = remainingShare.ToString(), PaidShare = expense.Cost, User = proxy.CurrentUser }); Expense savedExpense = await proxy.CreateExpense(expense); return savedExpense; }
public async Task<Expense> CreateExpense(Expense expense) { var request = new RestRequest("create_expense", Method.POST); JObject payload = new JObject(); payload["cost"] = expense.Cost; payload["payment"] = false; payload["description"] = expense.Description; for (int i = 0; i < expense.Users.Count; i++) { Payment payment = expense.Users[i]; payload.AddIfNotDefault("users__" + i + "__user_id", payment.User.Id); payload.AddIfNotDefault("users__" + i + "__fist_name", payment.User.FirstName); payload.AddIfNotDefault("users__" + i + "__last_name", payment.User.LastName); payload.AddIfNotDefault("users__" + i + "__email", payment.User.Email); payload.AddIfNotDefault("users__" + i + "__paid_share", payment.PaidShare); payload.AddIfNotDefault("users__" + i + "__owed_share", payment.OwedShare); } var str = payload.ToString(); request.AddParameter("application/json", str, ParameterType.RequestBody); ExpenseWrapper result = await this.client.ExecuteRequestAsync<ExpenseWrapper>(request); if (result.Expenses.Count == 1) { // Success return result.Expenses[0]; } return null; }