public IActionResult Post(GameItem item)
        {
            //if (!ModelState.IsValid)return BadRequest("Not a valid model");
            //if (string.IsNullOrEmpty(item.Name))return BadRequest("No name is provided");
            //if (item == null)return BadRequest("No data is provided");
            //if ID already exists?
            if (item.Name.Length < 1 || item.Name == null)
            {
                return(BadRequest("Name has to be at least 1 letter."));
            }
            else if (item.MinPlayerCount == null || item.MaxPlayerCount == null)
            {
                return(BadRequest("MinPlayerCount/MaxPlayerCount cannot be empty"));
            }
            else if (item.MinPlayerCount > item.MaxPlayerCount)
            {
                return(BadRequest("MinPlayerCount has to be smaller than MaxPlayerCount/MaxPlayerCount has to be larger than MinPlayerCount."));
            }
            else if (item.GameModeItems.Count == 0 || item.GameModeItems == null)
            {
                return(BadRequest("Each game has to have at least one GameMode."));
            }
            else
            {
                var game = new Game
                {
                    Name           = item.Name,
                    MinPlayerCount = item.MinPlayerCount,
                    MaxPlayerCount = item.MaxPlayerCount,
                    GameModes      = new List <GameMode>()
                };

                _context.Games.Add(game);

                foreach (var gameMode in item.GameModeItems)
                {
                    if (_context.WinConditions.SingleOrDefault(w => w.ID == gameMode.WinConditionID) == null)
                    {
                        return(BadRequest("Cannot find WinConditionId"));
                    }
                    else
                    {
                        var gm = new GameMode
                        {
                            Name         = gameMode.Name,
                            Description  = gameMode.Description,
                            winCondition = _context.WinConditions.SingleOrDefault(w => w.ID == gameMode.WinConditionID)
                        };
                        game.GameModes.Add(gm);
                    }
                }
            }

            _context.SaveChanges();

            return(Ok(
                       new
                       { game = _context.Games.OrderByDescending(g => g.ID).FirstOrDefault(),
                         message = "Item is posted" }));
        }
Ejemplo n.º 2
0
        public IActionResult PostNewTournament(Tournament tournament, int TournamentTypeId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }
            var type = _Context.TournamentTypes.SingleOrDefault(tt => tt.ID == TournamentTypeId);

            if (type == null)
            {
                return(BadRequest("Tournament Type ID is not found"));
            }


            _Context.Tournaments.Add(new Tournament()
            {
                Name           = tournament.Name,
                StartDate      = tournament.StartDate,
                EndDate        = tournament.EndDate,
                tournamentType = type
            });;

            _Context.SaveChanges();



            return(Ok(
                       new
            {
                message = "OK"
            }));
        }
Ejemplo n.º 3
0
        public ActionResult Create([Bind(Include = "Nome,LoginUsuario,Senha,Email,Pefil,DataContratacao")] Usuario usuario)
        {
            if (ModelState.IsValid)
            {
                db.Usuarios.Add(usuario);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(usuario));
        }
Ejemplo n.º 4
0
        public static void Initialize(appContext context)
        {
            context.Database.EnsureCreated();

            if (context.Product.Any())
            {
                return;
            }

            var ProductType = new ProductType[]
            {
                new ProductType {
                    Name = "Papel"
                },
                new ProductType {
                    Name = "Metal"
                },
                new ProductType {
                    Name = "Borracha"
                },
                new ProductType {
                    Name = "Pano"
                },
                new ProductType {
                    Name = "Eletronico"
                },
                new ProductType {
                    Name = "Vidro"
                }
            };

            foreach (var pType in ProductType)
            {
                context.ProductType.Add(pType);
            }
            context.SaveChanges();

            var product = new Product[]
            {
                new Product {
                    Name = "Produto Teste", Amount = 10, Value = 15, ProductType = ProductType[0]
                },
            };

            foreach (var prod in product)
            {
                context.Product.Add(prod);
            }
            context.SaveChanges();
        }
        public ActionResult Delete(int?id = null)
        {
            try
            {
                if (id == null)
                {
                    return(BadRequest("Game Match not found."));
                }
                else if (!ModelState.IsValid)
                {
                    return(BadRequest("This is not a valid model."));
                }
                //else if(_context.GameMatches.SingleOrDefault(g => g.ID == id)== null)
                //{
                //    return BadRequest("Game Match does not exist.");
                //}


                _context.GameMatches.Remove(_context.GameMatches.SingleOrDefault(g => g.ID == id));
                _context.SaveChanges();
                return(Ok("Game Match is deleted"));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
                //Value cannot be null. (Parameter 'entity')
            }
        }
Ejemplo n.º 6
0
 public void Save()
 {
     context.SaveChanges();
 }