Create HTTP REST API for Game Library
This commit is contained in:
commit
930a45c584
69
Controllers/GamesController.cs
Normal file
69
Controllers/GamesController.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using GameLibrary.Models;
|
||||
using GameLibrary.Repositories;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameLibrary.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class GamesController : ControllerBase
|
||||
{
|
||||
private readonly IGameRepository _gameRepository;
|
||||
|
||||
public GamesController(IGameRepository gameRepository)
|
||||
{
|
||||
_gameRepository = gameRepository;
|
||||
}
|
||||
|
||||
// GET: api/games
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Game>>> GetGames(string genre = null)
|
||||
{
|
||||
var games = await _gameRepository.GetAllGames(genre);
|
||||
return Ok(games);
|
||||
}
|
||||
|
||||
// GET: api/games/{id}
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Game>> GetGame(int id)
|
||||
{
|
||||
var game = await _gameRepository.GetGameById(id);
|
||||
if (game == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(game);
|
||||
}
|
||||
|
||||
// POST: api/games
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Game>> CreateGame(Game game)
|
||||
{
|
||||
await _gameRepository.CreateGame(game);
|
||||
return CreatedAtAction(nameof(GetGame), new { id = game.Id }, game);
|
||||
}
|
||||
|
||||
// PUT: api/games/{id}
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateGame(int id, Game game)
|
||||
{
|
||||
if (id != game.Id)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
await _gameRepository.UpdateGame(game);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// DELETE: api/games/{id}
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteGame(int id)
|
||||
{
|
||||
await _gameRepository.DeleteGame(id);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
24
Data/GameContext.cs
Normal file
24
Data/GameContext.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using GameLibrary.Models;
|
||||
|
||||
namespace GameLibrary.Data
|
||||
{
|
||||
public class GameContext : DbContext
|
||||
{
|
||||
protected readonly IConfiguration Configuration;
|
||||
public GameContext(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
{
|
||||
// connect to sqlite database
|
||||
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
|
||||
}
|
||||
|
||||
public DbSet<Game> Games { get; set; }
|
||||
}
|
||||
}
|
13
GameLibrary.csproj
Normal file
13
GameLibrary.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
25
GameLibrary.sln
Normal file
25
GameLibrary.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.11.35312.102
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameLibrary", "GameLibrary.csproj", "{A4C8B6FE-20B5-4304-A80F-E098461F1138}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A4C8B6FE-20B5-4304-A80F-E098461F1138}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A4C8B6FE-20B5-4304-A80F-E098461F1138}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A4C8B6FE-20B5-4304-A80F-E098461F1138}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A4C8B6FE-20B5-4304-A80F-E098461F1138}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {5429A3BB-08B5-4F88-90AF-B686275A5080}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
12
Models/Game.cs
Normal file
12
Models/Game.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GameLibrary.Models
|
||||
{
|
||||
public class Game
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Developer { get; set; }
|
||||
public List<string> Genres { get; set; }
|
||||
}
|
||||
}
|
49
Program.cs
Normal file
49
Program.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using GameLibrary.Data;
|
||||
using GameLibrary.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GameLibrary
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddDbContext<GameContext>();
|
||||
|
||||
builder.Services.AddScoped<IGameRepository, GameRepository>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowAllOrigins",
|
||||
builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseCors("AllowAllOrigins");
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var dbContext = scope.ServiceProvider.GetRequiredService<GameContext>();
|
||||
dbContext.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
58
Repositories/GameRepository.cs
Normal file
58
Repositories/GameRepository.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using GameLibrary.Data;
|
||||
using GameLibrary.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameLibrary.Repositories
|
||||
{
|
||||
public class GameRepository : IGameRepository
|
||||
{
|
||||
private readonly GameContext _context;
|
||||
|
||||
public GameRepository(GameContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Game>> GetAllGames(string genre = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(genre))
|
||||
{
|
||||
return await _context.Games.ToListAsync();
|
||||
}
|
||||
|
||||
return await _context.Games
|
||||
.Where(g => g.Genres.Contains(genre))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Game> GetGameById(int id)
|
||||
{
|
||||
return await _context.Games.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task CreateGame(Game game)
|
||||
{
|
||||
_context.Games.Add(game);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateGame(Game game)
|
||||
{
|
||||
_context.Entry(game).State = EntityState.Modified;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteGame(int id)
|
||||
{
|
||||
var game = await _context.Games.FindAsync(id);
|
||||
if (game != null)
|
||||
{
|
||||
_context.Games.Remove(game);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
Repositories/IGameRepository.cs
Normal file
15
Repositories/IGameRepository.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using GameLibrary.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameLibrary.Repositories
|
||||
{
|
||||
public interface IGameRepository
|
||||
{
|
||||
Task<IEnumerable<Game>> GetAllGames(string genre = null);
|
||||
Task<Game> GetGameById(int id);
|
||||
Task CreateGame(Game game);
|
||||
Task UpdateGame(Game game);
|
||||
Task DeleteGame(int id);
|
||||
}
|
||||
}
|
8
appsettings.Development.json
Normal file
8
appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
12
appsettings.json
Normal file
12
appsettings.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=GameLibrary.db"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Loading…
Reference in New Issue
Block a user