Beispiel #1
0
        public static string ImportPurchases(VaporStoreDbContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var purchases = new List <Purchase>();

            var importPurchases = XMLConverter.Deserializer <PurchaseInputModel>(xmlString, "Purchases");

            foreach (var importPurchase in importPurchases)
            {
                bool isDateValid = DateTime.TryParseExact(importPurchase.Date, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime validDate);

                bool isValidPurchase = Enum.TryParse(typeof(PurchaseType), importPurchase.Type, out var validPurchase);

                var card = context.Cards.FirstOrDefault(c => c.Number == importPurchase.Card);

                var game = context.Games.FirstOrDefault(g => g.Name == importPurchase.Game);

                if (!IsValid(importPurchase) ||
                    isDateValid == false ||
                    isValidPurchase == false ||
                    card == null ||
                    game == null)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var currentPurchase = new Purchase
                {
                    Game       = game,
                    Type       = (PurchaseType)validPurchase,
                    ProductKey = importPurchase.ProductKey,
                    Card       = card,
                    Date       = validDate
                };

                purchases.Add(currentPurchase);

                sb.AppendLine($"Imported {currentPurchase.Game.Name} for {currentPurchase.Card.User.Username}");
            }

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

            return(sb.ToString().Trim());
        }
Beispiel #2
0
        public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType)
        {
            var purchaseType = Enum.Parse <PurchaseType>(storeType);

            var users = context.Users
                        .Where(u => u.Cards.Any(c => c.Purchases.Count > 0))
                        .ToList()
                        .Select(u => new UserOutputModel()
            {
                Username  = u.Username,
                Purchases = context
                            .Purchases
                            .Where(p => p.Card.User.Username == u.Username && p.Type == purchaseType)
                            .OrderBy(p => p.Date)
                            .ToList()
                            .Select(p => new PurchaseOutputModel()
                {
                    Card = p.Card.Number,
                    Cvc  = p.Card.Cvc,
                    Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
                    Game = new GameOutputModel()
                    {
                        Title = p.Game.Name,
                        Genre = p.Game.Genre.Name,
                        Price = p.Game.Price
                    }
                })
                            .ToList(),
                TotalSpent = context
                             .Purchases
                             .Where(p => p.Card.User.Username == u.Username &&
                                    p.Type == purchaseType)
                             .ToList()
                             .Sum(p => p.Game.Price)
            })
                        .Where(u => u.Purchases.Count > 0)
                        .OrderByDescending(u => u.TotalSpent)
                        .ThenBy(u => u.Username)
                        .ToList();

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

            return(result);
        }
Beispiel #3
0
        public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType)
        {
            PurchaseType purchaseTypeEnum = Enum.Parse <PurchaseType>(storeType);

            var users = context
                        .Users
                        .ToArray()
                        .Where(u => u.Cards.Any(c => c.Purchases.Any()))
                        .Select(u => new ExportUserDTO()
            {
                Username  = u.Username,
                Purchases = context
                            .Purchases
                            .ToArray()
                            .Where(p => p.Card.User.Username == u.Username && p.Type == purchaseTypeEnum)
                            .OrderBy(p => p.Date)
                            .Select(p => new ExportUserPurchaseDTO()
                {
                    CardNumber = p.Card.Number,
                    CardCvc    = p.Card.Cvc,
                    Date       = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
                    Game       = new ExportUserPurchaseGameDTO()
                    {
                        Name  = p.Game.Name,
                        Genre = p.Game.Genre.Name,
                        Price = p.Game.Price
                    }
                })
                            .ToArray(),
                TotalSpent = context
                             .Purchases
                             .ToArray()
                             .Where(p => p.Card.User.Username == u.Username && p.Type == purchaseTypeEnum)
                             .Sum(p => p.Game.Price)
            })
                        .Where(u => u.Purchases.Length > 0)
                        .OrderByDescending(u => u.TotalSpent)
                        .ThenBy(u => u.Username)
                        .ToArray();

            return(XMLConverter.Serialize(users, "Users"));
        }
Beispiel #4
0
        public static string ImportPurchases(VaporStoreDbContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var purchasesDtos = XMLConverter.Deserializer <ImportPurchaseDTO>(xmlString, "Purchases");

            var purchases = new List <Purchase>();

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

                object validPurchaseType;
                bool   isValidPurchaseType = Enum.TryParse(typeof(PurchaseType), purchaseDto.Type, out validPurchaseType);

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

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

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

                var card = context
                           .Cards
                           .FirstOrDefault(c => c.Number == purchaseDto.CardNumber);

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

                var game = context
                           .Games
                           .FirstOrDefault(g => g.Name == purchaseDto.GameTitle);

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

                var purchase = new Purchase()
                {
                    Game       = game,
                    Card       = card,
                    Date       = validDate,
                    ProductKey = purchaseDto.ProductKey,
                    Type       = (PurchaseType)validPurchaseType
                };

                purchases.Add(purchase);
                var gameName = purchase.Game.Name;
                var userName = purchase.Card.User.Username;
                sb.AppendLine(String.Format(SuccessfullyImportedPurchase, gameName, userName));
            }

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

            return(sb.ToString().TrimEnd());
        }