Which Way .NET Developer?

You must create an endpoint that will return games, joined by genre by id, from the database.

Option 1

public abstract class BaseModel
{
    public Guid Id { get; set; }
}

public class Genre : BaseModel
{
    public string Name { get; set; }
}

public class Game : BaseModel
{
    public string Name { get; set; }
    public Genre Genre { get; set; }
    public decimal Price { get; set; }
}

public interface IGameRepository
{
    Game Get(Guid id);
}

public class GameRepository : IGameRepository
{
    private  readonly DataContext _context;

    public GameRepository(DataContext context)
    {
        _context = context;
    }

    public Game? Get(Guid id)
    {
        return _context
            .Games
            .Include(g => g.Genre)
            .Where(g = g.Id == id)
            .AsNoTracking()
            .FirstOrDefault();
    }
}

public class GameDto // base DTO?
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Genre { get; set; } // Genre DTO?
    public decimal Price { get; set; }
}

public static class GameExtension 
{
    public static GameDto AsDto(this Game game)
    {
        return new GameDto
        {
            Id = game.Id,
            Name = game.Name,
            Genre = game.Genre.Name,
            Price = game.Price
        };
    }
}

public interface IGameService
{
    GameDto GetGameById(Guid id);
}

public class GameService : IGameService
{
    private readonly IGameRepository _repository;

    public GameService(IGameRepository repository)
    {
        _repository = repository;
    }

    public GameDto? GetGameById(Guid id)
    {
        return _repository.Get(id)?.AsDto();
}

public class GamesController : Controller
{
    private readonly IGameService _service;

    public GameController(IGameService service)
    {
        _service = service;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(Guid id)
    {
        var game = _service.GetGameById(id);
        if (game == null)
        {
            return NotFound();
        }
        return Ok(result);
    }
}
public abstract class BaseModel
{
    public Guid Id { get; set; }
}

public class Genre : BaseModel
{
    public string Name { get; set; }
}

public class Game : BaseModel
{
    public string Name { get; set; }
    public Genre Genre { get; set; }
    public decimal Price { get; set; }
}

public interface IGameRepository
{
    Game Get(Guid id);
}

public class GameRepository : IGameRepository
{
    private  readonly DataContext _context;

    public GameRepository(DataContext context)
    {
        _context = context;
    }

    public Game? Get(Guid id)
    {
        return _context
            .Games
            .Include(g => g.Genre)
            .Where(g = g.Id == id)
            .AsNoTracking()
            .FirstOrDefault();
    }
}

public class GameDto // base DTO?
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Genre { get; set; } // Genre DTO?
    public decimal Price { get; set; }
}

public static class GameExtension 
{
    public static GameDto AsDto(this Game game)
    {
        return new GameDto
        {
            Id = game.Id,
            Name = game.Name,
            Genre = game.Genre.Name,
            Price = game.Price
        };
    }
}

public interface IGameService
{
    GameDto GetGameById(Guid id);
}

public class GameService : IGameService
{
    private readonly IGameRepository _repository;

    public GameService(IGameRepository repository)
    {
        _repository = repository;
    }

    public GameDto? GetGameById(Guid id)
    {
        return _repository.Get(id)?.AsDto();
}

public class GamesController : Controller
{
    private readonly IGameService _service;

    public GameController(IGameService service)
    {
        _service = service;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> Get(Guid id)
    {
        var game = _service.GetGameById(id);
        if (game == null)
        {
            return NotFound();
        }
        return Ok(result);
    }
}

Option 2

app.MapGet("/games/{id}", (DbConnection connection, HttpResponse response, Guid id) => {
    response.ContentType = MediaTypeNames.Application.Json;
    var result = connection.Read<string>(@"
        select
            json_build_object(
                'id', game_id,
                'name', games.name,
                'genre', genres.name,
                'price', price
            )
        from 
            games join genres using (genre_id)
        where
            game_id = @id
    ", id)
    .FirstOrDefault();
    if (result == null)
    {
        return Results.NotFound();
    }
    return Results.Ok(result);
});
app.MapGet("/games/{id}", (DbConnection connection, HttpResponse response, Guid id) => {
    response.ContentType = MediaTypeNames.Application.Json;
    var result = connection.Read<string>(@"
        select
            json_build_object(
                'id', game_id,
                'name', games.name,
                'genre', genres.name,
                'price', price
            )
        from 
            games join genres using (genre_id)
        where
            game_id = @id
    ", id)
    .FirstOrDefault();
    if (result == null)
    {
        return Results.NotFound();
    }
    return Results.Ok(result);
});

Which way?

To receive notifications about new posts and updates, consider subscribing to my LinkdIn page:
vb-software linkedin

You will receive notifications about new posts on your LinkedIn feed.
Comments
If you like my content, use my software, or otherwise benefit from my work, consider supporting me by buying me a coffee. The software runs on coffee after all.
Buy me a Coffee Scan Here To Buy Me a Cofee