Example #1
0
        public ActionResult <User> Create([FromBody] Operation operation)
        {
            _finContext.Add(operation);
            _finContext.SaveChanges();

            User    user = _finContext.Users.Single(x => x.Id == operation.UserId);
            decimal sum  = _finContext.Operations.Where(x => x.UserId == operation.UserId).Sum(x => x.Price);

            user.AllMoney          = sum;
            operation.CurrentMoney = sum;
            _finContext.Update(operation);
            _finContext.SaveChanges();


            var v = _finContext.Operations.Where(u => u.UserId == user.Id).ToArray();

            user.Operation = v;

            _finContext.Update(user);


            _finContext.SaveChanges();
            //IQueryable<User> users = _finContext.Users.FromSqlRaw("exec UpdateData @UserId", 1);
            return(user);
        }
        public IActionResult RegisterUser(RegisterUser newuser)
        {
            User CheckEmail = _context.Users
                              .SingleOrDefault(u => u.Email == newuser.Email);

            if (CheckEmail != null)
            {
                ViewBag.errors = "That email already exists";
                return(RedirectToAction("Register", "Home"));
            }
            if (ModelState.IsValid)
            {
                PasswordHasher <RegisterUser> Hasher = new PasswordHasher <RegisterUser>();
                User newUser = new User
                {
                    UserId    = newuser.UserId,
                    FirstName = newuser.FirstName,
                    LastName  = newuser.LastName,
                    Email     = newuser.Email,
                    Password  = Hasher.HashPassword(newuser, newuser.Password)
                };
                _context.Add(newUser);
                _context.SaveChanges();
                ViewBag.success = "Successfully registered";
                return(RedirectToAction("Login", "Home"));
            }
            else
            {
                return(View("Register"));
            }
        }
Example #3
0
        public IActionResult Create(CreateAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                Account account = new Account
                {
                    UserGuid      = userManager.GetUserId(User),
                    BankName      = model.BankName,
                    AccountName   = model.AccountName,
                    AccountNumber = model.AccountNumber,
                    CardNumber    = model.CardNumber,
                    Credit        = Convert.ToInt64(model.Credit)
                };

                context.Account.Add(account);

                if (Convert.ToBoolean(context.SaveChanges()))
                {
                    TempData["ToasterState"]   = ToasterState.Success;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.CreateAccountSuccessful;
                }
                else
                {
                    TempData["ToasterState"]   = ToasterState.Error;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.CreateAccountFailed;
                }

                return(RedirectToAction("Index"));
            }

            return(BadRequest());
        }
Example #4
0
 public ActionResult <User> Create([FromBody] User user)
 {
     if (_finContext.Users.Count(x => x.Username == user.Username) == 0)
     {
         _finContext.Add(user);
         _finContext.SaveChanges();
         return(user);
     }
     return(NotFound());
 }
        public int CreateNewExpense(Expense e)
        {
            using (var db = new FinancialContext())
            {
                int    result = 0;
                string type   = "placeholder",
                       amount = "placeholder";

                type   = e.ExpenseType;
                amount = e.ExpenseAmount;

                var expense = new Expense {
                    ExpenseType = type, ExpenseAmount = amount
                };
                db.Expenses.Add(expense);

                result = db.SaveChanges();

                if (false)
                {
                    //handle unexpected result
                }
                else
                {
                    return(result);
                }
            }
        }
Example #6
0
 public ActionResult <Group> Create([FromBody] Group group)
 {
     if (_finContext.Groups.Count(x => x.Name == group.Name) == 0)
     {
         _finContext.Add(group);
         _finContext.SaveChanges();
         return(group);
     }
     return(NotFound());
 }
        public PredictionTO Add(PredictionTO Entity)
        {
            if (Entity is null)
            {
                throw new ArgumentNullException(nameof(Entity));
            }

            var prediction = Entity.ToEF();
            var result     = financialContext.Predictions.Add(prediction);

            financialContext.SaveChanges();
            return(result.Entity.ToTransferObject());
        }
        public EffectiveTO Add(EffectiveTO Entity)
        {
            if (Entity is null)
            {
                throw new ArgumentNullException(nameof(Entity));
            }

            var effective = Entity.ToEF();
            var result    = financialContext.Effectives.Add(effective);

            financialContext.SaveChanges();
            return(result.Entity.ToTransferObject());
        }
