while writing an Api wtih .NET Core 6(This is my first Api), I'm having some trouble while writing an Update method. I need to update my Application table below
public partial class Application
{
public Guid Id { get; set; }
public DateTime Createddate { get; set; }
public DateTime Modifieddate { get; set; }
public string? Companyid { get; set; }
public Guid? Customerid { get; set; }
public Guid? Productid { get; set; }
public string Status { get; set; } = null!;
}
But only the Status should change and rest must remain same. For this purpose, I wrote a DTO like below,
public class ApplicationUpdateDto
{
public Guid Id { get; set; }
public string Status { get; set; }
public DateTime ModifiedDate = DateTime.Now;
}
And my HttpPut Method is
[HttpPut]
public async Task<IActionResult> Update(ApplicationUpdateDto applicationUpdateDto)
{
await _service.UpdateAsync(_mapper.Map<Application>(applicationUpdateDto));
return CreateActionResult(CustomResponseDto<List<NoContentDto>>.Success(204));
}
In this case, request has only 2 data which are ID and Status and when executed,Status changes but also, other values that dont exist in DTO but exists in Application get default values or null values. I'm using Automapper 11 and this is my MapProfile;
public MapProfile()
{
CreateMap<ApplicationUpdateDto, Application>();
}
How can i solve this and what is the best practise while handling this kind of situations?
I appreciate any help. Thank you.