added games controller

This commit is contained in:
Ashraful Haque 2021-02-18 14:10:39 +00:00
parent 231f86f64d
commit 15a1af43ee
3 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using TestApplication.Models;
namespace TestApplication.Controllers
{
[ApiController]
[Route("[controller]")]
public class GamesController : ControllerBase
{
public static List<Games> GetGames()
{
List<Games> games = new List<Games>();
games.Add(new Games(){Id = 1, Name = "Game 1", Price = 10});
games.Add(new Games(){Id = 2, Name = "Game 2", Price = 15});
games.Add(new Games(){Id = 3, Name = "Game 3", Price = 20});
games.Add(new Games(){Id = 4, Name = "Game 4", Price = 25});
games.Add(new Games(){Id = 5, Name = "Game 5", Price = 30});
return games;
}
[HttpGet]
public IEnumerable<Games> GetGames_List()
{
return GetGames();
}
[HttpGet("{id}")]
public ActionResult<Games> GetGames_ById(int id)
{
var games = GetGames().Find(x => x.Id == id);
if(games != null)
{
return games;
}
else
{
return NotFound();
}
}
}
}

View File

@ -36,5 +36,24 @@ namespace TestApplication.Controllers
}) })
.ToArray(); .ToArray();
} }
[HttpGet("list")]
public IEnumerable<string> Get_List()
{
return Summaries.ToList();
}
[HttpGet("list/{id}")]
public string Get_list_byId(int id)
{
if(id < 0 || id > Summaries.Length - 1)
{
return new string("not found");
}
else
{
return Summaries[id];
}
}
} }
} }

9
Models/Games.cs Normal file
View File

@ -0,0 +1,9 @@
namespace TestApplication.Models
{
public class Games
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
}