public String ValidateDebt(DebtViewModel debtAttribute) { String Message = String.Empty; Decimal number; bool TestValue = Decimal.TryParse(debtAttribute.Value, out number); if (!TestValue) { Message = String.Concat(Message, "Campo Valor deve ser decimal /n "); } DateTime date; bool TestDate = DateTime.TryParse(debtAttribute.Date, out date); if (!TestDate) { Message = String.Concat(Message, "Campo Date deve ser no formato dd/mm/aaaa /n "); } if (debtAttribute.FriendNameIn == debtAttribute.FriendNameOut) { Message = String.Concat(Message, "O devedor deve ser diferente do amigo devido. /n "); } return(Message); }
public HttpResponseMessage UpdateDebt([FromBody] DebtViewModel debtAttribute, [FromUri] int debtId) { Validates validates = new Validates(); MessageViewModel messageAttribute = new MessageViewModel(); String Message = validates.ValidateUpdateDebt(debtAttribute, debtId); if (Message == String.Empty) { Debt debt = new Debt(); FriendRepository friendName = new FriendRepository(); Boolean sucess; if (debtRepository == null) { DebtRepository debtRepository = new DebtRepository(); debt = debtRepository.GetDebtById(debtId); if (debt != null) { debtAttribute.Id = debtId; debt.Value = Convert.ToDecimal(debtAttribute.Value); debt.Date = Convert.ToDateTime(debtAttribute.Date); debt.Description = debtAttribute.Description; debt.FriendIdIn = friendName.GetFriendIdByName(debtAttribute.FriendNameIn); debt.FriendIdOut = friendName.GetFriendIdByName(debtAttribute.FriendNameOut); sucess = debtRepository.Update(debt, debtId); if (sucess) { return(Request.CreateResponse(HttpStatusCode.OK, debtAttribute)); } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not update debt.")); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not find debt.")); } } else { debt = debtRepository.GetDebtById(debtId); sucess = debtRepository.Update(debt, debtId); if (sucess) { return(Request.CreateResponse(HttpStatusCode.OK, debt)); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not find debt.")); } else { return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, Message)); } }
public String ValidateUpdateDebt(DebtViewModel debtAttribute, int debtId) { String Message = String.Empty; String testId = debtId.ToString(); if (testId == "") { Message = "Id deve estar preenchido. /n "; } if (testId.Length <= 0) { Message = String.Concat(Message, "Id deve ser maior que 0."); } int number = 0; bool TestId = Int32.TryParse(testId, out number); if (!TestId) { Message = String.Concat(Message, "Campo Id deve ser inteiro /n "); } else if (Message == "") { if (debtAttribute == null) { Message = String.Concat(Message, "Não pode ser enviado um débito em branco. /n "); } if (debtAttribute.Value.Length <= 0) { Message = String.Concat(Message, "Campo valor não pode ser nulo. /n "); } DateTime date; bool TestDate = DateTime.TryParse(debtAttribute.Date, out date); if (!TestDate) { Message = String.Concat(Message, "Campo Date deve ser no formato dd/mm/aaaa /n "); } if (debtAttribute.FriendNameIn == debtAttribute.FriendNameOut) { Message = String.Concat(Message, "O devedor deve ser diferente do amigo devido. /n "); } Decimal value = 0; bool TestValue = Decimal.TryParse(debtAttribute.Value, out value); if (!TestValue) { Message = String.Concat(Message, "Campo valor deve ser inteiro /n "); } } return(Message); }
public HttpResponseMessage CreateDebt([FromBody] DebtViewModel debtAttribute) { Validates validates = new Validates(); MessageViewModel messageAttribute = new MessageViewModel(); var Message = validates.ValidateDebt(debtAttribute); if (Message == String.Empty) { Debt debt = new Debt(); Boolean sucess; if (debtRepository == null) { DebtRepository debtRepository = new DebtRepository(); debt.Value = Convert.ToDecimal(debtAttribute.Value); debt.Date = Convert.ToDateTime(debtAttribute.Date); debt.Description = debtAttribute.Description; FriendRepository friendName = new FriendRepository(); debt.FriendIdIn = friendName.GetFriendIdByName(debtAttribute.FriendNameIn); debt.FriendIdOut = friendName.GetFriendIdByName(debtAttribute.FriendNameOut); sucess = debtRepository.Insert(debt); if (sucess) { return(Request.CreateResponse(HttpStatusCode.Created, messageAttribute.Create())); } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not create debit.")); } else { sucess = debtRepository.Insert(debt); if (sucess) { return(Request.CreateResponse(HttpStatusCode.Created, messageAttribute.Create())); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not create debit.")); } return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, Message)); }
public HttpResponseMessage GetDebtById(int debtId) { Debt getDebt = new Debt(); if (debtRepository == null) { DebtRepository debtRepository = new DebtRepository(); getDebt = debtRepository.GetDebtById(debtId); if (getDebt != null) { DebtViewModel debt = new DebtViewModel(); FriendRepository friendName = new FriendRepository(); debt.Id = getDebt.Id; debt.Value = getDebt.Value.ToString(); debt.Date = Convert.ToString(getDebt.Date); debt.Description = getDebt.Description; debt.FriendNameIn = friendName.GetFriendNameById(getDebt.FriendIdIn).Trim(); debt.FriendNameOut = friendName.GetFriendNameById(getDebt.FriendIdOut).Trim(); return(Request.CreateResponse(HttpStatusCode.OK, debt)); } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not find debt.")); } else { getDebt = debtRepository.GetDebtById(debtId); if (getDebt != null) { return(Request.CreateResponse(HttpStatusCode.OK, getDebt)); } } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not find debt.")); }