Exemplo n.º 1
0
        public async Task <List <AccountDto> > All()
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                List <Account> accountsToMap = await context.Accounts.ToListAsync();

                return(Mapper.Map <List <Account>, List <AccountDto> >(accountsToMap));
            }
        }
Exemplo n.º 2
0
        public async Task <List <OperationTypeDto> > All()
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                List <OperationType> fromDb = await context.OperationTypes.ToListAsync();

                return(Mapper.Map <List <OperationType>, List <OperationTypeDto> >(fromDb));
            }
        }
Exemplo n.º 3
0
        public async Task Update(OperationTypeDto t)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                OperationType toUpdate = Mapper.Map <OperationTypeDto, OperationType>(t);

                context.OperationTypes.Attach(toUpdate);
                context.Entry(toUpdate).State = System.Data.Entity.EntityState.Modified;
                await context.SaveChangesAsync();
            }
        }
Exemplo n.º 4
0
        public async Task <int> Add(OperationTypeDto item)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                OperationType toAdd = Mapper.Map <OperationTypeDto, OperationType>(item);
                context.OperationTypes.Add(toAdd);
                await context.SaveChangesAsync();

                return(toAdd.Id);
            }
        }
Exemplo n.º 5
0
        public async Task <int> Add(AccountDto toMap)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                Account toAdd = Mapper.Map <AccountDto, Account>(toMap);
                context.Accounts.Add(toAdd);
                await context.SaveChangesAsync();

                return(toAdd.Id);
            }
        }
Exemplo n.º 6
0
        public async Task <OperationTypeDto> Get(int i)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                OperationType toGet = await context.OperationTypes.FirstOrDefaultAsync(d => d.Id == i);

                if (toGet == null)
                {
                    return(null);
                }
                return(Mapper.Map <OperationType, OperationTypeDto>(toGet));
            }
        }
Exemplo n.º 7
0
        public async Task <AccountDto> Get(int i)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                Account fromDb = await context.Accounts.FirstOrDefaultAsync(d => d.Id == i);

                if (fromDb == null)
                {
                    return(null);
                }

                return(Mapper.Map <Account, AccountDto>(fromDb));
            }
        }
Exemplo n.º 8
0
        public async Task <List <AccountDto> > Filter(AccountDtoFilter filter)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                List <Account> fromDb = new List <Account>();

                if (filter.IdCurrency != null)
                {
                    fromDb = await context.Accounts.Where(d => d.IdCurrency == filter.IdCurrency).ToListAsync();
                }

                return(Mapper.Map <List <Account>, List <AccountDto> >(fromDb));
            }
        }
Exemplo n.º 9
0
        public async Task <OperationDto> Get(int i)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                Operation fromDb = await context.Operations.FirstOrDefaultAsync(d => d.Id == i);

                if (fromDb == null)
                {
                    return(null);
                }

                return(Mapper.Map <Operation, OperationDto>(fromDb));
            }
        }
Exemplo n.º 10
0
        public async Task Update(AccountDto t)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                //Account toUpdate = context.Accounts.FirstOrDefault(d => d.Id == t.Id);
                //toUpdate.Name = t.Name;
                //toUpdate.IdCurrency = t.IdCurrency;


                Account toUpdate = Mapper.Map <AccountDto, Account>(t);
                context.Accounts.Attach(toUpdate);
                context.Entry(toUpdate).State = System.Data.Entity.EntityState.Modified;

                await context.SaveChangesAsync();
            }
        }
Exemplo n.º 11
0
        public async Task <bool> Delete(int id)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                OperationType toDelete = await context.OperationTypes.FirstOrDefaultAsync(d => d.Id == id);

                if (toDelete == null)
                {
                    return(false);
                }

                context.OperationTypes.Remove(toDelete);
                await context.SaveChangesAsync();

                return(true);
            }
        }
Exemplo n.º 12
0
        public async Task <List <OperationDto> > Filter(OperationDtoFilter filter)
        {
            using (SpendingsDBContext context = new SpendingsDBContext())
            {
                IQueryable <Operation> query = context.Operations;

                if (filter.IdAccount != null)
                {
                    query = query.Where(d => d.IdAccount == filter.IdAccount);
                }

                if (filter.IdCurrency != null)
                {
                    query = query.Where(d => d.Account.IdCurrency == filter.IdCurrency);
                }

                if (filter.IdOperationType != null)
                {
                    query = query.Where(d => d.IdOperationType == filter.IdOperationType);
                }

                if (filter.AmmountFrom != null)
                {
                    query = query.Where(d => d.Ammount >= filter.AmmountFrom);
                }

                if (filter.AmmountTo != null)
                {
                    query = query.Where(d => d.Ammount <= filter.AmmountTo);
                }

                if (filter.DateFrom != null)
                {
                    query = query.Where(d => d.Date >= filter.DateFrom);
                }

                if (filter.DateTo != null)
                {
                    query = query.Where(d => d.Date <= filter.DateTo);
                }

                return(Mapper.Map <List <Operation>, List <OperationDto> >(await query.ToListAsync()));
            }
        }