Exemple #1
0
        public async Task <Result <Cards> > RegisterCardsAsync(RegisterCardOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Description))
            {
                return new Result <Cards>()
                       {
                           Code    = ResultCodes.BadRequest,
                           Message = $"Prepei na doseis perigrafi!"
                       }
            }
            ;


            var cards = new Cards()
            {
                Description = options.Description
            };

            await _dbContext.AddAsync <Cards>(cards);

            await _dbContext.SaveChangesAsync();

            return(new Result <Cards>()
            {
                Code = ResultCodes.Success,
                Data = cards
            });
        }
Exemple #2
0
 public async Task <int> Create(Card card)
 {
     try {
         _context.Add(card);
         return(await _context.SaveChangesAsync());
     }
     catch (Exception ex)
     {
         return(-1);
     }
 }
Exemple #3
0
        public async Task <bool> SaveCard(CardModel cardModel)
        {
            var card = _dbContext.Cards.Where
                           (x => x.Id == cardModel.Id).FirstOrDefault();

            if (card == null)
            {
                card = new Card()
                {
                    Name        = cardModel.Name,
                    Description = cardModel.Description,
                    ImageUrl    = cardModel.ImageUrl,
                    Type        = cardModel.Type,
                    Value       = cardModel.Value
                };
                _dbContext.Cards.Add(card);
            }
            else
            {
                card.Name        = cardModel.Name;
                card.Description = cardModel.Description;
                card.ImageUrl    = cardModel.ImageUrl;
                card.Type        = cardModel.Type;
                card.Value       = cardModel.Value;
            }

            return(await _dbContext.SaveChangesAsync() >= 1);
        }
        public async Task <Result <Transaction> > RegisterTransactionAsync(RegisterTransactionOptions options)
        {
            if (options.Amount <= 0)
            {
                return(new Result <Transaction>()
                {
                    Code = ResultCodes.BadRequest,
                    Message = $"Prepei na doseis poso megalirerro tou midenos."
                });
            }


            var cards = (await _cards.GetCardsByIdAsync(options.cardsId)).Data;//fernoume tin karta kai ta transaction(limits) ola (incluce)


            if (cards != null)//an i karta iparxei
            {
                DateTime d            = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                string   transDateNow = d.ToString();


                //pernoume ta simerina transactions (limits) kai eCommerce kai cardPresent, ola (to poli mexri dio na mas ferei)
                var transToday = cards.Transactions
                                 .Where(t => t.Created == transDateNow)
                                 .ToList();


                if (cards.Balance > options.Amount)
                {
                    if (options.TransType == TransactionType.CardPresent)
                    {
                        var CardPresentToday = transToday.Where(t => t.Type == TransactionType.CardPresent)//fere tin simerini(transToday vl pio pano) eggrafi  me ton tipo cardPresent
                                               .SingleOrDefault();

                        if (CardPresentToday == null)
                        {
                            var t = new Transaction()
                            {
                                Type   = options.TransType,
                                Amount = options.Amount + CardPresentToday.Amount
                            };
                            cards.Transactions.Add(t);
                            cards.Balance = cards.Balance - options.Amount;
                            _dbContext.Update(t);
                            await _dbContext.SaveChangesAsync();

                            return(new Result <Transaction>()
                            {
                                Code = ResultCodes.Success,
                                Data = t
                            });
                        }
                        else//an idi simera exei ginei mia cardPresent
                        {
                            if (CardPresentToday.Amount + options.Amount <= 1500)
                            {
                                var t = new Transaction()
                                {
                                    Type   = options.TransType,
                                    Amount = CardPresentToday.Amount + options.Amount
                                };
                                CardPresentToday = t;
                                cards.Balance    = cards.Balance - options.Amount;
                                await _dbContext.SaveChangesAsync();

                                return(new Result <Transaction>()
                                {
                                    Code = ResultCodes.Success,
                                    Data = t
                                });
                            }
                            else
                            {
                                return(new Result <Transaction>()
                                {
                                    Code = ResultCodes.BadRequest,
                                    Message = $"Me tin agora afti ksepernas to orio tou cardPresent pou einai 1500"
                                });
                            }
                        }
                    }
                    else if (options.TransType == TransactionType.CardPresent)
                    {
                        var ECommerceToday = transToday.Where(t => t.Type == TransactionType.ECommerce)
                                             .SingleOrDefault();

                        if (ECommerceToday == null)
                        {
                            var t = new Transaction()
                            {
                                Type   = options.TransType,
                                Amount = options.Amount + ECommerceToday.Amount
                            };
                            cards.Transactions.Add(t);
                            cards.Balance = cards.Balance - options.Amount;
                            _dbContext.Update(t);
                            await _dbContext.SaveChangesAsync();

                            return(new Result <Transaction>()
                            {
                                Code = ResultCodes.Success,
                                Data = t
                            });
                        }
                        else
                        {
                            if (ECommerceToday.Amount + options.Amount <= 500)
                            {
                                var t = new Transaction()
                                {
                                    Type   = options.TransType,
                                    Amount = ECommerceToday.Amount + options.Amount
                                };
                                ECommerceToday = t;
                                cards.Balance  = cards.Balance - options.Amount;
                                await _dbContext.SaveChangesAsync();

                                return(new Result <Transaction>()
                                {
                                    Code = ResultCodes.Success,
                                    Data = t
                                });
                            }
                            else
                            {
                                return(new Result <Transaction>()
                                {
                                    Code = ResultCodes.BadRequest,
                                    Message = $"Me tin agora afti ksepernas to orio tou cardPresent pou einai 1500"
                                });
                            }
                        }
                    }
                    else
                    {
                        return(new Result <Transaction>()
                        {
                            Code = ResultCodes.BadRequest,
                            Message = $"Kati den epelekses kala me to tipo tis kinisis"
                        });
                    }
                }
                else
                {
                    return(new Result <Transaction>()
                    {
                        Code = ResultCodes.BadRequest,
                        Message = $"To poso agoras {options.Amount} ksepernaei to poso tou ipoloipou tis kartas {cards.Balance}"
                    });
                }
            }
            else
            {
                return(new Result <Transaction>()
                {
                    Code = ResultCodes.BadRequest,
                    Message = $"Den iparxei i karta me kodiko {options.cardsId}"
                });
            }
        }