Ejemplo n.º 1
0
        public ActionResult Create(Game game, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                db.Games.Add(game);

                if (!String.IsNullOrEmpty(collection["Genre(s)"]))
                {
                    string[] userGenreString;
                    if (collection["Genre(s)"].Contains(','))
                    {
                        userGenreString = collection["Genre(s)"].Split(',');
                    }
                    else
                    {
                        userGenreString    = new string[1];
                        userGenreString[0] = collection["Genre(s)"];
                    }


                    foreach (string item in userGenreString)
                    {
                        String myItem = item;
                        //System.Diagnostics.Debug.WriteLine(myItem);
                        if (!String.IsNullOrEmpty(myItem))
                        {
                            if (myItem[0] == ' ')
                            {
                                //remove inital spacing after comma (e.g. "Action, Adventure)
                                myItem = myItem.Substring(1);
                            }

                            var myGenreID = db.genres.FirstOrDefault(i => i.Genre1.ToLower() == myItem.ToLower());

                            if (myGenreID != null) //if genre is valid
                            {
                                gameGenre newGameGenre = new gameGenre();
                                newGameGenre.gameID  = game.ID;
                                newGameGenre.genreID = myGenreID.ID;
                                db.gameGenres.Add(newGameGenre);

                                //System.Diagnostics.Debug.WriteLine("Added " + myItem);
                            }
                        }
                    }
                }


                if (!String.IsNullOrEmpty(collection["Platform(s)"]))
                {
                    string[] userPlatformString;
                    if (collection["Platform(s)"].Contains(','))
                    {
                        userPlatformString = collection["Platform(s)"].Split(',');
                    }
                    else
                    {
                        userPlatformString    = new string[1];
                        userPlatformString[0] = collection["Platform(s)"];
                    }

                    foreach (string item in userPlatformString)
                    {
                        String myItem = item;
                        //System.Diagnostics.Debug.WriteLine(myItem);
                        if (!String.IsNullOrEmpty(myItem))
                        {
                            if (myItem[0] == ' ')
                            {
                                //remove inital spacing after comma (e.g. "Steam, Switch)
                                myItem = myItem.Substring(1);
                            }

                            switch (myItem.ToLower())
                            {
                            case "ps4":
                            case "playstation4":
                            case "playstation-4":
                            case "playstation_4":
                            case "playstation 4":
                                myItem = "Playstation 4";
                                break;

                            case "xb1":
                            case "xbox1":
                            case "xbone":
                            case "xbox-one":
                            case "xboxone":
                            case "xbox_one":
                            case "xbox one":
                                myItem = "Xbox One";
                                break;

                            case "nintendo switch":
                            case "switch":
                                myItem = "Switch";
                                break;

                            case "pc":
                            case "steam":
                                myItem = "Steam";
                                break;

                            default:
                                myItem = null;
                                break;
                            }

                            if (myItem != null)
                            {
                                var myPlatformID = db.platforms.FirstOrDefault(i => i.Platform1 == myItem);

                                gamePlatform newGamePlatform = new gamePlatform();
                                newGamePlatform.gameID     = game.ID;
                                newGamePlatform.platformID = myPlatformID.ID;
                                db.gamePlatforms.Add(newGamePlatform);
                                //System.Diagnostics.Debug.WriteLine("Added");
                            }
                        }
                    }
                }


                db.SaveChanges();
                return(RedirectToAction("index"));
            }
            return(View(game));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Game game, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                db.Entry(game).State = System.Data.Entity.EntityState.Modified;

                string[] userGenreTable    = convertGenreString(collection["Genre(s)"]);
                string[] userPlatformTable = convertPlatformString(collection["Platform(s)"]);

                var DBgenreTable = from g in db.gameGenres
                                   where g.gameID == game.ID
                                   select g;

                var DBplatformTable = from g in db.gamePlatforms
                                      where g.gameID == game.ID
                                      select g;

                foreach (var genre in db.genres)
                {
                    bool onUserString   = false;
                    bool onDBgenreTable = false;

                    genre myGenreID;

                    foreach (string item in userGenreTable)
                    {
                        myGenreID = db.genres.FirstOrDefault(i => i.Genre1 == item);

                        if (myGenreID != null) //if genre is valid
                        {
                            if (genre.ID == myGenreID.ID)
                            {
                                onUserString = true;
                                break;
                            }
                        }
                    }

                    foreach (var gameGenre in DBgenreTable)
                    {
                        if (genre.ID == gameGenre.genreID)
                        {
                            onDBgenreTable = true;
                            break;
                        }
                    }

                    if (onUserString)
                    {
                        //user wants to add this genre
                        if (onDBgenreTable)
                        {
                            //its already on the table -> do nothing
                        }
                        else
                        {
                            //not yet on table -> add genre
                            gameGenre newGameGenre = new gameGenre();
                            newGameGenre.gameID  = game.ID;
                            newGameGenre.genreID = genre.ID;
                            db.gameGenres.Add(newGameGenre);
                            //System.Diagnostics.Debug.WriteLine("Added");
                        }
                    }
                    else if (onDBgenreTable)
                    {
                        //on table but user has removed it from string -> remove genre
                        db.gameGenres.RemoveRange(db.gameGenres.Where(g => g.gameID == game.ID && g.genreID == genre.ID));
                    }
                }


                foreach (var platform in db.platforms)
                {
                    bool onUserString      = false;
                    bool onDBplatformTable = false;

                    platform myPlatformID;
                    foreach (string item in userPlatformTable)
                    {
                        myPlatformID = db.platforms.FirstOrDefault(i => i.Platform1 == item);

                        if (myPlatformID != null)
                        {
                            if (platform.ID == myPlatformID.ID)
                            {
                                onUserString = true;
                                break;
                            }
                        }
                    }

                    foreach (var gamePlatform in DBplatformTable)
                    {
                        if (platform.ID == gamePlatform.platformID)
                        {
                            onDBplatformTable = true;
                            break;
                        }
                    }

                    if (onUserString)
                    {
                        //user wants to add this platform
                        if (onDBplatformTable)
                        {
                            //Its already on the table -> do nothing
                        }
                        else
                        {
                            //Not yet on table -> add platform
                            gamePlatform newGamePlatform = new gamePlatform();
                            newGamePlatform.gameID     = game.ID;
                            newGamePlatform.platformID = platform.ID;
                            db.gamePlatforms.Add(newGamePlatform);
                        }
                    }
                    else if (onDBplatformTable)
                    {
                        //on table but user has removed it from string -> remove platform
                        db.gamePlatforms.RemoveRange(db.gamePlatforms.Where(g => g.gameID == game.ID && g.platformID == platform.ID));
                    }
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(game));
        }