Files
gameauthapi/Controllers/SpawnableController.cs

77 lines
2.2 KiB
C#

using System.Threading.Tasks;
using GameAuthApi.Models;
using GameAuthApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace GameAuthApi.Controllers;
[ApiController]
[Route("api/[controller]")]
public class SpawnableController : ControllerBase
{
private readonly SpawnableService _spawnableService;
public SpawnableController(SpawnableService spawnableService)
{
_spawnableService = spawnableService;
}
[HttpPost]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> Create([FromBody] CreateSpawnableRequest req)
{
var spawnable = new Spawnable
{
Name = req.Name.ToLower(),
SpawnRate = req.SpawnRate,
CollectAction = req.CollectAction,
CollectResults = req.CollectResults,
ConsumeOnSuccess = req.ConsumeOnSuccess,
Uses = req.Uses
};
await _spawnableService.CreateAsync(spawnable);
return Ok(spawnable);
}
[HttpPut("{id}")]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> Update(string id, [FromBody] UpdateSpawnableRequest req)
{
var spawnable = await _spawnableService.GetByIdAsync(id);
if (spawnable == null)
return NotFound("Spawnable not found.");
spawnable.Name = req.Name.ToLower();
spawnable.SpawnRate = req.SpawnRate;
spawnable.CollectAction = req.CollectAction;
spawnable.CollectResults = req.CollectResults;
spawnable.ConsumeOnSuccess = req.ConsumeOnSuccess;
spawnable.Uses = req.Uses;
await _spawnableService.UpdateAsync(spawnable);
return Ok(spawnable);
}
[HttpGet]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> GetAll()
{
var all = await _spawnableService.GetAllAsync();
return Ok(all);
}
[HttpDelete("{id}")]
[Authorize(Roles = "SUPER")]
public async Task<IActionResult> Delete(string id)
{
var spawnable = await _spawnableService.GetByIdAsync(id);
if (spawnable == null)
return NotFound("Spawnable not found.");
await _spawnableService.DeleteAsync(id);
return Ok($"Spawnable '{spawnable.Name}' deleted.");
}
}