Rob
Rob

Reputation: 159

Get record by ID using Entity Framework

How can I change the following action to get a single result by ID? I have the following code to get All records from the database, I want to modifiy it so that I can get a single record.

namespace PDS.Core.App.Data.Action.Product
{
    public class GetProducts_Action : BaseEFAction<GetProducts_Action_Request, GetProducts_Action_Response>
    {
        private CRMSContext Context { get; }
        private IMapper Mapper { get; }

        public GetProducts_Action(ILogger<GetProducts_Action> logger, CRMSContext context, ITransactionManager scope, IMapper mapper) : base(logger, context, scope)
        {
            Context = context.ValidateAndConsumeNonNullableArgument(nameof(context));
            Mapper = mapper.ValidateAndConsumeNonNullableArgument(nameof(mapper));
        }

        protected override async Task<GetProducts_Action_Response> PerformActionAsync(GetProducts_Action_Request request)
        {
            var tb_Products = await Context.TB_Products
                .ToListAsync();

            var tb_ProductsDTOs = Mapper.Map<IList<TB_ProductDTO>>(tb_Products);
            return new GetProducts_Action_Response { TB_Products = tb_ProductsDTOs };
        }
    }

    public class GetProducts_Action_Request : BaseActionRequest
    {

    }

    public class GetProducts_Action_Response : BaseActionResponse
    {
        public IList<TB_ProductDTO> TB_Products { get; set; }
    }
}

Upvotes: 0

Views: 1689

Answers (1)

Selim Yildiz
Selim Yildiz

Reputation: 5370

You need to write the code a bit if you extend the code instead of modifying it.

First, you can create a new request class similar to GetProducts_Action_Request that has ProductId in order to use it for getting a single product item. For example, let's say GetProduct_Action_Request:

public class GetProduct_Action_Request : BaseActionRequest
{
    public int ProductId {get; set;}
}

And response object that has single product:

public class GetProduct_Action_Response : BaseActionResponse
{
    public TB_ProductDTO TB_Product { get; set; }
}

Then you need to create new action GetProduct_Action similar to GetProducts_Action. And you can use SingleOrDefaultAsync to return the only Product element that satisfies a Id condition:

public class GetProduct_Action : BaseEFAction<GetProduct_Action_Request, GetProduct_Action_Response>
{
    private CRMSContext Context { get; }
    private IMapper Mapper { get; }

    public GetProduct_Action(ILogger<GetProduct_Action> logger, CRMSContext context, ITransactionManager scope, IMapper mapper) : base(logger, context, scope)
    {
        Context = context.ValidateAndConsumeNonNullableArgument(nameof(context));
        Mapper = mapper.ValidateAndConsumeNonNullableArgument(nameof(mapper));
    }

    protected override async Task<GetProduct_Action_Response> PerformActionAsync(GetProduct_Action_Request request)
    {
        var tb_Product = await Context.TB_Products.SingleOrDefaultAsync(i=> i.Id == request.ProductId);

        var tb_ProductDTO = Mapper.Map<IList<TB_ProductDTO>>(tb_Product);
        return new GetProduct_Action_Response { TB_Product = tb_ProductDTO };
    }
}

public class GetProduct_Action_Request : BaseActionRequest
{
    public int ProductId {get; set;}
}

public class GetProduct_Action_Response : BaseActionResponse
{
    public TB_ProductDTO TB_Product { get; set; }
}

Therefore you have 2 actions GetProduct_Action for a single product based on Id and GetProducts_Action for a list of all products.

Upvotes: 1

Related Questions