Example #1
0
        public async Task <IActionResult> PutBudget(int id, Budget budget)
        {
            if (id != budget.Id)
            {
                return(BadRequest());
            }

            _context.Entry(budget).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BudgetExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> PutExpense(long id, Expense expense)
        {
            if (id != expense.Id)
            {
                return(BadRequest());
            }

            _context.Entry(expense).State = EntityState.Modified;

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

            return(NoContent());
        }
Example #3
0
        public async Task <Expense> Save(Expense expense)
        {
            var e = _context.Add(expense);
            await _context.SaveChangesAsync();

            return(e.Entity);
        }
        public async Task <ActionResult <Expense> > AddExpense(Expense expense)
        {
            _context.Expenses.Add(expense);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetExpense), new { id = expense.Id }, expense));
        }
Example #5
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));
        }
Example #6
0
        public async Task <IActionResult> PutExpense([FromRoute] string id, [FromQuery] bool approved)
        {
            var _expense = await _context.Expenses.AsNoTracking().Include("Employee").SingleOrDefaultAsync(_exp => _exp.UUID == id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_expense == null)
            {
                return(NotFound($"Expense not found with the UUID {id}"));
            }

            var newExpense = new Expense()
            {
                Description = _expense.Description,
                Amount      = _expense.Amount,
                Created_at  = _expense.Created_at,
                Approved    = approved,
                Currency    = _expense.Currency,
                Employee    = _expense.Employee,
                UUID        = id
            };

            try
            {
                _context.Entry(newExpense).Property(x => x.Approved).IsModified = true; //just update approved field
                await _context.SaveChangesAsync();
            }
            catch (Exception error)
            {
                return(BadRequest(error.Message));
            }


            await _context.Entry(newExpense).Reference(x => x.Employee).LoadAsync();

            var expenseDto = new ExpenseDTO()
            {
                ExpenseUUID  = newExpense.UUID,
                EmployeeUUID = newExpense.Employee.UUID,
                First_Name   = newExpense.Employee.First_name,
                Amount       = newExpense.Amount,
                Approved     = newExpense.Approved
            };

            return(CreatedAtAction("GetExpense", new { id = newExpense.UUID }, expenseDto));
        }
Example #7
0
        public async Task <bool> AddExpense(ExpenseDTO expense)
        {
            try
            {
                if (expense.Value > 0)
                {
                    var data = new Expense
                    {
                        Value         = expense.Value,
                        Reason        = expense.Reason,
                        DateOfExpense = expense.Date,
                        DateCreated   = DateTime.Now
                    };

                    data.VAT = ExtractVATFromValue(expense.Value);

                    _context.Expenses.Add(data);
                    await _context.SaveChangesAsync();

                    return(true);
                }
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #8
0
        public async Task <ActionResult <ExpenseItem> > PostExpenseItem(ExpenseItem item)
        {
            _context.ExpenseItems.Add(item);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetExpenseItem), new { id = item.Id }, item));
        }
Example #9
0
        public async Task <IActionResult> PostExpense([FromBody] Expense expense)
        {
            if (!ModelState.IsValid)
            {
                logger.LogError(this.getLoggerModelState(ModelState));

                return(BadRequest(ModelState));
            }

            // Find expense by primary keys
            var primaryExpense = await context.Expenses.FindAsync(expense.TransactionDate, expense.Recipient, expense.Currency, expense.ExpenseType);

            // Update amount if the expense is already added
            if (primaryExpense == null)
            {
                context.Expenses.Add(expense);
            }
            else
            {
                expense.Id     = primaryExpense.Id;
                expense.Amount = expense.Amount;
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (expenseExists(expense.Id))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetExpense", new { id = expense.Id }, expense));
        }
        public async Task <int> Register(User user, string password)
        {
            CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user.Id);
        }
        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) }));
        }
Example #12
0
        public async Task <int> Execute(SaveReservationCommand arguments)
        {
            var vendor = await ctx
                         .Vendors
                         .SingleOrDefaultAsync(x => x.Name == arguments.Reservation.Vendor.Name);

            if (vendor != null)
            {
                arguments.Reservation.Vendor = vendor;
            }

            ctx.Reservations.Add(arguments.Reservation);
            await ctx.SaveChangesAsync();

            return(arguments.Reservation.Id);
        }
 public async Task DeleteAsync(Expense expense)
 {
     _dbContext.Expenses.Remove(expense);
     await _dbContext.SaveChangesAsync();
 }
Example #14
0
 public async Task <bool> SaveAll()
 {
     return(await _context.SaveChangesAsync() > 0);
 }
 public Task <int> SaveChangesAsync()
 {
     return(_context.SaveChangesAsync());
 }