Endpoint /artistas esta funcionando normal! EndPOint /artistas/{nome} dando o erro abaixo: Erro:
ScreenSoundAPI/program.cs :
using Microsoft.AspNetCore.Diagnostics;
using ScreenSound.Banco;
using ScreenSound.Modelos;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options => options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
var app = builder.Build();
app.MapGet("/artistas", () =>
{
var dal = new DAL<Artista>(new ScreenSoundContext());
return dal.Listar();
});
app.MapGet("/artistas/{nome}", (string nome, ILogger<Program> logger) =>
{
var dal = new DAL<Artista>(new ScreenSoundContext());
return dal.RecuperarPor(a => a.Nome.ToUpper().Equals(nome.ToUpper()));
});
app.Run();
ScreenSound.Shared.Dados/Banco/DAL.cs :
using Microsoft.EntityFrameworkCore;
using ScreenSound.Modelos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScreenSound.Banco
{
public class DAL<T> where T : class
{
private readonly ScreenSoundContext context;
public DAL(ScreenSoundContext context)
{
this.context = context;
}
public IEnumerable<T> Listar()
{
return context.Set<T>().ToList();
}
public void Adicionar(T objeto)
{
context.Set<T>().Add(objeto);
context.SaveChanges();
}
public void Atualizar(T objeto)
{
context.Set<T>().Update(objeto);
context.SaveChanges();
}
public void Deletar(T objeto)
{
context.Set<T>().Remove(objeto);
context.SaveChanges();
}
public T? RecuperarPor(Func<T, bool> condicao) => context.Set<T>().FirstOrDefault(condicao);
public IEnumerable<T> RecuperarListaPor(Func<T, bool> condicao)
{
return context.Set<T>().Where(condicao).ToList();
}
}
}