hello every one i am biginner with APIs

  Kiến thức lập trình

I’m currently working on an API project where I’m using AutoMapper (IMapper) for mapping objects. However, when testing my API endpoints with Postman, I’m encountering an issue where the retrieved data is returning null values.
Issue with retrieving data using IMapper in API project – getting null value from Postman
generic repo

 public class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity

 {
     private readonly StoreDbContext _context;

     public GenericRepository(StoreDbContext context)
     {
         _context = context;
     }

     public async Task<IEnumerable<T>> GetAllAsync()
     {
         //if (typeof(T) == typeof(Product)) { 

         //    return (IEnumerable<T>) await  _context.Products.Include(P=>P.Brand).Include(P=>P.Category).ToListAsync();
         //}
         //return await _context.Set<T>().ToListAsync();
         if (typeof(T) == typeof(Product))
         {
             return (IEnumerable<T>) await _context.Products
                 .Include(p => p.Brand)
                 .Include(p => p.Category)
                 .ToListAsync();
         }
         return await _context.Set<T>().ToListAsync();
     }

     public async Task<T?> GetAsync(int id)
     {
         //if (typeof(T) == typeof(Product))
         //{
         //    return _context.Products.Where(P=>P.Id == id).Include(P=>P.Brand).Include(P=>P.Category).FirstOrDefault() as T;

         //}
         //return await _context.Set<T>().FindAsync(id);
         if (typeof(T) == typeof(Product))
         {
             return await _context.Products
                 .Where(p => p.Id == id)
                 .Include(p => p.Brand)
                 .Include(p => p.Category)
                 .FirstOrDefaultAsync() as T;
         }
         return await _context.Set<T>().FindAsync(id);
     }

     public async Task<IEnumerable<T>> GetWithSpecsAllAsync(ISpecifications<T> spec)
     {
          return await SpecificationsEvaluator<T>.GetQuery(_context.Set<T>(),spec).ToListAsync();   
     }

     public async Task<T?> GetWithSpecsAsync(ISpecifications<T> spec)
     {
         return await SpecificationsEvaluator<T>.GetQuery(_context.Set<T>(), spec).FirstOrDefaultAsync();
     }
 public class ProductToReturnDto
 {
     public int Id { get; set; }
     public string Name { get; set; }
     public string PictureUrl { get; set; }
     public decimal Price { get; set; }
     public string Description { get; set; }
     public int? BrandId { get; set; }
     public string Brand { get; set; }
     public int? CategoryId { get; set; }
     public string Category { get; set; }
 }
public class MappingProfile :Profile
{
    public MappingProfile()
    {   
        CreateMap<Product, ProductToReturnDto>()
            .ForMember(D => D.Brand, O => O.MapFrom(S=>S.Brand.Name))
            .ForMember(D => D.Category, O => O.MapFrom(S=>S.Category.Name));
                                                                   
    }
}

New contributor

Mohammed Abdallah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT