Home > OS >  type 'Commands.UpdateFirmDateCommand' cannot be used as type parameter 'TRequest'
type 'Commands.UpdateFirmDateCommand' cannot be used as type parameter 'TRequest'

Time:01-26

I am using asp.net core 6 web api ,entity framework code first and CQRS - Mediatr for a project which has functinalities to update database and get data from database and show it in the swaggerui but I came across with a problem when I try to create a handler for the update data by its ID in the database I get this error : Error CS0311 The type 'EnocaChallengeV2.Commands.UpdateFirmDateCommand' cannot be used as type parameter 'TRequest' in the generic type or method 'IRequestHandler<TRequest, TResponse>'. There is no implicit reference conversion from 'EnocaChallengeV2.Commands.UpdateFirmDateCommand' to 'MediatR.IRequest<int>'.

This is my Command:

using EnocaChallengeV2.Models;
using MediatR;

namespace EnocaChallengeV2.Commands
{
    public class UpdateFirmDateCommand : IRequest<Firm> {
        public int Id { get; set; }
        public DateTime startTime { get; set; }
        public DateTime endTime { get; set; }

        public UpdateFirmDateCommand(int id, DateTime StartTime, DateTime EndTime)
        {
            Id = id;
            startTime = StartTime;
            endTime = EndTime;
        }
    }
        
    
}

This is the Handler:

using EnocaChallengeV2.Commands;
using EnocaChallengeV2.Models;
using EnocaChallengeV2.Queries;
using EnocaChallengeV2.Repositories;
using MediatR;

namespace EnocaChallengeV2.Handlers
{
    public class UpdateFirmDateHandler : IRequestHandler<UpdateFirmDateCommand, int>
    {
        private readonly IFirmRepository _firmRepository;
        public UpdateFirmDateHandler(IFirmRepository firmRepository)
        {
            _firmRepository = firmRepository;
        }
        public async Task<int> Handle(UpdateFirmDateCommand command, CancellationToken cancellationToken)
        {
            var firm = await _firmRepository.GetFirmByIdAsync(command.Id);
            if (firm == null)
                return default;

            firm.startTime = command.startTime;
            firm.endTime = command.endTime;

            return await _firmRepository.UpdateFirmDateAsync(firm);
        }
    }
}

This is the interface:

using EnocaChallengeV2.Models;

namespace EnocaChallengeV2.Repositories
{
    public interface IFirmRepository
    {
        public Task<List<Firm>> GetFirmListAsync();
        public Task<Firm> GetFirmByIdAsync(int Id);
        public Task<Firm> AddFirmAsync(Firm firm);
        public Task<int> UpdateFirmDateAsync(Firm firm);
        public Task<int> UpdateFirmVerificationAsync(Firm firm);
    }
}

This is the repository:

using EnocaChallengeV2.Data;
using EnocaChallengeV2.Models;
using Microsoft.EntityFrameworkCore;

namespace EnocaChallengeV2.Repositories
{
    public class FirmRepository : IFirmRepository
    {
        private readonly DbContextClass _dbContext;

        public FirmRepository(DbContextClass dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<Firm> AddFirmAsync(Firm firm)
        {
            var result = _dbContext.Firms.Add(firm);
            await _dbContext.SaveChangesAsync();
            return result.Entity;
        }

        public async Task<int> DeleteFirmAsync(int Id)
        {
            var filteredData = _dbContext.Firms.Where(x => x.Id == Id).FirstOrDefault();
            _dbContext.Firms.Remove(filteredData);
            return await _dbContext.SaveChangesAsync();
        }

        public async Task<Firm> GetFirmByIdAsync(int Id)
        {
            return await _dbContext.Firms.Where(x => x.Id == Id).FirstOrDefaultAsync();
        }

        public async Task<List<Firm>> GetFirmListAsync()
        {
            return await _dbContext.Firms.ToListAsync();
        }

        public async Task<int> UpdateFirmDateByIdAsync(Firm firm)
        {
            _dbContext.Firms.Update(firm);
            return await _dbContext.SaveChangesAsync();
        }

        //public async Task<Firm> UpdateFirmVerificationAsync(Firm firm)
        //{
        //    _dbContext.Firms.Update(firm);
        //    return await _dbContext.SaveChangesAsync();
        //}
    }
}

I tried to change the Task<> to Firm and then to int but didn't seem to work.

CodePudding user response:

You command needs to define a return type of 'int':

namespace EnocaChallengeV2.Commands
{
    public class UpdateFirmDateCommand : IRequest<int> {
        public int Id { get; set; }
        public DateTime startTime { get; set; }
        public DateTime endTime { get; set; }

        public UpdateFirmDateCommand(int id, DateTime StartTime, DateTime EndTime)
        {
            Id = id;
            startTime = StartTime;
            endTime = EndTime;
        }
    }
}

Note the change:

IRequest<int>
  • Related