public IActionResult Create([FromBody] Bounty bounty)
        {
            // Check that serialization was successful
            if (bounty == null)
            {
                return(BadRequest());
            }

            // Check if the client passed an Id
            if (bounty.Id > 0)
            {
                ModelState.AddModelError("Id", "Model Ids are automatically generated. Do not pass an Id on creation.");
            }

            // Validate inputs
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Add bounty
            _context.Bounties.Add(bounty);
            _context.SaveChanges();

            return(CreatedAtRoute("GetBounty", new { id = bounty.Id }, bounty));
        }
Exemplo n.º 2
0
        private static void CreateTestData(BountyContext context)
        {
            Bounty[] testData =
            {
                new Bounty
                {
                    Name        = "Filthy Hands Floyd",
                    Description = "He betrayed his partner and took off with all his loot. Filth ain't the only thing he's got on his hands.",
                    AliveReward = 900.00m,
                    DeadReward  = 300.00m,
                    Captured    = false
                },
                new Bounty
                {
                    Name        = "Boilz Booty",
                    Description = "The value on this guy has shot up lately. Bring him back alive and we'll pay you handsomely.",
                    AliveReward = 750.50m,
                    DeadReward  = 230.15m,
                    Captured    = true
                },
                new Bounty
                {
                    Name        = "Packrat Polluka",
                    Description = "He's packin' a whole load of cash he stole from the city bank. Find him and bring him back...dead or alive!",
                    AliveReward = 1000.00m,
                    DeadReward  = 100.00m,
                    Captured    = false
                },
                new Bounty
                {
                    Name        = "Dangerous Dave",
                    Description = "Danger is his middle name and he's gonna make sure everyone knows it until he's caught!",
                    AliveReward = 550.00m,
                    DeadReward  = 510.10m,
                    Captured    = true
                },
                new Bounty
                {
                    Name        = "Long John Silver",
                    Description = "Ol' Long John probably ain't got much life left in him. That doesn't mean he gets to spend the rest of it robbin' trains.",
                    AliveReward = 350.00m,
                    DeadReward  = 300.00m,
                    Captured    = false
                },
                new Bounty
                {
                    Name        = "Red Sam",
                    Description = "He's scheduled to be executed once caught. Bring him back dead so we can save ourselves the time.",
                    AliveReward = 300.00m,
                    DeadReward  = 1000.00m,
                    Captured    = false
                },
            };

            context.AddRange(testData);
            context.SaveChanges();
        }