Exemplo n.º 1
0
        public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType)
        {
            var userPurchases = context.Users
                                .ToArray()
                                .Where(x => x.Cards.Any(y => y.Purchases.Any()))
                                .Select(x => new UserPurchasesExportModel
            {
                Username  = x.Username,
                Purchases = context.Purchases
                            .ToArray()
                            .Where(p => p.Card.User.Username == x.Username && p.Type.ToString() == storeType)
                            .OrderBy(p => p.Date)
                            .Select(p => new PurchaseExportModel
                {
                    CardNumber = p.Card.Number,
                    Cvc        = p.Card.Cvc,
                    Date       = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
                    Game       = new GameExportModel()
                    {
                        GameName = p.Game.Name,
                        Genre    = p.Game.Genre.Name,
                        Price    = p.Game.Price
                    }
                })
                            .ToArray(),

                TotalSpent = context.Purchases.ToArray().Where(p => p.Card.User.Username == x.Username &&
                                                               p.Type.ToString() == storeType).Sum(p => p.Game.Price)
            })
                                .Where(u => u.Purchases.Length > 0)
                                .OrderByDescending(x => x.TotalSpent)
                                .ThenBy(x => x.Username)
                                .ToList();

            var xml = XmlConverter.Serialize(userPurchases, "Users");

            return(xml);
        }
        public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType)
        {
            var data = context.Users
                       .ToArray()
                       .Where(u => u.Cards.Any(c => c.Purchases.Any(p => p.Type.ToString() == storeType)))
                       .Select(u => new UserPurchasesXmlModel
            {
                Username  = u.Username,
                Purchases = u.Cards.SelectMany(c => c.Purchases)
                            .Where(c => c.Type.ToString() == storeType)
                            .Select(p => new PurchaseXmlModel
                {
                    Card = p.Card.Number,
                    Cvc  = p.Card.Cvc,
                    Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
                    Game = new GameXmlModel
                    {
                        Title = p.Game.Name,
                        Genre = p.Game.Genre.Name,
                        Price = p.Game.Price
                    }
                })
                            .OrderBy(p => p.Date)
                            .ToArray(),
                TotalSpent = u.Cards.SelectMany(c => c.Purchases)
                             .Where(c => c.Type.ToString() == storeType)
                             .Sum(p => p.Game.Price)
            })
                       .OrderByDescending(u => u.TotalSpent)
                       .ThenBy(u => u.Username)
                       .ToArray();

            var xml = XmlConverter.Serialize(data, "Users");

            return(xml);
        }
Exemplo n.º 3
0
		public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType)
        {
            var users = context.Users.ToList().Where(x => x.Cards.Any(a => a.Purchases.Any(p=>p.Type.ToString()==storeType)))
                .Select(x => new UsersXmlExportModel()
                {
                    Username = x.Username,
                    Purchases = x.Cards.SelectMany(c => c.Purchases)
                        .Where(pp=>pp.Type.ToString()==storeType)
                        .Select(s => new PurchasesXmlOutputModel
                    {
                        Card = s.Card.Number,
                        Cvc = s.Card.Cvc,
                        Date = s.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
                        Game = new GameXmlOutputModel()
                        {
                            Title = s.Game.Name,
                            Genre = s.Game.Genre.Name,
                            Price = s.Game.Price,
                        }
                    })
                        .OrderBy(o=>o.Date)
                        .ToArray(),
                    TotalSpent = x.Cards.Sum(s =>
                        s.Purchases.Where(p => p.Type.ToString() == storeType).Sum(su => su.Game.Price))
                })
                .OrderByDescending(x=>x.TotalSpent)
                .ThenBy(x=>x.Username)
                .ToList();


            //var users = context.Users.Where(x => x.Cards.Any(c => c.Purchases.Any()))
            //    .SelectMany(x=>x.Cards, (x,c) => new
            //    {
            //        x.Username,
            //        Purchases = c.Purchases.Where(p=>p.Type.ToString() == storeType).Select(s=> new
            //        {
            //            Card = s.Card.Number,
            //            Cvc = s.Card.Cvc,
            //            Date = s.Date.ToString("yyyy-MM-dd HH:mm",CultureInfo.InvariantCulture),
            //            Game = s.Game.Name,
            //            GameGenre = s.Game.Genre.Name,
            //            GamePrice = s.Game.Price
            //        }),
            //    }).ToList();
            //var usersList = new List<UsersXmlExportModel>();

            //foreach (var user in users)
            //{
            //    var newUser = new UsersXmlExportModel
            //    {
            //        Username = user.Username,
                    
            //    };

            //    var list = new List<PurchasesXmlOutputModel>();

            //    foreach (var purchase in user.Purchases)
            //    {
            //        var game = new GameXmlOutputModel()
            //        {
            //            Genre = purchase.GameGenre,
            //            Price = purchase.GamePrice,
            //            Title = purchase.Game
            //        };


            //        var purchaseForUser = new PurchasesXmlOutputModel
            //        {
            //            Card = purchase.Card,
            //            Game = game,
            //            Cvc = purchase.Cvc,
            //            Date = purchase.Date

            //        };

            //        list.Add(purchaseForUser);
            //    }

            //    newUser.Purchases = list.ToArray();
            //    newUser.TotalSpent = 69;

            //    usersList.Add(newUser);
            //}

            var result = XmlConverter.Serialize(users, "Users");
            

            return result;
        }
