Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

[Bug] Erro ao consultar o Get de Projetos

Seguindo os passos da aula 5 do modulo 3 do curso, no final ocorreu um erro ao realizar o get de projetos para ver se vinha as informações de Especialidades, segue a baixo imagem do erro.

Print da tela do swagger do get de projetosOlhei no Program.cs do projeto API achando que era algo a registro da classe e ele está assim:

using Freelando.Api.Endpoints;
using Freelando.Dados;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<FreelandoContext>((options) =>
{
    options.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]);
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.AddEndPointEspecialidades();
app.AddEndPointProjeto();
app.AddEndPointCandidatura();
app.AddEndPointClientes();
app.AddEndPointContrato();
app.AddEndPointProfissional();
app.AddEndPointServico();

app.UseHttpsRedirection();

app.Run();

Meu ProjetoConverter.cs está assim:

using Freelando.Api.Converters;
using Freelando.Api.Requests;
using Freelando.Api.Responses;
using Freelando.Modelos;

public class ProjetoConverter
{
    private ClienteConverter? _clienteConverter;
    private EspecialidadeConverter? _especialidadeConverter;
    public ProjetoResponse EntityToResponse(Projeto projeto)
    {
        _clienteConverter = new ClienteConverter();
        _especialidadeConverter = new EspecialidadeConverter();

        return (projeto is null)
            ? new ProjetoResponse(Guid.Empty, "", "", StatusProjeto.Disponivel.ToString(), null, new List<EspecialidadeResponse>())
            : new ProjetoResponse(projeto.Id, projeto.Titulo, projeto.Descricao, projeto.Status.ToString(), _clienteConverter.EntityToResponse(projeto.Cliente), _especialidadeConverter.EntityListToResponseList(projeto.Especialidades!));
    }

    public Projeto RequestToEntity(ProjetoRequest projetoRequest)
    {
        _especialidadeConverter = new EspecialidadeConverter();

        return (projetoRequest is null)
            ? new Projeto(Guid.Empty, "", "", StatusProjeto.Disponivel, new Cliente(Guid.Empty, "", "", "", "", new List<Projeto>()), new List<Especialidade>())
            : new Projeto(projetoRequest.Id, projetoRequest.Titulo!, projetoRequest.Descricao!, projetoRequest.Status, new Cliente(), _especialidadeConverter.RequestListToEntityList(projetoRequest.Especialidades));
    }

    public ICollection<ProjetoResponse> EntityListToResponseList(IEnumerable<Projeto>? projetos)
    {
        return (projetos is null)
            ? new List<ProjetoResponse>()
            : projetos.Select(p => EntityToResponse(p)).ToList();
    }

    public ICollection<Projeto> RequestListToEntityList(IEnumerable<ProjetoRequest>? projetos)
    {
        return (projetos is null)
            ? new List<Projeto>()
            : projetos.Select(a => RequestToEntity(a)).ToList();
    }
}
1 resposta
solução!

Olá Miguel! Tudo bem?

Pelo erro que você está enfrentando, parece que o ProjetoConverter não foi registrado no contêiner de injeção de dependência. Isso é necessário para que ele possa ser injetado nos seus endpoints.

Para resolver isso, você pode registrar o ProjetoConverter no Program.cs. Tente adicionar a seguinte linha antes de var app = builder.Build();:

builder.Services.AddTransient<ProjetoConverter>();

Isso deve garantir que o ProjetoConverter seja corretamente injetado quando necessário.

Espero ter ajudado e bons estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.