Пример #1
0
        public UserDto Handle(NewUserCommand request)
        {
            using (var context = new BoilerDbContext())
            {
                var newUser = new User();

                using (var trans = context.Database.BeginTransaction())
                {
                    try
                    {
                        newUser = new User()
                        {
                            Firstname  = request.Firstname,
                            Middlename = request.Middlename,
                            Lastname   = request.Lastname,
                            Age        = request.Age
                        };

                        _dbContext.Users.Add(newUser);
                        _dbContext.SaveChanges();


                        _dbContext.SaveChanges();
                    }

                    catch (Exception error)
                    {
                        throw new Exception("Something went wrong");
                    }
                }


                return(Mapper.Map <UserDto>(newUser));
            }
        }
Пример #2
0
        public PizzaDto Handle(NewPizzaCalculationCommand request)
        {
            var newPizzaCalc = new Pizza
            {
                Flavours       = request.Flavours,
                MaxSlices      = request.MaxSlices,
                NumberOfPeople = request.NumberOfPeople,
                Slices         = request.Slices,
                UserId         = request.UserId
            };

            _dbContext.Pizzas.Add(newPizzaCalc);
            _dbContext.SaveChanges();

            var dto = new PizzaDto()
            {
                UserId         = newPizzaCalc.UserId,
                Slices         = newPizzaCalc.Slices,
                MaxSlices      = newPizzaCalc.MaxSlices,
                Flavours       = newPizzaCalc.Flavours,
                NumberOfPeople = newPizzaCalc.NumberOfPeople,
                NumberOfPizza  = (decimal)(newPizzaCalc.NumberOfPeople * newPizzaCalc.Flavours * newPizzaCalc.MaxSlices) / newPizzaCalc.Slices
            };


            return(dto);
        }
Пример #3
0
        public UserDto Handle(DeleteUserByIdCommand request)
        {
            var selectedUSer = _dbContext.Users.FirstOrDefault(users => users.Id == request.Id);

            var selectedUserDto = Mapper.Map <UserDto>(selectedUSer);

            _dbContext.Users.Remove(selectedUSer);

            _dbContext.SaveChanges();

            return(selectedUserDto);
        }
Пример #4
0
        public IncomeDto Handle(EditIncomeCommand request)
        {
            var selectedIncome = _dbContext.Incomes.FirstOrDefault(x => x.Id == request.Income.Id);

            selectedIncome.Date        = request.Income.Date;
            selectedIncome.Description = request.Income.Description;
            selectedIncome.Remarks     = request.Income.Remarks;
            selectedIncome.Total       = request.Income.Total;

            _dbContext.SaveChanges();

            return(Mapper.Map <IncomeDto>(selectedIncome));
        }
Пример #5
0
        public int Handle(DeleteExpenseByExpenseIdCommand request)
        {
            var selectedExpense = _dbContext.Expenses.FirstOrDefault(expense => expense.Id == request.ExpenseId);

            if (selectedExpense == null)
            {
                throw new Exception("Expense does not exist");
            }

            _dbContext.Expenses.Remove(selectedExpense);
            _dbContext.SaveChanges();

            return(selectedExpense.Id);
        }
Пример #6
0
        public int Handle(DeleteIncomeByIncomeIdCommand request)
        {
            var selectedIncome = _dbContext.Incomes.FirstOrDefault(x => x.Id == request.IncomeId);

            if (selectedIncome == null)
            {
                throw new Exception("Income does not exist");
            }

            _dbContext.Incomes.Remove(selectedIncome);
            _dbContext.SaveChanges();

            return(selectedIncome.Id);
        }
Пример #7
0
        public bool Handle(DoneTodoCommand request)
        {
            var selectedTodo = _dbContext.Todos.FirstOrDefault(x => x.Id == request.TodoId);

            if (selectedTodo == null)
            {
                throw new Exception("No todo found");
            }

            selectedTodo.Done = true;
            _dbContext.SaveChanges();


            return(true);
        }
Пример #8
0
        public bool Handle(DeleteTodoByIdCommand request)
        {
            var todo = _dbContext.Todos.FirstOrDefault(x => x.Id == request.TodoId);

            if (todo == null)
            {
                throw new Exception("No todo found");
            }

            _dbContext.Todos.Remove(todo);

            _dbContext.SaveChanges();

            return(true);
        }
Пример #9
0
        public ExpenseDto Handle(NewExpenseCommand request)
        {
            var previousExpenseTotal = _dbContext.Expenses.Where(x => x.UserId == request.UserId &&
                                                                 (x.Date.Value.Month == request.ExpenseDate.Value.Month &&
                                                                  x.Date.Value.Year == request.ExpenseDate.Value.Year)).Sum(aaa => (decimal?)aaa.Total) ?? 0;

            var totalCurrentExpense = previousExpenseTotal + request.Total;

            var incomeTotal = _dbContext.Incomes.Where(x => x.UserId == request.UserId && (x.Date.Value.Month == request.ExpenseDate.Value.Month &&
                                                                                           x.Date.Value.Year == request.ExpenseDate.Value.Year)).Sum(aaa => (decimal?)aaa.Total) ?? 0;

            //Update the balance
            var balance = _dbContext.Balances.FirstOrDefault(x => x.UserId == request.UserId &&
                                                             (x.BalanceDate.Value.Month == request.ExpenseDate.Value.Month && x.BalanceDate.Value.Year == request.ExpenseDate.Value.Year));

            var newBalance = new Balance();

            if (balance == null)
            {
                newBalance.BalanceDate  = request.ExpenseDate;
                newBalance.TotalBalance = incomeTotal - totalCurrentExpense;
                newBalance.UserId       = request.UserId;

                _dbContext.Balances.Add(newBalance);
            }
            else
            {
                balance.TotalBalance = incomeTotal - totalCurrentExpense;
                balance.BalanceDate  = request.ExpenseDate;
            }


            var newExpense = new Expense
            {
                Date        = request.ExpenseDate,
                Description = request.Description,
                Remarks     = request.Remarks,
                Status      = request.Status,
                Total       = request.Total,
                UserId      = request.UserId
            };

            _dbContext.Expenses.Add(newExpense);
            _dbContext.SaveChanges();


            return(Mapper.Map <ExpenseDto>(newExpense));
        }
