diff --git a/Server/Controllers/ScoreboardController.cs b/Server/Controllers/ScoreboardController.cs new file mode 100644 index 0000000..be50283 --- /dev/null +++ b/Server/Controllers/ScoreboardController.cs @@ -0,0 +1,82 @@ +using DatabaseModels; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Server.Data; + +namespace Server.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class ScoreboardController : ControllerBase +{ + private readonly ServerDbContext _context; + + public ScoreboardController(ServerDbContext context) + { + _context = context; + } + + // GET: api/Scoreboard + [HttpGet] + public async Task> Get() + { + return await _context.Scoreboard.ToArrayAsync(); + } + + // GET: api/Scoreboard/cuqmbr + [HttpGet("{username}", Name = "Get")] + public async Task> Get(string username) + { + var sbRecord = await _context.Scoreboard.FirstOrDefaultAsync(sbr => sbr.Username == username); + + if (sbRecord == null) + { + return NotFound(); + } + + return sbRecord; + } + + // POST: api/Scoreboard + [HttpPost] + public async Task Post([FromBody] ScoreboardRecord sbRecord) + { + await _context.AddAsync(sbRecord); + await _context.SaveChangesAsync(); + + return CreatedAtAction(nameof(Get), new {sbRecord.Username}, sbRecord); + } + + // PUT: api/Scoreboard/cuqmbr + [HttpPut("{username}")] + public async Task Put(string username, [FromBody] ScoreboardRecord sbRecord) + { + if (username != sbRecord.Username) + { + return BadRequest(); + } + + _context.Entry(sbRecord).State = EntityState.Modified; + + try + { + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!await ScoreboardRecordExists(sbRecord.Username)) + { + return NotFound(); + } + + throw; + } + + return NoContent(); + } + + private async Task ScoreboardRecordExists(string username) + { + return await _context.Scoreboard.AnyAsync(sbr => sbr.Username == username); + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 43e88c6..81a472e 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -8,7 +8,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddDbContext(o => o.UseSqlite()); +builder.Services.AddDbContext(o => o.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"))); var app = builder.Build(); diff --git a/Server/Properties/launchSettings.json b/Server/Properties/launchSettings.json index f21c3a1..da63d20 100644 --- a/Server/Properties/launchSettings.json +++ b/Server/Properties/launchSettings.json @@ -13,7 +13,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "", "applicationUrl": "https://localhost:7248;http://localhost:5214", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" @@ -22,7 +22,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "swagger", + "launchUrl": "", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Server/appsettings.Development.json b/Server/appsettings.Development.json index 0c208ae..79f92cd 100644 --- a/Server/appsettings.Development.json +++ b/Server/appsettings.Development.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "DefaultConnection": "Filename=./Scoreboard-SQLite.db" + }, "Logging": { "LogLevel": { "Default": "Information",