Exemplo n.º 4
0
        public static string ImportPurchases(VaporStoreDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var purcasesXml = XmlConverter.Deserializer <PurchaseInputModel>(xmlString, "Purchases");

            List <Purchase> purchases = new List <Purchase>();

            foreach (var xmlPurchase in purcasesXml)
            {
                if (!IsValid(xmlPurchase))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                object purchaseTypeObj;
                bool   isPurchaseTypeValid =
                    Enum.TryParse(typeof(PurchaseType), xmlPurchase.PurchaiseType, out purchaseTypeObj);

                if (!isPurchaseTypeValid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                PurchaseType purchaseType = (PurchaseType)purchaseTypeObj;

                DateTime date;
                bool     isDateValid = DateTime.TryParseExact(xmlPurchase.Date, "dd/MM/yyyy HH:mm",
                                                              CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

                if (!isDateValid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var card = context.Cards.FirstOrDefault(x => x.Number == xmlPurchase.Card);
                var game = context.Games.FirstOrDefault(x => x.Name == xmlPurchase.GameTitle);

                if (card == null || game == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var purchase = new Purchase
                {
                    Type       = purchaseType,
                    ProductKey = xmlPurchase.Key,
                    Date       = date,
                    Card       = card,
                    Game       = game,
                };

                purchases.Add(purchase);
                var username = context.Users.
                               Where(x => x.Id == purchase.Card.UserId).Select(x => x.Username).FirstOrDefault();
                //sb.AppendLine($"Imported {xmlPurchase.GameTitle} for {purchase.Card.User.Username}");
                sb.AppendLine($"Imported {xmlPurchase.GameTitle} for {username}");
            }

            context.Purchases.AddRange(purchases);

            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Exemplo n.º 5
0
        public static string ImportPurchases(VaporStoreDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            string xmlRoot   = "Purchases";
            var    purchases = XmlConverter.Deserializer <PurchaseDto>(xmlString, xmlRoot);

            List <Purchase> dbPurchases = new List <Purchase>();


            var games = context.Games.ToList();
            var cards = context.Cards
                        .AsQueryable()
                        .Include(c => c.User)
                        .ToList();

            foreach (var purchase in purchases)
            {
                if (!IsValid(purchase))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                Purchase dbPurchase = new Purchase()
                {
                    ProductKey = purchase.Key
                };

                if (!DateTime
                    .TryParseExact(purchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture,
                                   DateTimeStyles.None, out DateTime purchaseDate))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                dbPurchase.Date = purchaseDate;

                if (!Enum.TryParse(typeof(PurchaseType), purchase.Type, out object purchaseType))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                dbPurchase.Type = (PurchaseType)purchaseType;

                var card = cards.FirstOrDefault(c => c.Number == purchase.Card);

                if (card == null)
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                dbPurchase.Card = card;

                var game = games.FirstOrDefault(g => g.Name == purchase.Title);

                if (game == null)
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                dbPurchase.Game = game;
                dbPurchases.Add(dbPurchase);
                sb.AppendLine($"Imported {purchase.Title} for {card.User.Username}");
            }

            context.Purchases.AddRange(dbPurchases);
            context.SaveChanges();

            return(sb.ToString());
        }