Пример #10
0
        public TodoDto Handle(NewTodoCommand request)
        {
            var newTodos = new Expense()
            {
                Done     = false,
                Todos    = request.Todos,
                TodoDate = request.TodoDate,
                UserId   = request.UserId
            };

            _dbContext.Todos.Add(newTodos);

            var samp = _dbContext.Todos.Select(x => x).ToList();

            _dbContext.SaveChanges();

            return(Mapper.Map <TodoDto>(newTodos));
        }
Пример #11
0
        public UserDto Handle(EditUserCommand request)
        {
            var selectedUser = _dbContext.Users.FirstOrDefault(x => x.Id == request.Id);

            if (selectedUser == null)
            {
                throw new Exception("No user found");
            }

            selectedUser.Lastname   = request.Lastname;
            selectedUser.Firstname  = request.Firstname;
            selectedUser.Middlename = request.Middlename;
            selectedUser.Age        = request.Age;

            _dbContext.SaveChanges();


            return(Mapper.Map <UserDto>(selectedUser));
        }
Пример #12
0
        public IncomeDto Handle(NewIncomeCommand request)
        {
            var previousIncomeTotal = _dbContext.Incomes.Where(x => x.UserId == request.UserId && (x.Date.Value.Month == request.Date.Value.Month &&
                                                                                                   x.Date.Value.Year == request.Date.Value.Year)).Sum(aaa => (decimal?)aaa.Total) ?? 0;
            var totalCurrentIncome = previousIncomeTotal + request.Total;

            var expenseTotal = _dbContext.Expenses.Where(x => x.UserId == request.UserId && (x.Date.Value.Month == request.Date.Value.Month &&
                                                                                             x.Date.Value.Year == request.Date.Value.Year)).Sum(aaa => (decimal?)aaa.Total) ?? 0;

            var newIncome = Mapper.Map <Income>(request);

            //Update the balance if there's balance if not create balance
            var balance = _dbContext.Balances.FirstOrDefault(x => x.UserId == request.UserId &&
                                                             (x.BalanceDate.Value.Month == request.Date.Value.Month && x.BalanceDate.Value.Year == request.Date.Value.Year));

            var newBalance = new Balance();

            if (balance == null)
            {
                newBalance.BalanceDate  = request.Date;
                newBalance.TotalBalance = totalCurrentIncome - expenseTotal;
                newBalance.UserId       = request.UserId;

                _dbContext.Balances.Add(newBalance);
            }
            else
            {
                balance.TotalBalance = totalCurrentIncome - expenseTotal;
                balance.BalanceDate  = request.Date;
            }



            #region Update Balance
            //Update Balance
            //balance = new Balance()
            //{
            //    BalanceDate = request.Date,
            //    TotalBalance = totalCurrentIncome - expenseTotal,
            //    UserId = request.UserId,

            //};
            //_dbContext.Balances.Add(balance);
            #endregion


            _dbContext.Incomes.Add(newIncome);
            _dbContext.SaveChanges();


            var incomeDto = new IncomeDto()
            {
                //CarriedOver = balanceIsCarriedOver,
                UserId      = request.UserId,
                Date        = request.Date,
                Description = request.Description,
                Id          = newIncome.Id,
                Remarks     = request.Remarks,
                Total       = request.Total
            };

            return(incomeDto);
        }
Пример #13
0
        public UserDto Handle(NewUserCommand request)
        {
            using (var context = new BoilerDbContext())
            {
                var newUser = new User();
                var balance = new Balance();
                #region Handle expense
                // var expense = _dbContext.Expenses.Where(item => item.UserId == request.)
                #endregion


                using (var trans = context.Database.BeginTransaction())
                {
                    try
                    {
                        newUser = new User()
                        {
                            Firstname  = request.Firstname,
                            Middlename = request.Middlename,
                            Lastname   = request.Lastname,
                            Age        = request.Age
                        };

                        _dbContext.Users.Add(newUser);
                        _dbContext.SaveChanges();

                        var income = new Income()
                        {
                            UserId      = newUser.Id,
                            Date        = request.IncomeDate,
                            Description = request.Description,
                            Total       = request.Total,
                            Remarks     = request.Remarks
                        };

                        _dbContext.Incomes.Add(income);
                        _dbContext.SaveChanges();

                        balance = new Balance()
                        {
                            UserId       = newUser.Id,
                            BalanceDate  = request.IncomeDate,
                            TotalBalance = request.Total,
                            CarriedOver  = false
                        };

                        _dbContext.Balances.Add(balance);
                        _dbContext.SaveChanges();
                    }

                    catch (Exception error)
                    {
                        throw new Exception("Something went wrong");
                    }
                }

                var newUserDto = new UserDto()
                {
                    Firstname   = request.Firstname,
                    Lastname    = request.Lastname,
                    Middlename  = request.Middlename,
                    Age         = request.Age,
                    TotalIncome = request.Total,
                    Balance     = balance.TotalBalance,
                    Id          = newUser.Id
                };


                return(newUserDto);
            }
        }