public IHttpActionResult Put(int id, [FromBody] ExpenseGroupDTO expensegroup) { try { if (expensegroup == null) { return(BadRequest()); } var eg = ExpenseGroupFactory.CreateExpenseGroupEntity(expensegroup); var result = _repo.UpdateExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Updated) { var updatedExpenseGroup = ExpenseGroupFactory.CreateExpenseGroupDTO(result.Entity); return(Ok(updatedExpenseGroup)); } else if (result.Status == RepositoryActionStatus.NotFound) { return(NotFound()); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public IHttpActionResult Post([FromBody] ExpenseGroupDTO expensegroup) { try { if (expensegroup == null) { return(BadRequest()); } else { //no reverse mapping with AutoMap var eg = ExpenseGroupFactory.CreateExpenseGroupEntity(expensegroup); var result = _repo.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { var newExpenseGroup = ExpenseGroupFactory.CreateExpenseGroupDTO(result.Entity); return(Created <ExpenseGroupDTO>(Request.RequestUri + "/" + newExpenseGroup.Id.ToString(), newExpenseGroup)); } return(BadRequest()); } } catch (Exception) { return(InternalServerError()); } }
public static ExpenseGroup CreateExpenseGroupEntity(ExpenseGroupDTO expenseGroup) { return(new ExpenseGroup() { Description = expenseGroup.Description, ExpenseGroupStatusId = expenseGroup.ExpenseGroupStatusId, Id = expenseGroup.Id, UserId = expenseGroup.UserId, Title = expenseGroup.Title, Expenses = expenseGroup.Expenses == null ? new List <Expense>() : expenseGroup.Expenses.Select(e => ExpenseFactory.CreateExpenseEntity(e)).ToList() }); }
public static object CreateDataShapedObjectExpenseGroup(ExpenseGroupDTO expenseGroup, List <string> lstOfFields) { // work with a new instance, as we'll manipulate this list in this method List <string> lstOfFieldsToWorkWith = new List <string>(lstOfFields); if (!lstOfFieldsToWorkWith.Any()) { return(expenseGroup); } else { // does it include any expense-related field? var lstOfExpenseFields = lstOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); // if one of those fields is "expenses", we need to ensure the FULL expense is returned. If // it's only subfields, only those subfields have to be returned. bool returnPartialExpense = lstOfExpenseFields.Any() && !lstOfExpenseFields.Contains("expenses"); // if we don't want to return the full expense, we need to know which fields if (returnPartialExpense) { // remove all expense-related fields from the list of fields, // as we will use the CreateDateShapedObject function in ExpenseFactory // for that. lstOfFieldsToWorkWith.RemoveRange(lstOfExpenseFields); lstOfExpenseFields = lstOfExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { // we shouldn't return a partial expense, but the consumer might still have // asked for a subfield together with the main field, ie: expense,expense.id. We // need to remove those subfields in that case. lstOfExpenseFields.Remove("expenses"); lstOfFieldsToWorkWith.RemoveRange(lstOfExpenseFields); } // create a new ExpandoObject & dynamically create the properties for this object // if we have an expense ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in lstOfFieldsToWorkWith) { // need to include public and instance, b/c specifying a binding flag overwrites the // already-existing binding flags. var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); // add the field to the ExpandoObject ((IDictionary <String, Object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpense) { // add a list of expenses, and in that, add all those expenses List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(ExpenseFactory.CreateDataShapedObject(expense, lstOfExpenseFields)); } ((IDictionary <String, Object>)objectToReturn).Add("expenses", expenses); } return(objectToReturn); } }