public async Task <IActionResult> AddCategory([FromBody] ApiCategory model)
        {
            var category = new Category
            {
                UserId    = Token.UserId,
                Name      = model.Name,
                Colour    = model.Colour,
                Type      = model.Type,
                Recurring = model.Recurring,
                DateAdded = DateTime.UtcNow
            };

            if (category.Recurring)
            {
                category.RecurringDate  = model.RecurringDate;
                category.RecurringValue = model.RecurringValue;
            }

            _uow.Categories.Add(category);
            await _uow.CommitAsync();

            model = _mapper.Map <Category, ApiCategory>(category);

            return(new JsonResult(model));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddCategory([FromBody] ApiCategory model)
        {
            var category = new Category
            {
                UserId    = Token.UserId,
                Name      = model.Name,
                Type      = model.Type,
                ColourHex = model.ColourHex,
                DateAdded = DateTime.UtcNow
            };

            _uow.Categories.Add(category);
            await _uow.CommitAsync();

            model = _mapper.Map <Category, ApiCategory>(category);

            return(new JsonResult(model));
        }
        public async Task AddUserAsync(User user)
        {
            user.CreatedBy   = 1;
            user.CreatedDate = DateTime.Now;

            await _uow.Users.AddAsync(user);

            await _uow.CommitAsync();
        }
Esempio n. 4
0
    public async Task Test1()
    {
        // Arrange
        var hotel = new Hotel
        {
            Nombre = "Prueba",
            Id     = 0
        };

        // Act
        await uow.Hotels.Add(hotel);

        await uow.CommitAsync();

        var number = await uow.Hotels.CountAll();

        // Assert
        uow.Should().NotBeNull();
        number.Should().Be(1);
    }
Esempio n. 5
0
        public async Task <IActionResult> ExecutarAsync <T>(Task <T> task, CancellationToken cancellationToken = default) where T : Resposta
        {
            try
            {
                var resposta = await task;
                if (resposta.Tipo == RespostaTipo.Sucesso)
                {
                    await _uow.CommitAsync(cancellationToken);
                }

                return(Ok(resposta));
            }catch (Exception ex)
            {
                return(BadRequest(Resposta.Falha("Ocorreu um erro interno no servidor!", new
                {
                    ex.Message,
                    ex.StackTrace
                })));
            }
        }
Esempio n. 6
0
    public async Task AddHotelAsync(Hotel hotel)
    {
        await uow.Hotels.Add(hotel);

        await uow.CommitAsync();
    }
Esempio n. 7
0
        public async Task <IActionResult> GetBudget(int year, int month)
        {
            if (month == 0)
            {
                month = 1;
            }

            var budget = await _uow.Budgets.GetBudget(year, month);

            if (budget != null)
            {
                var categories = _uow.Categories
                                 .GetAll()
                                 .Where(x => x.UserId == Token.UserId)
                                 .ToList();

                var budgetOutgoingCatIds = budget.Outgoings.Select(x => x.CategoryId).ToArray();
                var budgetIncomeCatdIds  = budget.Incomes.Select(X => X.CategoryId).ToArray();
                var outgoingCatIds       = categories.Where(x => x.Type == CategoryType.Outgoing).Select(x => x.Id).ToArray();
                var incomeCatIds         = categories.Where(x => x.Type == CategoryType.Income).Select(x => x.Id).ToArray();

                var missingOutgoingKeys = outgoingCatIds.Where(x => !budgetOutgoingCatIds.Contains(x)).Select(x => x);
                var missingIncomeKeys   = incomeCatIds.Where(x => !budgetIncomeCatdIds.Contains(x)).Select(x => x);

                if (missingOutgoingKeys.Any())
                {
                    var outgoings = categories
                                    .Where(x => missingOutgoingKeys.Contains(x.Id))
                                    .Select(x => new Outgoing
                    {
                        BudgetId   = budget.Id,
                        UserId     = Token.UserId,
                        DateAdded  = DateTime.UtcNow,
                        CategoryId = x.Id
                    })
                                    .ToList();

                    await _uow.Outgoings.AddRangeAsync(outgoings);

                    budget.Outgoings.Concat(outgoings);
                    await _uow.CommitAsync();
                }

                if (missingIncomeKeys.Any())
                {
                    var incomes = categories
                                  .Where(x => missingIncomeKeys.Contains(x.Id))
                                  .Select(x => new Income
                    {
                        BudgetId   = budget.Id,
                        UserId     = Token.UserId,
                        DateAdded  = DateTime.UtcNow,
                        CategoryId = x.Id
                    })
                                  .ToList();

                    await _uow.Incomes.AddRangeAsync(incomes);

                    budget.Incomes.Concat(incomes);
                    await _uow.CommitAsync();
                }

                var apiBudget     = _mapper.Map <Budget, ApiBudget>(budget);
                var apiCategories = _mapper.Map <IEnumerable <Category>, IEnumerable <ApiCategory> >(categories);

                apiBudget.Categories = apiCategories;

                return(new JsonResult(apiBudget));
            }
            else
            {
                return(new NotFoundResult());
            }
        }
Esempio n. 8
0
        public static async Task SaveLog(IUow uow, string message, string userId)
        {
            uow._ManagerLog.Add(new EF.ManagerLog
            {
                EventDateTime = DateTime.UtcNow,
                EventDetails = message,
                UserId=userId
            });

            await uow.CommitAsync();
        }
Esempio n. 9
0
        public async Task <IActionResult> GetBudget(int year, int month)
        {
            var budget = await _uow.Budgets.GetBudget(year, month);

            if (budget == null)
            {
                budget = new Budget
                {
                    UserId    = Token.UserId,
                    Year      = year,
                    Month     = month,
                    DateAdded = DateTime.UtcNow
                };

                var categories = _uow.Categories
                                 .GetAll()
                                 .Where(x => x.UserId == Token.UserId)
                                 .ToList();

                var incomes = categories
                              .Where(x => x.Type == CategoryType.Income)
                              .Select(x => new Income
                {
                    BudgetId   = budget.Id,
                    UserId     = Token.UserId,
                    DateAdded  = DateTime.UtcNow,
                    CategoryId = x.Id,
                    Total      = x.Recurring && x.RecurringValue.HasValue ? x.RecurringValue.Value : 0,
                    Date       = x.RecurringDate
                })
                              .ToList();

                await _uow.Incomes.AddRangeAsync(incomes);

                var outgoings = categories
                                .Where(x => x.Type == CategoryType.Dedicated || x.Type == CategoryType.Variable)
                                .Select(x => new Outgoing
                {
                    BudgetId   = budget.Id,
                    UserId     = Token.UserId,
                    DateAdded  = DateTime.UtcNow,
                    CategoryId = x.Id,
                    Budgeted   = x.Recurring && x.RecurringValue.HasValue ? x.RecurringValue.Value : 0,
                    Date       = x.RecurringDate
                })
                                .ToList();

                await _uow.Outgoings.AddRangeAsync(outgoings);

                var savings = categories
                              .Where(x => x.Type == CategoryType.Savings)
                              .Select(x => new Saving
                {
                    BudgetId   = budget.Id,
                    UserId     = Token.UserId,
                    DateAdded  = DateTime.UtcNow,
                    CategoryId = x.Id,
                    Total      = x.Recurring && x.RecurringValue.HasValue ? x.RecurringValue.Value : 0,
                    Date       = x.RecurringDate
                })
                              .ToList();

                await _uow.Savings.AddRangeAsync(savings);

                budget.Incomes   = incomes;
                budget.Outgoings = outgoings;
                budget.Savings   = savings;

                var transactions = categories
                                   .Where(x => x.Recurring)
                                   .Select(x => new Transaction
                {
                    BudgetId   = budget.Id,
                    Budget     = budget,
                    UserId     = Token.UserId,
                    CategoryId = x.Id,
                    Category   = x,
                    Amount     = x.RecurringValue.Value,
                    Date       = x.RecurringDate.Value
                })
                                   .ToList();

                budget.Transactions = transactions;

                await _uow.Transactions.AddRangeAsync(transactions);

                await _uow.Budgets.AddAsync(budget);

                await _uow.CommitAsync();
            }

            var model = _mapper.Map <Budget, ApiBudget>(budget);

            return(new JsonResult(model));
        }