You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
4.3 KiB
192 lines
4.3 KiB
using System.Runtime.InteropServices.ComTypes;
|
|
using DreamsCaster.Helpers;
|
|
using DreamsCaster.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Console = System.Console;
|
|
|
|
namespace DreamsCaster.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class GameController : ControllerBase
|
|
{
|
|
public class PostRelease
|
|
{
|
|
public DateTime Release;
|
|
public Guid ConsoleId;
|
|
}
|
|
|
|
public class PostGame
|
|
{
|
|
public string Name;
|
|
public string Description;
|
|
public string CoverPhoto;
|
|
|
|
public List<PostRelease> Releases;
|
|
}
|
|
|
|
public class PatchGame
|
|
{
|
|
public Optional<string> Name;
|
|
public Optional<string> Description;
|
|
public Optional<string> CoverPhoto;
|
|
}
|
|
|
|
public AppDbContext Database { get; set; }
|
|
|
|
#region Game CRUD
|
|
|
|
[HttpGet]
|
|
public IActionResult Get([FromQuery] Guid gameId)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out Game game)) return NotFound();
|
|
|
|
return Ok(game);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Authorize]
|
|
public async Task<IActionResult> DeleteAsync([FromQuery] Guid gameId)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out var game)) return NotFound();
|
|
|
|
foreach (var release in game.Releases)
|
|
{
|
|
Database.Remove(release);
|
|
}
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
Database.Games.Remove(game);
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[HttpPatch]
|
|
[Authorize]
|
|
public async Task<IActionResult> PatchAsync([FromQuery] Guid gameId, [FromBody] PatchGame newData)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out Game game)) return NotFound();
|
|
|
|
if (newData.Name.HasValue) game.Name = newData.Name;
|
|
if (newData.Description.HasValue) game.Data.Description = newData.Description;
|
|
if (newData.CoverPhoto.HasValue) game.Data.CoverPhoto = newData.CoverPhoto;
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
return Ok(game);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<IActionResult> PostAsync([FromBody] PostGame newGame)
|
|
{
|
|
try
|
|
{
|
|
var game = Database.Games.Add(new Game()
|
|
{
|
|
Name = newGame.Name,
|
|
Data = new MetaData<Game>()
|
|
{
|
|
CoverPhoto = newGame.CoverPhoto,
|
|
Description = newGame.Description
|
|
}
|
|
});
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
foreach (var release in newGame.Releases)
|
|
{
|
|
if (!Database.Consoles.TryGet(release.ConsoleId, out var console)) continue;
|
|
|
|
game.Entity.Releases.Add(new Release()
|
|
{
|
|
ReleaseDate = release.Release,
|
|
Console = console
|
|
});
|
|
}
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
return Ok(game);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
#endregion Game CRUD
|
|
|
|
#region Releases CUD
|
|
|
|
[HttpPatch("{gameId:guid}/releases")]
|
|
[Authorize]
|
|
public async Task<IActionResult> PatchReleaseAsync([FromQuery] Guid gameId, [FromBody] PostRelease release)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out Game game)) return NotFound();
|
|
if (!Database.Consoles.TryGet(release.ConsoleId, out var console)) return NotFound();
|
|
|
|
try
|
|
{
|
|
game.Releases.Add(Release.New(release.Release, console));
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPut("{gameId:guid}/releases/{releaseId:guid}")]
|
|
[Authorize]
|
|
public async Task<IActionResult> PatchReleaseAsync([FromQuery] Guid gameId, [FromQuery] Guid releaseId, [FromBody] DateTime newReleaseDate)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out Game game)) return NotFound();
|
|
if (!game.Releases.TryGet(releaseId, out var release)) return NotFound();
|
|
|
|
try
|
|
{
|
|
release.ReleaseDate = newReleaseDate;
|
|
|
|
await Database.SaveChangesAsync();
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{gameId:guid}/releases/{releaseId:guid}")]
|
|
[Authorize]
|
|
public async Task<IActionResult> DeleteReleaseAsync([FromQuery] Guid gameId, [FromQuery] Guid releaseId)
|
|
{
|
|
if (!Database.Games.TryGet(gameId, out Game game)) return NotFound();
|
|
if (!game.Releases.TryGet(releaseId, out Release release)) return NotFound();
|
|
|
|
try
|
|
{
|
|
game.Releases.Remove(release);
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
#endregion Releases CUD
|
|
} |