Skip to content

Commit

Permalink
Merge pull request #11 from tacobell1896/feat/add-game
Browse files Browse the repository at this point in the history
Add game entity
  • Loading branch information
tacobell1896 authored May 10, 2024
2 parents 556a7ca + 18e26fc commit c276842
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 108 deletions.
133 changes: 133 additions & 0 deletions src/Controllers/SavePointGamesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SavePointAPI.Models;

namespace SavePointAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SavePointGamesController : ControllerBase
{
private readonly SavePointContext _context;

public SavePointGamesController(SavePointContext context)
{
_context = context;
}

// GET: api/SavePointGames
[HttpGet]
public async Task<ActionResult<IEnumerable<SavePointGame>>> GetSavePointGames()
{
// TODO: Return the list of notes associated with the game
return await _context.SavePointGames.ToListAsync();
}

// GET: api/SavePointGames/5
[HttpGet("{id}")]
public async Task<ActionResult<SavePointGameDTO>> GetSavePointGame(int id)
{
var savePointGame = await _context.SavePointGames.FindAsync(id);

if (savePointGame == null)
{
return NotFound();
}

return SavePointGameToDTO(savePointGame);
}

// PUT: api/SavePointGames/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutSavePointGame(int id, SavePointGameDTO savePointGameDTO)
{
if (id != savePointGameDTO.SavePointGameId)
{
return BadRequest();
}

_context.Entry(savePointGameDTO).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SavePointGameExists(id))
{
return NotFound();
}
else
{
throw;
}
}

return NoContent();
}

// POST: api/SavePointGames
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<SavePointGame>> PostSavePointGame(SavePointGameDTO savePointGame)
{
var Game = new SavePointGame
{
GameName = savePointGame.GameName,
GameConsole = savePointGame.GameConsole,
GameGenre = savePointGame.GameGenre,
GameDeveloper = savePointGame.GameDeveloper,
GamePublisher = savePointGame.GamePublisher,
GameReleaseDate = savePointGame.GameReleaseDate,
GameDescription = savePointGame.GameDescription,
};
_context.SavePointGames.Add(Game);
await _context.SaveChangesAsync();

return CreatedAtAction(nameof(PostSavePointGame), new { id = savePointGame.SavePointGameId }, savePointGame);
}

// DELETE: api/SavePointGames/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteSavePointGame(int id)
{
var savePointGame = await _context.SavePointGames.FindAsync(id);
if (savePointGame == null)
{
return NotFound();
}

_context.SavePointGames.Remove(savePointGame);
await _context.SaveChangesAsync();

return NoContent();
}

private bool SavePointGameExists(int id)
{
return _context.SavePointGames.Any(e => e.SavePointGameId == id);
}

