Http Patch Operation Not Updating Database In EF Core .NET Core

368 views Asked by At

I am trying to apply patch operation in Data Layer Repository but due to have no Model State available in Data Layer i am unable to update it. I have debug the overall logic i come to the conclusion that patch.ApplyTo() operation update the Model but context.SaveChanges() is not updating the database context.

Below is the context class

public class ApplicationDbContext : DbContext, IApplicationDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> opts) : base(opts)
        {

        }
        public override int SaveChanges()
        {
            return base.SaveChanges();
        }

    }

Below is the repositroy method code

public class SpotWriteOnly : ISpotWriteOnly {
    private IApplicationDbContext _context;
    public SpotWriteOnly(IApplicationDbContext context)
    {
        _context = context;
    }
    public int UpdateSpot(long id, JsonPatchDocument<SpotData> patch) {
        Spot s = _context.Spots.Include(x => x.Agent)
                        .Include(x => x.Channel)
                        .Include(x => x.Timeband)
                        .Include(x => x.Region)
                        .Include(x => x.Sponsor)
                        .Include(x => x.SpotType)
                        .Include(x => x.Status)
                        .Include(x => x.SpotStatusReason)
                        .OrderByDescending(x => x.Version)
                        .FirstOrDefault(x => x.SpotId == id && x.IsDeleted == false);
        SpotData sData = new SpotData { Spot = s };
        patch.ApplyTo(sData);
        int response = _context.SaveChanges();
        return response;
    }
}

Below is the Handler Class

public class UpdateSpotQueryHandler : IRequestHandler<UpdateSpotQuery, int> {
    public ISpotWriteOnly _repository;
    public IMapper _mapper;
    public UpdateSpotQueryHandler(ISpotWriteOnly repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    public Task<int> Handle(UpdateSpotQuery query, CancellationToken cancellationToken) {
        return Task.FromResult(_repository.UpdateSpot(query.SpotId, query.Patch));
    }

}

Below is the Query

    public class UpdateSpotQuery : IRequest<int> {
        public long SpotId { get; set; }
        public JsonPatchDocument<SpotData> Patch { get; set; }
    }

Below is the controller code

    [HttpPatch("{id}")]
    public IActionResult UpdateSpot(long id,[FromBody] JsonPatchDocument<SpotData> patch) {
        if(ModelState.IsValid) {
            var result = Mediator.Send(new UpdateSpotQuery { SpotId = id, Patch = patch} );
            var response = new Envelop
                {
                    IsSuccessful = true,
                    StatusCode = 200,
                    StatusMessage = "OK",
                    Response = result.Result,
                    isExceptionGenerated = false,
                    Exception = null,
                };
            return Ok(response);
        }
        else {
            var response = new Envelop
                {
                    IsSuccessful = false,
                    StatusCode = 200,
                    StatusMessage = "Error in Model",
                    Response = ModelState,
                    isExceptionGenerated = true,
                    Exception = new Exception[] { new ArgumentException()},
                };
            return BadRequest(response);
        }
    }

I am trying to update database using patch operation in data layer.

0

There are 0 answers