public IHttpActionResult Put(int id, [FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null || expenseGroup.Id == 0 || id == 0) { return(BadRequest()); } var result = _repository.Update(_expenseGroupFactory.CreateExpenseGroup(expenseGroup)); if (result.Status == RepositoryActionStatus.NotFound) { return(NotFound()); } if (result.Status == RepositoryActionStatus.Updated) { return(Ok(_expenseGroupFactory.CreateExpenseGroup(result.Entity))); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public IHttpActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } // map the DTO to an entity we can use in our repository var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); // try to insert the entity, returns a status code and the entity created var result = _repository.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { // map to dto var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); return(Created <DTO.ExpenseGroup>(Request.RequestUri + "/" + newExpenseGroup.Id.ToString(), newExpenseGroup)); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
/// <summary> /// Test http put: /// /// HEADER /// ------ /// User-Agent: Fiddler /// Accept: application/json /// Host: localhost:53204 /// Content-Length: 171 /// Content-Type: application/json /// /// CONTENT /// ------- /// { /// "id" : 15, /// "userid" : "https://expsensetrackeridsrv3/embedded_1", /// "title" : "New ExpenseGroup updated", /// "description" : "ExpenseGroup description", /// "expenseGroupStatusId" : 1 /// } /// /// </summary> public IHttpActionResult Put(int id, [FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } var group = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.UpdateExpenseGroup(group); switch (result.Status) { case RepositoryActionStatus.Updated: var updatedExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); return(Ok(updatedExpenseGroup)); case RepositoryActionStatus.NotFound: return(NotFound()); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public IHttpActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } var group = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.InsertExpenseGroup(group); if (result.Status != RepositoryActionStatus.Created) { return(BadRequest()); } var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); var uriNewExpenseGroup = string.Format("{0}/{1}", Request.RequestUri, newExpenseGroup.Id); return(Created(uriNewExpenseGroup, newExpenseGroup)); } catch (Exception) { return(InternalServerError()); } }
public IHttpActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) //create a new { try { if (expenseGroup == null) { return(BadRequest()); //http-404 } // try mapping & saving var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { // map to dto var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup (result.Entity); return(Created(Request.RequestUri + "/" + newExpenseGroup.Id.ToString() , newExpenseGroup)); //return http-status-201 } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
private async void SetNewExpenseGroup(DTO.ExpenseGroup expenseGroup) { // a new expense group has been clicked. Set the expense group, and // load the expenses for this expense group. ExpenseGroup = expenseGroup; // load expenses for group Expenses.Clear(); var client = ExpenseTrackerHttpClient.GetClient(); HttpResponseMessage response = await client.GetAsync("api/expensegroups/" + ExpenseGroup.Id + "/expenses?fields=id,date,description,amount"); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); var lstExpenses = JsonConvert.DeserializeObject <IEnumerable <Expense> >(content); Expenses = new ObservableCollection <Expense>(lstExpenses); } else { // something went wrong, log this, handle this, show message, ... } }
// POST public IHttpActionResult POST([FromBody] DTO.ExpenseGroup expenseGroup) { try { //Check if null if (expenseGroup == null) { return(BadRequest()); } // Map DTO to entity to be used: var eG = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repo.InsertExpenseGroup(eG); if (result.Status == RepositoryActionStatus.Created) { var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); return(Created(Request.RequestUri + "/" + newExpenseGroup.Id, newExpenseGroup)); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public IHttpActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup (result.Entity); return(Created(Request.RequestUri + "/" + newExpenseGroup.Id.ToString() , newExpenseGroup)); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public IActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repo.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); return(Created(Request.PathBase + "/" + newExpenseGroup.Id.ToString(), expenseGroup)); } return(BadRequest()); } catch (Exception) { return(StatusCode(500)); } }
public IHttpActionResult Post([FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(this.BadRequest(Request.Headers.AcceptEncoding.ToString())); } var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.InsertExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Created) { var newExpenseGroup = _expenseGroupFactory.CreateExpenseGroup(result.Entity); //location of newly created resource return(Created(Request.RequestUri + "/" + newExpenseGroup.Id, newExpenseGroup)); } return(this.BadRequest()); } catch (Exception) { return(this.InternalServerError()); } }
public IHttpActionResult Put(int id, [FromBody] DTO.ExpenseGroup expenseGroup) { try { if (expenseGroup == null) { return(BadRequest()); } // map var eg = _expenseGroupFactory.CreateExpenseGroup(expenseGroup); var result = _repository.UpdateExpenseGroup(eg); if (result.Status == RepositoryActionStatus.Updated) { // map to dto var updatedExpenseGroup = _expenseGroupFactory .CreateExpenseGroup(result.Entity); return(Ok(updatedExpenseGroup)); } else if (result.Status == RepositoryActionStatus.NotFound) { return(NotFound()); } return(BadRequest()); } catch (Exception) { return(InternalServerError()); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List<string> listOfFields) { var listOfFieldsToWorkWith = new List<string>(listOfFields); if (!listOfFieldsToWorkWith.Any()) return expenseGroup; //does it include any expense-related field? List<string> listOfExpenseFields = listOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); //if one of those fields is "expenses", we need to ensure FULL expense is returned. If //it's only subfields, only those subfields have to be returned. bool returnPartialExpense = listOfExpenseFields.Any() && !listOfExpenseFields.Contains("expenses"); if (returnPartialExpense) { //remove all expense-related fields from the list of fields, //as we will use the CreateDataShapeObject function in the ExpenseFactory for that listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); listOfExpenseFields = listOfExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { //we shouldnt 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. listOfExpenseFields.Remove("expenses"); listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); } //create a new ExpandoObject & dynamically create the properties for this object //if we have an expense ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in listOfFieldsToWorkWith) { //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); ((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, listOfExpenseFields)); } ((IDictionary<string, object>)objectToReturn).Add("expenses", expenses); } return objectToReturn; }
public IEntity CreateEntityFromDTO(IDTOEntity entity) { DTO.ExpenseGroup expenseGroup = (DTO.ExpenseGroup)entity; return(new Entities.ExpenseGroup() { Description = expenseGroup.Description, IsActive = true, Title = expenseGroup.Title }); }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> fieldList) { List <string> listOfFieldsToWorkWith = new List <string>(fieldList); if (!listOfFieldsToWorkWith.Any()) { return(expenseGroup); } else { var listOfExpenseFields = listOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); var returnPartialExpenses = listOfExpenseFields.Any() && !listOfExpenseFields.Contains("expenses"); if (returnPartialExpenses) { listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); // removes expense-related fields from the original list of fields listOfExpenseFields = listOfExpenseFields.Select(f => f.Substring(f.IndexOf(".", StringComparison.Ordinal) + 1)) .ToList(); } else { listOfExpenseFields.Remove("expenses"); listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); } ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in listOfFieldsToWorkWith) { var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <string, object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpenses) { List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, listOfExpenseFields)); } ((IDictionary <string, object>)objectToReturn).Add("expenses", expenses); } return(objectToReturn); } }
public ExpenseGroup CreateExpenseGroup(DTO.ExpenseGroup expenseGroup) { return(new ExpenseGroup() { Description = expenseGroup.Description, ExpenseGroupStatusId = expenseGroup.ExpenseGroupStatusId, Id = expenseGroup.Id, Title = expenseGroup.Title, UserId = expenseGroup.UserId, Expenses = expenseGroup.Expenses == null ? new List <Expense>() : expenseGroup.Expenses.Select(e => expenseFactory.CreateExpense(e)).ToList() }); }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> fieldList) { List <string> workingFieldList = new List <string>(fieldList); if (!workingFieldList.Any()) { return(expenseGroup); } else { var expenseFieldList = workingFieldList.Where(f => f.ToLower().Contains("expense")).ToList(); bool returnPartialExpense = expenseFieldList.Any() && !expenseFieldList.Contains("expenses"); if (returnPartialExpense) { workingFieldList.RemoveRange(expenseFieldList); expenseFieldList = expenseFieldList.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { expenseFieldList.Remove("expenses"); workingFieldList.RemoveRange(expenseFieldList); } ExpandoObject shapedExpenseGroup = new ExpandoObject(); foreach (var field in workingFieldList) { var fieldValue = expenseGroup .GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <String, Object>)shapedExpenseGroup).Add(field, fieldValue); } if (returnPartialExpense) { List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, expenseFieldList)); } ((IDictionary <String, Object>)shapedExpenseGroup).Add("expenses", expenses); } return(shapedExpenseGroup); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> lstOfFields) { List <string> fieldsToWorkWithList = new List <string>(lstOfFields); if (!fieldsToWorkWithList.Any()) { return(expenseGroup); } else { var lstOfExpenseFields = fieldsToWorkWithList.Where(f => f.Contains("expenses")).ToList(); bool returnPartialExpense = lstOfExpenseFields.Any() && !lstOfExpenseFields.Contains("expenses"); if (returnPartialExpense) { fieldsToWorkWithList.RemoveRange(lstOfExpenseFields); lstOfExpenseFields = lstOfExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { lstOfExpenseFields.Remove("expenses"); fieldsToWorkWithList.RemoveRange(lstOfExpenseFields); } ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in fieldsToWorkWithList) { var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <string, object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpense) { 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); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> lstOfFields) { // work with a new instance, as we'll manipulate this list in this method var 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. var 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 subfields together with the main field, ie: expense,expense.id. // 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 var 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 var 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); } }