private SavePointGameDTO SavePointGameToDTO(SavePointGame savePointGame) =>
new SavePointGameDTO
{
SavePointGameId = savePointGame.SavePointGameId,
GameName = savePointGame.GameName,
GameConsole = savePointGame.GameConsole,
GameGenre = savePointGame.GameGenre,
GameDeveloper = savePointGame.GameDeveloper,
GamePublisher = savePointGame.GamePublisher,
GameReleaseDate = savePointGame.GameReleaseDate,
GameDescription = savePointGame.GameDescription,
GameRating = savePointGame.GameRating,
GameImage = savePointGame.GameImage
};
}
}
16 changes: 10 additions & 6 deletions src/Controllers/SavePointNotesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public SavePointNotesController(SavePointContext context)
[HttpGet]
public async Task<ActionResult<IEnumerable<SavePointNoteDTO>>> GetSavePointNotes()
{
// TODO: Return the Game Object with the SavePointNote
return await
_context.SavePointNotes
.Select(x => SavePointNoteToDTO(x))
Expand All @@ -47,9 +48,9 @@ public async Task<ActionResult<SavePointNoteDTO>> GetSavePointNote(int id)
// PUT: api/SavePointNotes/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutSavePointNote(int id, SavePointNoteDTO savePointDTO)
public async Task<IActionResult> PutSavePointNote(int id, SavePointNoteDTO savePointNoteDTO)
{
if (id != savePointDTO.SavePointNoteId)
if (id != savePointNoteDTO.SavePointNoteId)
{
return BadRequest();
}
Expand All @@ -60,8 +61,9 @@ public async Task<IActionResult> PutSavePointNote(int id, SavePointNoteDTO saveP
return NotFound();
}

savePointNote.Note = savePointDTO.Note;
savePointNote.GameName = savePointDTO.GameName;
savePointNote.Note = savePointNoteDTO.Note;
savePointNote.NoteDate = savePointNoteDTO.NoteDate;
savePointNote.SavePointGameId = savePointNoteDTO.SavePointGameId;

try
{
Expand All @@ -83,7 +85,8 @@ public async Task<ActionResult<SavePointNote>> PostSavePointNote(SavePointNoteDT
var savePointNote = new SavePointNote
{
Note = savePointNoteDTO.Note,
GameName = savePointNoteDTO.GameName
NoteDate = savePointNoteDTO.NoteDate,
SavePointGameId = savePointNoteDTO.SavePointGameId
};
_context.SavePointNotes.Add(savePointNote);
await _context.SaveChangesAsync();
Expand Down Expand Up @@ -119,7 +122,8 @@ private static SavePointNoteDTO SavePointNoteToDTO(SavePointNote savePointNote)
{
SavePointNoteId = savePointNote.SavePointNoteId,
Note = savePointNote.Note,
GameName = savePointNote.GameName
NoteDate = savePointNote.NoteDate,
SavePointGameId = savePointNote.SavePointGameId
};
}
}
14 changes: 14 additions & 0 deletions src/Models/SavePointContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@ public SavePointContext(DbContextOptions<SavePointContext> options)
}

public DbSet<SavePointNote> SavePointNotes { get; set; }
public DbSet<SavePointGame> SavePointGames { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SavePointGame>()
.HasMany(g => g.SavePointNotes)
.WithOne(n => n.SavePointGame!)
.HasForeignKey(n => n.SavePointGameId);

modelBuilder.Entity<SavePointNote>()
.HasOne(n => n.SavePointGame)
.WithMany(g => g.SavePointNotes!)
.HasForeignKey(n => n.SavePointGameId);
}
}
}
17 changes: 17 additions & 0 deletions src/Models/SavePointGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SavePointAPI.Models
{
public class SavePointGame
{
public int SavePointGameId { get; set; }
public string? GameName { get; set; }
public string? GameConsole { get; set; }
public string? GameGenre { get; set; }
public string? GameDeveloper { get; set; }
public string? GamePublisher { get; set; }
public string? GameReleaseDate { get; set; }
public string? GameDescription { get; set; }
public string? GameRating { get; set; }
public string? GameImage { get; set; }
public ICollection<SavePointNote>? SavePointNotes { get; }
}
}
17 changes: 17 additions & 0 deletions src/Models/SavePointGameDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

namespace SavePointAPI.Models
{
public class SavePointGameDTO
{
public int SavePointGameId { get; set; }
public string? GameName { get; set; }
public string? GameConsole { get; set; }
public string? GameGenre { get; set; }
public string? GameDeveloper { get; set; }
public string? GamePublisher { get; set; }
public string? GameReleaseDate { get; set; }
public string? GameDescription { get; set; }
public string? GameRating { get; set; }
public string? GameImage { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Models/SavePointNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class SavePointNote
{
public int SavePointNoteId { get; set; }
public string? Note { get; set; }
public string? GameName { get; set; }
public DateOnly NoteDate { get; set; }
public int SavePointGameId { get; set; }
public SavePointGame? SavePointGame { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Models/SavePointNoteDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class SavePointNoteDTO
{
public int SavePointNoteId { get; set; }
public string? Note { get; set; }
public string? GameName { get; set; }
public DateOnly NoteDate { get; set; }
public int SavePointGameId { get; set; }
}
2 changes: 1 addition & 1 deletion src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Add services to the container.

builder.Services.AddControllers();
var connectionString = builder.Configuration.GetConnectionString("SavePoint");
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<SavePointContext>(options =>
options.UseNpgsql(connectionString));

Expand Down
6 changes: 5 additions & 1 deletion src/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=SavePoint;Username=postgres;Password=39^K5uQy4iPZStxGSg"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
},
"AllowedHosts": "*"
}
2 changes: 1 addition & 1 deletion src/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=SavePoint;Username=postgres;Password=39^K5uQy4iPZStxGSg"
"DefaultConnection": "Host=db;Port=5432;Database=SavePoint;Username=postgres;Password=39^K5uQy4iPZStxGSg"
},
"Logging": {
"LogLevel": {
Expand Down
Loading

0 comments on commit c276842

Please sign in to comment.