Exemplo n.º 1
0
        public GameModel(Game game)
        {
            this.AddedBy = "ZVGQ"; // TODO: add property to denote who added it
            this.AddedOn = game.AddedOn.ToShortDateString();
            this.Id = game.GameId;
            this.ReleaseYear = game.ReleaseYear;
            this.Title = game.Title;
            this.TitleMedia = "/Content/images/defaultgamepicture.png";
            this.UpdatedBy = "ZVGQ";
            this.UpdatedOn = game.UpdatedOn.ToShortDateString();

            this.ActionUrl = this.GetActionUrl();
        }
Exemplo n.º 2
0
        public Game AddGame(Game newGame, int[] platforms, int[] genres, int userId, byte[] newTitleImageData)
        {
            Game game = context.Games.Create();

            try
            {
                if (game != null)
                {
                    game.Description = newGame.Description;
                    game.Genres = genreRepo.GetGenres(genres);
                    game.Platforms = platformRepo.GetPlatforms(platforms);
                    game.RegionId = newGame.RegionId;
                    game.ReleaseYear = newGame.ReleaseYear;
                    game.ShortName = GameRepository.CreateShortNameFromTitle(newGame.Title);
                    game.Title = newGame.Title;
                    game.UpdatedById = userId;
                    game.UpdatedOn = DateTime.UtcNow;

                    if (newTitleImageData != null && newTitleImageData.Length > 0)
                    {
                        game.TitleMediaItem = mediaRepo.AddMediaItem(newTitleImageData, "title.png", game.ShortName);
                    }

                    context.Games.Add(game);
                    context.SaveChanges();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                // TODO: Handle Exception
                foreach (var eve in ex.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }

            return game;
        }
Exemplo n.º 3
0
 public GameViewModel(Game sourceItem)
 {
     this.Id = sourceItem.GameId;
     this.Title = sourceItem.Title;
     this.TitleMediaUrl = (sourceItem.TitleMediaItem != null) ? sourceItem.TitleMediaItem.FileUrl : "";
 }
Exemplo n.º 4
0
        private Game MigrateTitleImage(Game game, DirectoryInfo gameDir, ZanyContext ctx)
        {
            FileInfo titleFile = null;

            List<FileInfo> files = gameDir.GetFiles().ToList();
            titleFile = files.Where(x => x.Name.Contains("title"))
                            .FirstOrDefault();
            if (titleFile == null)
            {
                titleFile = files.Where(x => x.Name.Contains("logo"))
                                .FirstOrDefault();
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer imageContainer = blobClient.GetContainerReference("media");
            imageContainer.CreateIfNotExists();

            BlobContainerPermissions permissions = new BlobContainerPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            imageContainer.SetPermissions(permissions);

            string blockBlobRef = game.ShortName + "/" + "title" + titleFile.Extension;
            CloudBlockBlob blockBlob = imageContainer.GetBlockBlobReference(blockBlobRef);

            using (var fileStream = System.IO.File.OpenRead(titleFile.FullName))
            {
                blockBlob.UploadFromStream(fileStream);
            }

            this.IntializeGameData(game, ctx);

            MediaItem gameTitleItem = new MediaItem();
            gameTitleItem.MediaType = MediaTypes.Image;
            gameTitleItem.FileUrl = blockBlob.Uri.ToString();
            game.TitleMediaItem = ctx.MediaItems.Add(gameTitleItem);

            return game;
        }
Exemplo n.º 5
0
        private HomeViewModel MigrateGames()
        {
            HomeViewModel model = new HomeViewModel();
            model.successfulTransfers = new List<Game>();
            model.failedTransfers = new List<Game>();

            Game currentGame = null;

            using (ZanyContext ctx = new ZanyContext())
            {
                foreach (var item in gameDirectories)
                {
                    currentGame = new Game();

                    currentGame.Title = item.Name;
                    currentGame.ShortName = item.Name;

                    try
                    {
                        currentGame = this.MigrateTitleImage(currentGame, item, ctx);
                        ctx.Games.Add(currentGame);
                        ctx.SaveChanges();

                        model.successfulTransfers.Add(currentGame);
                    }
                    catch (Exception ex)
                    {
                        model.failedTransfers.Add(currentGame);
                    }
                }

                ctx.SaveChanges();

            }

            return model;
        }
Exemplo n.º 6
0
        private void MigrateGame(string sourceDir, ZanyContext ctx)
        {
            DirectoryInfo gameDirectory = new DirectoryInfo(sourceDir);

            // create game entity
            Game game       = new Game();
            game.Title      = gameDirectory.Name;
            game.ShortName  = gameDirectory.Name;
            game            = this.IntializeGameData(game, ctx);

            // get file
            FileInfo fileInfo = this.GetTitleFile(gameDirectory);

            // add title media item
            string titleMediaUri = this.UploadMediaFile(fileInfo, "title", game.Title);
            MediaItem mediaItem = new MediaItem();
            mediaItem.MediaType = MediaTypes.Image;
            mediaItem.FileUrl = titleMediaUri;

            // get quotes
            List<Quote> gameQuotes = this.GetGameQuotes(gameDirectory, fileInfo, game.Title);

            // add entities
            game.TitleMediaItem = ctx.MediaItems.Add(mediaItem);
            foreach (var quote in gameQuotes)
            {
                game.Quotes.Add(quote);
            }
            ctx.Games.Add(game);

            // save context
            ctx.SaveChanges();
        }
Exemplo n.º 7
0
        private Game IntializeGameData(Game game, ZanyContext ctx)
        {
            if (_region == null)
            {
                _region = ctx.Regions.FirstOrDefault();
            }
            game.Region = _region;

            return game;
        }
Exemplo n.º 8
0
        public Game UpdateGame(Game game, int[] platforms, int[] genres, int userId, byte[] newTitleImage)
        {
            Game updateGame = this.GetGame(game.GameId);

            try
            {
                if (updateGame != null)
                {
                    updateGame.Description = game.Description;
                    updateGame.RegionId = game.RegionId;
                    updateGame.ReleaseYear = game.ReleaseYear;
                    updateGame.ShortName = GameRepository.CreateShortNameFromTitle(game.Title);
                    updateGame.Title = game.Title;
                    updateGame.UpdatedById = userId;
                    updateGame.UpdatedOn = DateTime.UtcNow;

                    updateGame.Genres.Clear();
                    foreach (var g in genreRepo.GetGenres(genres))
                    {
                        updateGame.Genres.Add(g);
                    }

                    updateGame.Platforms.Clear();
                    foreach (var p in platformRepo.GetPlatforms(platforms))
                    {
                        updateGame.Platforms.Add(p);
                    }

                    if (newTitleImage != null && newTitleImage.Length > 0)
                    {
                        if (game.TitleMediaItemId.HasValue)
                        {
                            updateGame.TitleMediaItem = mediaRepo.UpdateMediaItem(game.TitleMediaItemId.Value, newTitleImage);
                        }
                        else
                        {
                            updateGame.TitleMediaItem = mediaRepo.AddMediaItem(newTitleImage, "title.png", game.ShortName);
                        }
                    }

                    context.SaveChanges();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                // TODO: Handle Exception
                foreach (var eve in ex.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }

            return updateGame;
        }