Skip to content

Commit

Permalink
persisting sql database with delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaughnAnderson94 committed Jun 10, 2024
1 parent ead069a commit 862ab78
Show file tree
Hide file tree
Showing 163 changed files with 2,308 additions and 44 deletions.
73 changes: 39 additions & 34 deletions testapi/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using testapi.Data;
using testapi.Models;

namespace testapi.Controllers;

Expand All @@ -7,20 +10,21 @@ namespace testapi.Controllers;

public class GamesController : ControllerBase
{
private static List<Game> games;

// demo has this bellow methods I cant imagine why thoe order would matter though keep it in mind
public class Game{
public int id { get; set; }
public string? teamOneName { get; set; }
public string? teamTwoName { get; set; }
public int winner { get; set; }

}
List<Game> PopulateGames(){
return new List<Game>
{
new Game{

private readonly ILogger<GamesController> _logger;
private readonly GamesDbContext _context;

public GamesController(GamesDbContext context, ILogger<GamesController> logger)
{
_context = context;
_logger = logger;
if (!_context.Games.Any())
{
_context.Games.AddRange(new List<Game>
{
new Game{
id = 1,
teamOneName="London",
teamTwoName="Cardif",
Expand All @@ -38,34 +42,35 @@ List<Game> PopulateGames(){
teamTwoName="Manchester",
winner =1
},
};
}

private readonly ILogger<GamesController> _logger;

public GamesController(ILogger<GamesController> logger)
{
games = PopulateGames();
_logger = logger;
});
_context.SaveChanges();
}
}

[HttpGet(Name = "GetGames")]
public IEnumerable<Game> Get()
public async Task<ActionResult<IEnumerable<Game>>> GetGames()
{
return games;
return await _context.Games.ToListAsync();
}

[HttpDelete("{id}", Name = "DeleteEmployee")]
public IEnumerable<Game> Delete([FromRoute] int id)
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteGame(int id)
{
var game = games.FirstOrDefault(x => x.id == id);
if (game != null) games.Remove(game);
return games;
}
[HttpPost]
public IEnumerable<Game> AddEmployee([FromBody] Game game)
{
games.Add(game);
return games;
var game = await _context.Games.FindAsync(id);
if (game == null)
{
return NotFound();
}

_context.Games.Remove(game);
await _context.SaveChangesAsync();

return NoContent();
}
// [HttpPost]
// public IEnumerable<Game> AddEmployee([FromBody] Game game)
// {
// games.Add(game);
// return games;
// }
}
14 changes: 14 additions & 0 deletions testapi/Data/GamesDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using testapi.Models;

namespace testapi.Data
{
public class GamesDbContext : DbContext
{
public GamesDbContext(DbContextOptions<GamesDbContext> options) : base(options)
{
}

public DbSet<Game> Games { get; set; }
}
}
44 changes: 44 additions & 0 deletions testapi/Migrations/20240607110605_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions testapi/Migrations/20240607110605_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace testapi.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Games",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
teamOneName = table.Column<string>(type: "TEXT", nullable: true),
teamTwoName = table.Column<string>(type: "TEXT", nullable: true),
winner = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Games", x => x.id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Games");
}
}
}
41 changes: 41 additions & 0 deletions testapi/Migrations/GamesDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using testapi.Data;

#nullable disable

namespace testapi.Migrations
{
[DbContext(typeof(GamesDbContext))]
partial class GamesDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.6");

modelBuilder.Entity("testapi.Models.Game", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<string>("teamOneName")
.HasColumnType("TEXT");

b.Property<string>("teamTwoName")
.HasColumnType("TEXT");

b.Property<int>("winner")
.HasColumnType("INTEGER");

b.HasKey("id");

b.ToTable("Games");
});
#pragma warning restore 612, 618
}
}
}
5 changes: 5 additions & 0 deletions testapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Microsoft.EntityFrameworkCore;
using testapi.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand All @@ -6,6 +9,8 @@
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<GamesDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();

Expand Down
3 changes: 3 additions & 0 deletions testapi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=games.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
Binary file added testapi/bin/Debug/net8.0/Azure.Core.dll
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/Azure.Identity.dll
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/Humanizer.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/Mono.TextTemplating.dll
Binary file not shown.
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/SQLitePCLRaw.core.dll
Binary file not shown.
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/System.CodeDom.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testapi/bin/Debug/net8.0/System.Memory.Data.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions testapi/bin/Debug/net8.0/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=games.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 862ab78

Please sign in to comment.