コード例 #1
0
        public async Task <PlanSummary> GetCurrentPlanSummaryAsync()
        {
            IGenericRepository <Plan> plansRepo = RepositoryFactory.GetGenericRepository <Plan>();
            var filter = new PlansFilter
            {
                PageSize = 1
            };

            filter.SetFilters(GetCurrentPlanCondition());

            IList <Plan> plans = await plansRepo.GetAsync(filter);

            if (plans == null || plans.Count == 0 || plans.Count > 1)
            {
                throw new DocumentNotFoundException("Nie odnaleziono planu budżetowego o wskazanych kryteriach");
            }

            Plan plan = plans.SingleOrDefault();

            if (plan == null)
            {
                throw new DocumentNotFoundException("Nie odnaleziono planu budżetowego o wskazanych kryteriach");
            }

            return(await GetSummaryAsync(plan.Id));
        }
コード例 #2
0
        public async Task <Plan> GetNewPlanAsync()
        {
            IGenericRepository <Plan> plansRepo = RepositoryFactory.GetGenericRepository <Plan>();
            var filter = new PlansFilter
            {
                PageSize = 1
            };

            Plan lastPlan = (await plansRepo.GetAsync(filter)).FirstOrDefault();

            var plan = new Plan();
            var now  = DateTime.Now;

            plan.StartDate = new DateTime(now.Year, now.Month, 1);
            plan.EndDate   = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));

            if (lastPlan?.Incomes != null)
            {
                plan.Incomes = lastPlan.Incomes.Select(item => new Income
                {
                    Category = item.Category,
                }).ToList();
            }
            else
            {
                plan.Incomes = _predefinedCategories.IncomeCategories.Select(x => new Income
                {
                    Category = x.Name
                }).ToList();
            }

            if (lastPlan?.Expenses != null)
            {
                plan.Expenses = lastPlan.Expenses.Select(x => new Expense
                {
                    Type     = x.Type,
                    Category = x.Category
                }).ToList();
            }
            else
            {
                plan.Expenses = (from expenseCategory in _predefinedCategories.ExpenseCategories
                                 from expenseCategoryType in expenseCategory.Types
                                 select new Expense
                {
                    Type = expenseCategoryType.Name,
                    Category = expenseCategory.Name
                }).ToList();
            }

            return(plan);
        }
コード例 #3
0
        public async Task <IList <PlanListItem> > GetAsync(PlansFilter filter)
        {
            IGenericRepository <Plan> repo = RepositoryFactory.GetGenericRepository <Plan>();

            var currentPlanFilter = new PlansFilter
            {
                PageSize = 1
            };

            currentPlanFilter.SetFilters(GetCurrentPlanCondition());

            string currentPlanId = (await repo.GetAsAsync(plan => plan.Id, currentPlanFilter))
                                   .FirstOrDefault();

            var searchFilter      = filter ?? new PlansFilter();
            var filterExpressions = new List <Expression <Func <Plan, bool> > >();

            if (searchFilter.DateTo != null)
            {
                filterExpressions.Add(plan => plan.EndDate == null || plan.EndDate <= searchFilter.DateTo);
            }

            if (searchFilter.DateFrom != null)
            {
                filterExpressions.Add(plan => plan.StartDate == null || plan.StartDate >= searchFilter.DateFrom);
            }

            searchFilter.SetFilters(filterExpressions.ToArray());

            Expression <Func <Plan, PlanListItem> > planSummaryMapExpression = plan => new PlanListItem
            {
                Id          = plan.Id,
                StartDate   = plan.StartDate,
                EndDate     = plan.EndDate,
                IncomesSum  = plan.Incomes.Sum(income => income.Amount),
                ExpensesSum = plan.Expenses.Sum(expense => expense.Amount),
                IsActive    = plan.Id == currentPlanId
            };

            return(await repo.GetAsAsync(planSummaryMapExpression, searchFilter));
        }
コード例 #4
0
        public override async Task InsertAsync(Plan document)
        {
            IGenericRepository <Plan> repo = RepositoryFactory.GetGenericRepository <Plan>();
            var filter = new PlansFilter
            {
                PageSize = 1
            };

            filter.SetFilters(plan => document.StartDate <= plan.StartDate);

            IList <Plan> existingCurrentPlans = await repo.GetAsync(filter);

            if (existingCurrentPlans.Any())
            {
                throw new DuplicateNameException("Już istnieje plan budżetowy dla wybranego okresu");
            }

            await SyncCategoriesAsync <IncomeCategory>(document);
            await SyncCategoriesAsync <ExpenseCategory>(document);

            await repo.InsertAsync(document);
        }
コード例 #5
0
        public async Task <IActionResult> Get([FromBody] PlansFilter filter)
        {
            IList <PlanListItem> plans = await _planService.GetAsync(filter);

            return(Ok(plans));
        }