예제 #1
0
        public async Task <Boolean> DeleteExpenseAsync(int groupId, string currentUserId, int expenseId)
        {
            Activity activity = new Activity();
            List <UserExpenseMapper> userExpense = new List <UserExpenseMapper>();

            if (groupId == 0)
            {
                userExpense = await _context.UserExpenseMappers.Where(u => u.Expenses.GroupID == null).Where(u => u.ExpenseID == expenseId).ToListAsync();
            }
            else
            {
                userExpense = await _context.UserExpenseMappers.Where(u => u.Expenses.GroupID == groupId).Where(u => u.ExpenseID == expenseId).ToListAsync();
            }
            if (userExpense != null)
            {
                foreach (var expense in userExpense)
                {
                    _context.UserExpenseMappers.Remove(expense);
                    await SaveAsync();
                }

                GroupExpenseMapper groupExpense = new GroupExpenseMapper();
                if (groupId == 0)
                {
                    groupExpense = await _context.GroupExpenseMappers.Where(g => g.ID == expenseId).Where(g => g.GroupID == null).Include(g => g.Groups).FirstOrDefaultAsync();
                }
                else
                {
                    groupExpense = await _context.GroupExpenseMappers.Where(g => g.ID == expenseId).Where(g => g.GroupID == groupId).Include(g => g.Groups).FirstOrDefaultAsync();
                }

                if (groupExpense != null)
                {
                    groupExpense.IsDeleted = true;

                    activity.UserID = currentUserId;
                    activity.Date   = DateTime.Now;

                    if (groupId == 0)
                    {
                        activity.Description = $"You deleted {groupExpense.Name} nongroup expense";
                    }
                    else
                    {
                        activity.Description = $"You deleted {groupExpense.Name} expense from {groupExpense.Groups.Name}";
                    }

                    _context.GroupExpenseMappers.Update(groupExpense);
                    await _context.Activities.AddAsync(activity);
                    await SaveAsync();

                    return(true);
                }
                return(false);
            }
            return(false);
        }
예제 #2
0
        public async Task CreateExpenseAsync(int groupId, string currentUserId, CreateExpenseAC expense)
        {
            Activity activity         = new Activity();
            var      currentGroupName = await _context.Groups.Where(g => g.ID == groupId).Select(g => g.Name).FirstOrDefaultAsync();

            GroupExpenseMapper groupExpenseMapper = new GroupExpenseMapper();

            groupExpenseMapper.Name      = expense.Name;
            groupExpenseMapper.SplitType = expense.SplitType;
            if (groupId == 0)
            {
                groupExpenseMapper.Payee = _userManager.FindByIdAsync(expense.Payee).Result.Id;
            }
            else
            {
                groupExpenseMapper.GroupID = groupId;
                groupExpenseMapper.Payee   = expense.Payee;
            }
            groupExpenseMapper.TotalAmount = expense.Amount;
            groupExpenseMapper.Date        = expense.Date;
            groupExpenseMapper.Description = expense.Note;
            groupExpenseMapper.IsDeleted   = false;
            groupExpenseMapper.IsSettled   = false;

            activity.UserID      = currentUserId;
            activity.Date        = DateTime.Now;
            activity.Description = $"You created the expense {groupExpenseMapper.Name} in {currentGroupName}";

            await _context.Activities.AddAsync(activity);

            await _context.GroupExpenseMappers.AddAsync(groupExpenseMapper);

            foreach (var expenses in expense.IncludedUsers)
            {
                if (expenses.Percentage == null)
                {
                    UserExpenseMapper userExpenseMapper = new UserExpenseMapper();

                    userExpenseMapper.ExpenseID = groupExpenseMapper.ID;

                    if (expenses.UserID == null)
                    {
                        userExpenseMapper.FromUser = _userManager.FindByNameAsync(expenses.UserName).Result.Id;
                    }
                    else
                    {
                        userExpenseMapper.FromUser = expenses.UserID;
                    }

                    if (expense.SplitType.Equals("Equal"))
                    {
                        userExpenseMapper.Amount = (expense.Amount / expense.IncludedUsers.Count());
                    }
                    else
                    {
                        userExpenseMapper.Amount = expenses.UserAmount;
                    }

                    await _context.UserExpenseMappers.AddAsync(userExpenseMapper);
                }
                else
                {
                    UserExpenseMapper userExpenseMapper = new UserExpenseMapper();

                    userExpenseMapper.ExpenseID = groupExpenseMapper.ID;
                    if (expenses.UserID == null)
                    {
                        userExpenseMapper.FromUser = _userManager.FindByNameAsync(expenses.UserName).Result.Id;
                    }
                    else
                    {
                        userExpenseMapper.FromUser = expenses.UserID;
                    }
                    userExpenseMapper.Amount = ((expense.Amount * expenses.Percentage) / 100);

                    await _context.UserExpenseMappers.AddAsync(userExpenseMapper);
                }
            }
        }