Ejemplo n.º 1
0
        public messageModel update(expenseModel value)
        {
            messageModel result = new messageModel();

            try
            {
                using (var context = new StandardCanEntities())
                {
                    if (String.IsNullOrEmpty(value.user_id))
                    {
                        throw new Exception("Unauthorized Access");
                    }
                    var userId = JwtHelper.GetUserIdFromToken(value.user_id);
                    if (String.IsNullOrEmpty(userId))
                    {
                        throw new Exception("Unauthorized Access");
                    }

                    int ret = context.sp_expense_update(value.id, value.code, value.name, userId);
                }

                result.status  = "S";
                result.message = "";
            }
            catch (Exception ex)
            {
                result.status  = "E";
                result.message = ex.Message.ToString();
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> CreateExpense([FromBody] expenseModel expense)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiGeneralResponse {
                    Status = false
                }));
            }

            try
            {
                var newExpense = await _ExpenseRepository.InsertExpenseAsync(expense);

                if (newExpense == null)
                {
                    return(BadRequest(new ApiGeneralResponse {
                        Status = false
                    }));
                }
                return(CreatedAtRoute("GetExpenseRoute", new { id = newExpense.expenseID },
                                      new ApiGeneralResponse {
                    Status = true, id = newExpense.expenseID
                }));
            }
            catch (Exception exp)
            {
                _Logger.LogError(exp.Message);
                return(BadRequest(new ApiGeneralResponse {
                    Status = false
                }));
            }
        }
Ejemplo n.º 3
0
        public messageModel insert(expenseModel value)
        {
            messageModel result = new messageModel();

            try
            {
                System.Data.Entity.Core.Objects.ObjectParameter myOutputParamInt = new System.Data.Entity.Core.Objects.ObjectParameter("r_id", typeof(Int32));
                using (var context = new StandardCanEntities())
                {
                    if (String.IsNullOrEmpty(value.user_id))
                    {
                        throw new Exception("Unauthorized Access");
                    }
                    var userId = JwtHelper.GetUserIdFromToken(value.user_id);
                    if (String.IsNullOrEmpty(userId))
                    {
                        throw new Exception("Unauthorized Access");
                    }

                    int ret = context.sp_expense_insert(value.code, value.name, userId, myOutputParamInt);
                }

                if (myOutputParamInt.Value != null)
                {
                    int r_id = Convert.ToInt32(myOutputParamInt.Value);
                    result.status  = "S";
                    result.message = "";
                    result.value   = r_id.ToString();
                }
                else
                {
                    result.status  = "E";
                    result.message = "";
                }
            }
            catch (Exception ex)
            {
                result.status  = "E";
                result.message = ex.Message.ToString();
            }

            return(result);
        }
Ejemplo n.º 4
0
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody] expenseModel value)
        {
            if (value == null)
            {
                return(null);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();

            expenseService      service  = new expenseService();
            HttpResponseMessage response = null;
            Object result = null;

            switch (value.method)
            {
            case "search":
                result = service.search(value);
                break;

            case "insert":
                result = service.insert(value);
                break;

            case "update":
                result = service.update(value);
                break;

            case "delete":
                result = service.delete(value);
                break;

            default:
                break;
            }


            string json = js.Serialize(result);

            response         = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
            return(response);
        }
Ejemplo n.º 5
0
 public IEnumerable <sp_expense_search_Result> search(expenseModel value)
 {
     try
     {
         if (String.IsNullOrEmpty(value.user_id))
         {
             throw new Exception("Unauthorized Access");
         }
         var userId = JwtHelper.GetUserIdFromToken(value.user_id);
         if (String.IsNullOrEmpty(userId))
         {
             throw new Exception("Unauthorized Access");
         }
         StandardCanEntities context = new StandardCanEntities();
         IEnumerable <sp_expense_search_Result> result = context.sp_expense_search(value.name).AsEnumerable();
         return(result);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Ejemplo n.º 6
0
        public async Task <expense> InsertExpenseAsync(expenseModel expense)
        {
            expense newExpense = new expense();

            newExpense.expenseName = expense.expenseName;
            newExpense.created_by  = expense.created_by;
            newExpense.created_at  = expense.created_at;
            newExpense.groupID     = expense.groupID;
            _Context.expense.Add(newExpense);

            foreach (var person in expense.payer)
            {
                user_expense user_Expense = new user_expense();
                user_Expense.expenseID       = newExpense.expenseID;
                user_Expense.user_expense_id = person.ID;
                user_Expense.paid_share      = person.amount;
                _Context.user_expense.Add(user_Expense);
            }

            foreach (var person in expense.commonmember)
            {
                expense_member member = new expense_member();
                member.expenseID      = newExpense.expenseID;
                member.commonmemberID = person.ID;
                member.payableAmount  = person.amount;
                _Context.expense_member.Add(member);
            }

            foreach (var data in expense.paymentModels)
            {
                if (data.groupID == null)
                {
                    var settle = await _Context.payment.SingleOrDefaultAsync(c => c.payerID == data.paymentID && c.commonmemberID == data.commonmemberID && c.groupID == null);

                    if (settle != null)
                    {
                        settle.payment_amount = settle.payment_amount + data.payment_amount;
                        _Context.payment.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.payment.SingleOrDefaultAsync(c => c.payerID == data.commonmemberID && c.commonmemberID == data.paymentID && c.groupID == null);

                        if (setle != null)
                        {
                            setle.payment_amount = setle.payment_amount - data.payment_amount;
                            _Context.payment.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new payment();
                            newSettle.payerID        = data.paymentID;
                            newSettle.commonmemberID = data.commonmemberID;
                            newSettle.payment_amount = data.payment_amount;
                            _Context.payment.Add(newSettle);
                        }
                    }
                }
                else
                {
                    var settle = await _Context.payment.SingleOrDefaultAsync(c => c.payerID == data.paymentID && c.commonmemberID == data.commonmemberID && c.groupID == data.groupID);

                    if (settle != null)
                    {
                        settle.payment_amount = settle.payment_amount + data.payment_amount;
                        _Context.payment.Attach(settle);
                    }
                    else
                    {
                        var setle = await _Context.payment.SingleOrDefaultAsync(c => c.payerID == data.commonmemberID && c.commonmemberID == data.paymentID && c.groupID == data.groupID);

                        if (setle != null)
                        {
                            setle.payment_amount = setle.payment_amount - data.payment_amount;
                            _Context.payment.Attach(setle);
                        }
                        else
                        {
                            var newSettle = new payment();
                            newSettle.payerID        = data.paymentID;
                            newSettle.commonmemberID = data.commonmemberID;
                            newSettle.payment_amount = data.payment_amount;
                            newSettle.groupID        = data.groupID;
                            _Context.payment.Add(newSettle);
                        }
                    }
                }
            }
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertExpenseAsync)}: " + exp.Message);
            }
            return(newExpense);
        }