C
C#2y ago
Makyō

ASP.NET Core | Issue with in-memory data removal

Hey there, I am currently trying to make a backend service for my chess game made in Next.js as an internship project. I am most likely struggling with some basic concept I can't dig up any info about. Regarding the main issue, I have made a controller that handles: A) GET request which returns the current chessboard state B) POST request which returns the default chessboard state C) POST request which returns the chessboard state after the piece moved D) DELETE request which returns the chessboard state after removing piece The problem occurs when I am trying to delete any piece using request D. The response that's being returned is correct, but right after when I try to fetch the chessboard with request A the seemingly deleted data is still present there.
9 Replies
Makyō
MakyōOP2y ago
public class ChessboardRepository : IChessboardRepository
{
private readonly List<Piece> _pieces = Chessboard.GetDefaultConfiguration().ToList();

public IQueryable<Piece> Pieces => _pieces.AsQueryable();

public IEnumerable<Piece> RestorePieces()
{
var configuration = Chessboard.GetDefaultConfiguration();

_pieces.Clear();
_pieces.AddRange(configuration);

return configuration;
}

public IEnumerable<Piece> RemovePiece(int id)
{
var piecesAfterDeletion = _pieces.Where(piece => piece.Id != id).ToList();

_pieces.Clear();
_pieces.AddRange(piecesAfterDeletion);

return piecesAfterDeletion;
}

public IEnumerable<Piece> MovePiece(int id, ref Square square)
{
var pieceToMove = _pieces.FirstOrDefault(piece => piece.Id == id)!;

pieceToMove.Position = square;

var piecesAfterDeletion = _pieces.Where(piece => piece.Id != id).ToList();

piecesAfterDeletion.Add(pieceToMove);

_pieces.Clear();
_pieces.AddRange(piecesAfterDeletion);

return piecesAfterDeletion;
}
}
public class ChessboardRepository : IChessboardRepository
{
private readonly List<Piece> _pieces = Chessboard.GetDefaultConfiguration().ToList();

public IQueryable<Piece> Pieces => _pieces.AsQueryable();

public IEnumerable<Piece> RestorePieces()
{
var configuration = Chessboard.GetDefaultConfiguration();

_pieces.Clear();
_pieces.AddRange(configuration);

return configuration;
}

public IEnumerable<Piece> RemovePiece(int id)
{
var piecesAfterDeletion = _pieces.Where(piece => piece.Id != id).ToList();

_pieces.Clear();
_pieces.AddRange(piecesAfterDeletion);

return piecesAfterDeletion;
}

public IEnumerable<Piece> MovePiece(int id, ref Square square)
{
var pieceToMove = _pieces.FirstOrDefault(piece => piece.Id == id)!;

pieceToMove.Position = square;

var piecesAfterDeletion = _pieces.Where(piece => piece.Id != id).ToList();

piecesAfterDeletion.Add(pieceToMove);

_pieces.Clear();
_pieces.AddRange(piecesAfterDeletion);

return piecesAfterDeletion;
}
}
[ApiController]
[Route("api/chessboard/")]
public class ChessboardController : ControllerBase
{
private readonly IChessboardRepository _repository;

public ChessboardController(IChessboardRepository repository)
{
_repository = repository;
}

[Produces("application/json")]
[HttpGet]
public IActionResult Get()
{
var pieces = _repository.Pieces;

return Ok(pieces);
}

[Produces("application/json")]
[HttpPost("restore")]
public IActionResult Restore()
{
var pieces = _repository.RestorePieces();

return Ok(pieces);
}

[Produces("application/json")]
[HttpPost("piece/{id:int}/move/")]
public IActionResult Post(int id, int x, int y)
{
var pieces = _repository.Pieces;

var pieceToMove = pieces.FirstOrDefault(piece => piece.Id == id);

if (pieceToMove is null)
{
return BadRequest("Piece of given id does not exist");
}

var square = Squares.GetSquare(x, y);

if (square is null)
{
return BadRequest("Square of given coordinates does not exist");
}

var piecesAfterMove = _repository.MovePiece(id, ref square);

return Ok(piecesAfterMove);
}

[Produces("application/json")]
[HttpDelete("piece/{id:int}/remove/")]
public IActionResult Delete(int id)
{
var pieces = _repository.Pieces;

var pieceToRemove = pieces.FirstOrDefault(piece => piece.Id == id);

if (pieceToRemove is null)
{
return BadRequest(new { error = "id not found" });
}

var piecesAfterRemoval = _repository.RemovePiece(id);

return Ok(piecesAfterRemoval);
}
}
[ApiController]
[Route("api/chessboard/")]
public class ChessboardController : ControllerBase
{
private readonly IChessboardRepository _repository;

public ChessboardController(IChessboardRepository repository)
{
_repository = repository;
}

[Produces("application/json")]
[HttpGet]
public IActionResult Get()
{
var pieces = _repository.Pieces;

return Ok(pieces);
}

[Produces("application/json")]
[HttpPost("restore")]
public IActionResult Restore()
{
var pieces = _repository.RestorePieces();

return Ok(pieces);
}

[Produces("application/json")]
[HttpPost("piece/{id:int}/move/")]
public IActionResult Post(int id, int x, int y)
{
var pieces = _repository.Pieces;

var pieceToMove = pieces.FirstOrDefault(piece => piece.Id == id);

if (pieceToMove is null)
{
return BadRequest("Piece of given id does not exist");
}

var square = Squares.GetSquare(x, y);

if (square is null)
{
return BadRequest("Square of given coordinates does not exist");
}

var piecesAfterMove = _repository.MovePiece(id, ref square);

return Ok(piecesAfterMove);
}

[Produces("application/json")]
[HttpDelete("piece/{id:int}/remove/")]
public IActionResult Delete(int id)
{
var pieces = _repository.Pieces;

var pieceToRemove = pieces.FirstOrDefault(piece => piece.Id == id);

if (pieceToRemove is null)
{
return BadRequest(new { error = "id not found" });
}

var piecesAfterRemoval = _repository.RemovePiece(id);

return Ok(piecesAfterRemoval);
}
}
Saber
Saber2y ago
if your repository is scoped, modifying _pieces isn't going to do anything useful because its going to be reset to the default configuration each request
Makyō
MakyōOP2y ago
Alright this actually might be the problem But how come the post requests behave as they are supposed to then It is currently bugging me
Saber
Saber2y ago
they "work" because they return the state after modifying it. But, if you posted something again, the previous post didn't actually happen because of the same issue.
Makyō
MakyōOP2y ago
Fair enough That's actually really informative Thank you Then I believe I am supposed to change UseScoped to UseTransient And I should be fine?
Saber
Saber2y ago
you can try it and see. Might be helpful to know the differences between the different registration types
Makyō
MakyōOP2y ago
I just read about them And it seems like it should update it ye, well it did not update it I have got no idea what to do then
Saber
Saber2y ago
i wonder if theres another registration type
Makyō
MakyōOP2y ago
AddSingleton apparently saves the data I suppose my issue's fixed then @Deluxe Anyways, thank you for your help I appreciate it a lot
Want results from more Discord servers?
Add your server