Exemplo n.º 1
0
        /// <summary>
        /// Get Expense by id
        /// </summary>
        /// <param name="id">Expense id</param>
        /// <returns>Expense json view model</returns>
        public IHttpActionResult Get(int id)
        {
            try
            {
                // get
                log.Debug("_expenseService.GetExpense - expenseId: " + id + " ");

                var expense = new ExpenseViewModel(_expenseService.GetExpense(id));

                log.Debug("_expenseService.GetExpense - " + ExpenseViewModel.FormatExpenseViewModel(expense));

                log.Debug("result: 'success'");

                //return Json(expense, JsonRequestBehavior.AllowGet);
                //return Content(JsonConvert.SerializeObject(expense), "application/json");
                //return expense;
                //return JsonConvert.SerializeObject(expense);
                return(Ok(expense));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 2
0
        private ExpenseDTO Create(ExpenseViewModel viewModel)
        {
            try
            {
                log.Debug(ExpenseViewModel.FormatExpenseViewModel(viewModel));

                ExpenseDTO expense = new ExpenseDTO();

                // copy values
                viewModel.UpdateDTO(expense, null); //RequestContext.Principal.Identity.GetUserId());

                // audit
                expense.CreateBy = null; //RequestContext.Principal.Identity.GetUserId();
                expense.CreateOn = DateTime.UtcNow;

                // add
                log.Debug("_expenseService.AddExpense - " + ExpenseDTO.FormatExpenseDTO(expense));

                int id = _expenseService.AddExpense(expense);

                expense.ExpenseId = id;

                log.Debug("result: 'success', id: " + id);

                return(expense);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 3
0
        private ExpenseDTO Update(ExpenseViewModel viewModel)
        {
            try
            {
                log.Debug(ExpenseViewModel.FormatExpenseViewModel(viewModel));

                // get
                log.Debug("_expenseService.GetExpense - expenseId: " + viewModel.ExpenseId + " ");

                var existingExpense = _expenseService.GetExpense(viewModel.ExpenseId);

                log.Debug("_expenseService.GetExpense - " + ExpenseDTO.FormatExpenseDTO(existingExpense));

                if (existingExpense != null)
                {
                    // copy values
                    viewModel.UpdateDTO(existingExpense, null); //RequestContext.Principal.Identity.GetUserId());

                    // update
                    log.Debug("_expenseService.UpdateExpense - " + ExpenseDTO.FormatExpenseDTO(existingExpense));

                    _expenseService.UpdateExpense(existingExpense);

                    log.Debug("result: 'success'");
                }
                else
                {
                    log.Error("existingExpense: null, ExpenseId: " + viewModel.ExpenseId);
                }

                return(existingExpense);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 4
0
        //[ValidateAntiForgeryToken]
        /// <summary>
        /// Save a list of Expense
        /// </summary>
        /// <param name="viewModels">Expense view models</param>
        /// <param name="id">(not used)</param>
        /// <returns>true if the operation is successfull</returns>
        public IHttpActionResult SaveList(ExpenseViewModel[] viewModels, int?id)
        {
            try
            {
                log.Debug("SaveList");

                if (viewModels != null)
                {
                    // save list
                    foreach (ExpenseViewModel viewModel in viewModels)
                    {
                        log.Debug(ExpenseViewModel.FormatExpenseViewModel(viewModel));

                        if (viewModel.ExpenseId > 0)
                        {
                            var t = Update(viewModel);
                        }
                        else
                        {
                            var t = Create(viewModel);
                        }
                    }
                }
                else
                {
                    log.Error("viewModels: null");
                }

                //return Json(true);
                //return JsonConvert.SerializeObject(true);
                return(Ok(true));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }