コード例 #1
0
        public async Task <Expense> Save(Expense expense)
        {
            var e = _context.Add(expense);
            await _context.SaveChangesAsync();

            return(e.Entity);
        }
コード例 #2
0
 public Expense CreateExpense(Expense expense)
 {
     _expenseContext.Add(expense);
     if (_expenseContext.SaveChanges() > 0)
     {
         return(expense);
     }
     return(null);
 }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("ID,Title,Date,Type,Price")] Expense expense)
        {
            if (ModelState.IsValid)
            {
                _context.Add(expense);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(expense));
        }
コード例 #4
0
 public ActionResult Create(Expense expense)
 {
     if (expense.AttchFile != null)
     {
         using (var memoryStream = new MemoryStream())
         {
             expense.AttchFile.CopyTo(memoryStream);
             expense.Attachment = memoryStream.ToArray();
             expense.FileName   = expense.AttchFile.FileName;
         }
     }
     _context.Add(expense);
     _context.SaveChanges();
     return(RedirectToAction("Index"));
 }
コード例 #5
0
        public async void GetExpenses_ReturnsTwoItems_WhenDBIsNotEmpty()
        {
            //Arrange
            //DBContext
            var optionsBuilder = new DbContextOptionsBuilder <ExpenseContext>();

            optionsBuilder.UseInMemoryDatabase("UnitTestInMemBD2");
            var dbContext = new ExpenseContext(optionsBuilder.Options);

            dbContext.Add(new Expense()
            {
                Amount      = 88.49f,
                Currency    = Currency.EUR,
                Description = "Skateboard trucks"
            });

            dbContext.Add(new Expense()
            {
                Amount      = 49.99f,
                Currency    = Currency.EUR,
                Description = "Skateboard deck"
            });

            dbContext.SaveChanges();

            var expenseRepo = new ExpenseRepository(dbContext);

            //Controller
            var controller = new ExpensesController(expenseRepo);

            //Act
            var result = await controller.GetExpenses();

            //Assert
            Assert.Equal(2, result.Value.Count);
        }
コード例 #6
0
        public async Task <IActionResult> AddOrEdit(int id, [Bind("id,type,amount,date,catalouge")] Expense expense)
        {
            if (ModelState.IsValid)
            {
                expense.UserId = HttpContext.Session.GetString("UserId");
                if (id == 0)
                {
                    expense.date = DateTime.Now;

                    _context.Add(expense);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    try
                    {
                        _context.Update(expense);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ExpenseExists(expense.id))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.ExpenseList.Where(x => x.UserId == HttpContext.Session.GetString("UserId")).ToList()) }));
            }
            return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", expense) }));
        }
コード例 #7
0
 public void Post([FromBody] Expense expense)
 {
     _context.Add(expense);
     _context.SaveChanges();
 }