Can anyone guide me why I am only able to update the default state after every crud call and why the controller object is reinitialized every time before the call.
Here is my code:
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
PlayerController:
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Unity_RestApi_Custom.Contracts;
namespace Unity_RestApi_Custom.Controllers
{
[Route("api/[controller]")]
public class PlayersController : Controller
{
int counter = 0;
private static List<Player> players;
public PlayersController(ILogger<PlayersController> logger)
{
//_logger = logger;
players = new List<Player>();
players.Add(new Player(1, "Name 1 Text", 1));
players.Add(new Player(2, "Name 2 Text", 2));
players.Add(new Player(3, "Name 3 Text", 3));
System.Diagnostics.Debug.WriteLine("Testing Constructor");
}
/*
[HttpGet(Name = "Players")]
public IEnumerable<Player> Get()
{
Player player = players.Single(p => p.Id == id);
return Enumerable.Range(0, players.Count).Select(index => new Player
{
Id = players[index].Id ,
FullName = players[index].FullName,
Score = players[index].Score
})
.ToArray();;
yield return player;
}*/
[HttpGet]
public JsonResult Get()
{
System.Diagnostics.Debug.WriteLine("Players Count == " + players.Count);
return Json(players);
//return new JsonResult(Random.Shared.Next());
}
[HttpGet("{id}")]
public JsonResult Get(int id)
{
Player player = players.Single(p => p.Id == id);
return Json(player);
}
[HttpPost]
public JsonResult Post([FromBody] Player newPlayer)
{
players.Add(newPlayer);
return Json(players);
}
[HttpPut("{id}")]
public JsonResult Put(int id,[FromBody] int newScore)
{
/*Player player = players.Single(player => player.Id == id);
player.Score = newScore;
return Json(player);*/
for (int i = 0; i < players.Count; i++)
{
if (players[i].Id == id)
{
players[i].Score = newScore;
}
}
return Json(players);
}
[HttpDelete("{id}")]
public JsonResult Delete(int id)
{
List<Player> playersLocal = players;
Player player = playersLocal.Single(player => player.Id == id);
playersLocal.Remove(player);
players = playersLocal;
//System.Diagnostics.Debug.WriteLine("Players Count == "+ playersLocal.Count);
return Json(players);
}
}
}
and Player model class:
using System;
using Newtonsoft.Json;
namespace Unity_RestApi_Custom.Contracts
{
[JsonObject,Serializable]
public class Player
{
public int Id { get; set; }
public string FullName { get; set; }
public int Score { get; set; }
public Player(int Id,string FullName,int Score)
{
this.Id = Id;
this.FullName = FullName;
this.Score= Score;
}
}
}
Basically I am a newbie to Web API development therefore I don't know which direction to go.