Previous part: Adding Objects to a Local Database in MAUI .NET 9 [GamesCatalog] - Part 10
Step 1. In GameRepo, add a function to retrieve a game from the local database using igdbId and another function to update the game's status.

Code
public async Task<GameDTO?> GetByIGDBIdAsync(int igdbId)
{
using var context = DbCtx.CreateDbContext();
return await context.Games.FirstOrDefaultAsync(g => g.IGDBId == igdbId);
}
public async Task UpdateStatusAsync(int id, DateTime updatedAt, GameStatus status, int? rate)
{
using var context = DbCtx.CreateDbContext();
await context.Games
.Where(x => x.Id == id)
.ExecuteUpdateAsync(y => y
.SetProperty(z => z.UpdatedAt, updatedAt)
.SetProperty(z => z.Status, status)
.SetProperty(z => z.Rate, rate));
}
Press [Ctrl + .] on GetByIGDBIdAsync and UpdateStatusAsync, then add them to the interface.
Step 2. In GameService, add both functions.

Code
public async Task<GameDTO?> GetByIGDBIdAsync(int igdbId) =>
await GameRepo.GetByIGDBIdAsync(igdbId);
public async Task UpdateStatusAsync(int id, GameStatus gameStatus, int? rate) =>
await GameRepo.UpdateStatusAsync(id, DateTime.Now, gameStatus, rate);
Press [Ctrl + .] on GetByIGDBIdAsync and UpdateStatusAsync, then add them to the interface.
Step 3. Add a variable for the local game ID.

Step 4. Still in AddGameVM, create a function to retrieve the game.
public async Task BuildGameStatus()
{
var gameDTO = await gameService.GetByIGDBIdAsync(int.Parse(IgdbId));
if (gameDTO is null)
return;
Id = gameDTO.Id;
switch (gameDTO.Status)
{
case GameStatus.Want:
_ = Want();
break;
case GameStatus.Playing:
_ = Playing();
break;
case GameStatus.Played:
_ = Played();
Rate = gameDTO.Rate;
break;
}
}




Join the conversation! Your thoughts help the community grow.