public async Task <ActionResult <RepeatableExpense> > PostRepeatableExpense(RepeatableExpenseInputModel repeatableExpenseInput)
        {
            var user = await _context.Users.FindAsync(GetUserId());

            var repeatableExpense = new RepeatableExpense
            {
                ExpiryDate         = repeatableExpenseInput.ExpiryDate,
                daysToTrigger      = repeatableExpenseInput.FirstOccurence.HasValue ? (repeatableExpenseInput.FirstOccurence.GetValueOrDefault() - DateTime.Now).Days : repeatableExpenseInput.TimeToRepeatInDays,
                timeToRepeatInDays = repeatableExpenseInput.TimeToRepeatInDays,
                ExpenseData        = new ExpenseData
                {
                    Amount      = repeatableExpenseInput.Amount,
                    Description = repeatableExpenseInput.Description
                },
                User = user
            };

            _context.RepeatableExpenses.Add(repeatableExpense);
            await _context.SaveChangesAsync();

            if (!repeatableExpenseInput.FirstOccurence.HasValue)
            {
                var currentExpense = new CurrentExpense
                {
                    Date        = DateTime.Now,
                    User        = user,
                    ExpenseData = repeatableExpense.ExpenseData
                };
                _context.CurrentExpenses.Add(currentExpense);
                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction("GetRepeatableExpense", new { id = repeatableExpense.Id }, repeatableExpense));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutMonthlyExpense(int id, MonthlyExpenseInputModel monthlyExpenseInput)
        {
            var uid             = GetUserId();
            var expenseToModify = await this.UserMonthlyExpensesQueryBuilder(uid).Where(c => c.Id == id).FirstOrDefaultAsync();

            if (expenseToModify == null)
            {
                throw new Exception("You don't own any current expense with that id");
            }

            expenseToModify.ExpiryDate            = monthlyExpenseInput.ExpiryDate;
            expenseToModify.TriggeringDateOfMonth = monthlyExpenseInput.TriggeringDayOfMonth;
            expenseToModify.ExpenseData           = new ExpenseData
            {
                Amount      = monthlyExpenseInput.Amount,
                Description = monthlyExpenseInput.Description
            };

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

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

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <int> Register(string username, string password)
        {
            var existingCheck = _context.Users.Where(x => x.Username == username).FirstOrDefault();

            if (existingCheck != null)
            {
                throw new Exception("Username already taken");
            }

            var user = new User {
                Username = username, PasswordHash = _hashingService.Hash(password)
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(user.Id);
        }