Example #9
0
        public IActionResult CreateAccountTransaction(CreateAccountTransactionViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.AccountGuid = Constants.DefaultAccountGuid;
                long cost = Convert.ToInt64(model.Cost);

                Transaction transaction = new Transaction
                {
                    AccountGuid   = model.AccountGuid,
                    TypeCodeGuid  = model.TypeGuid,
                    StateCodeGuid = model.StateGuid,
                    Title         = model.Title,
                    Cost          = cost,
                    AccountSide   = model.AccountSide,
                    Description   = model.Description,
                    ReceiptDate   = PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate)
                };

                if (model.AccountGuid.HasValue)
                {
                    if (model.StateGuid == Codes.PassedState)
                    {
                        var account = context.Account
                                      .Where(x => x.AccountGuid == model.AccountGuid)
                                      .SingleOrDefault();

                        if (account == null)
                        {
                            TempData["ToasterState"]   = ToasterState.Error;
                            TempData["ToasterType"]    = ToasterType.Message;
                            TempData["ToasterMessage"] = Messages.CreateTransactionFailedAccountNotValid;

                            return(RedirectToAction("Index"));
                        }

                        account.Credit       = model.TypeGuid == Codes.CreditorType ? account.Credit + cost : account.Credit - cost;
                        account.ModifiedDate = DateTime.Now;

                        var transactionBefore = context.Transaction
                                                .Where(x => !x.IsDelete && x.ReceiptDate < PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate))
                                                .OrderByDescending(x => x.ReceiptDate)
                                                .FirstOrDefault();

                        if (transactionBefore == null)
                        {
                            transaction.Credit = model.TypeGuid == Codes.CreditorType ? transaction.Credit + cost : transaction.Credit - cost;
                        }
                        else
                        {
                            transaction.Credit = model.TypeGuid == Codes.CreditorType ? transactionBefore.Credit + cost : transactionBefore.Credit - cost;
                        }

                        var transactionsAfter = context.Transaction
                                                .Where(x => !x.IsDelete && x.ReceiptDate > PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate))
                                                .OrderBy(x => x.ReceiptDate)
                                                .ToList();

                        if (transactionsAfter.Count > 0)
                        {
                            transactionsAfter[0].Credit = transactionsAfter[0].StateCodeGuid == Codes.PassedState ?
                                                          (transactionsAfter[0].TypeCodeGuid == Codes.CreditorType ? transaction.Credit + transactionsAfter[0].Cost : transaction.Credit - transactionsAfter[0].Cost) :
                                                          (transactionsAfter[0].Credit = transaction.Credit);

                            for (int i = 1; i < transactionsAfter.Count; i++)
                            {
                                transactionsAfter[i].Credit = transactionsAfter[i].StateCodeGuid == Codes.PassedState ?
                                                              (transactionsAfter[i].TypeCodeGuid == Codes.CreditorType ? transactionsAfter[i - 1].Credit + transactionsAfter[i].Cost : transactionsAfter[i - 1].Credit - transactionsAfter[i].Cost) :
                                                              (transactionsAfter[i].Credit = transactionsAfter[i - 1].Credit);
                            }
                        }
                    }
                    else
                    {
                        var transactionBefore = context.Transaction
                                                .Where(x => !x.IsDelete && x.ReceiptDate < PersianDateExtensionMethods.ToGeorgianDateTime(model.ReceiptDate))
                                                .OrderByDescending(x => x.ReceiptDate)
                                                .FirstOrDefault();

                        transaction.Credit = transactionBefore == null ? 0 : transactionBefore.Credit;
                    }
                }

                context.Transaction.Add(transaction);

                if (Convert.ToBoolean(context.SaveChanges() > 0))
                {
                    TempData["ToasterState"]   = ToasterState.Success;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.CreateTransactionSuccessful;
                }
                else
                {
                    TempData["ToasterState"]   = ToasterState.Error;
                    TempData["ToasterType"]    = ToasterType.Message;
                    TempData["ToasterMessage"] = Messages.CreateTransactionFailed;
                }

                return(RedirectToAction("Index"));
            }

            return(BadRequest());
        }
Example #10
0
 public static void InitializeDbForTests(FinancialContext db)
 {
     db.Customers.AddRange(GetSeedingCustomers());
     db.SaveChanges();
 }