Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Opertaion Query #25

Open
arunprasathv opened this issue Mar 27, 2019 · 2 comments
Open

Delete Opertaion Query #25

arunprasathv opened this issue Mar 27, 2019 · 2 comments
Assignees

Comments

@arunprasathv
Copy link

@Boriszn
I would like to know more about delete, since there's no context in api, i want to know how to delete row from a particular table.

@rudreshgp
Copy link
Contributor

rudreshgp commented Apr 1, 2019

To delete a record you need to first create a method in the service

IDeviceService.cs

/// <summary>
/// Deletes a record from the Device Table
/// </summary>
/// <param name="deviceId">Id of the device</param>
void DeleteDevice(Guid deviceId);

Implement this method in the DeviceService.cs

/// <summary>
/// Deletes a record from the Device Table
/// </summary>
/// <param name="deviceId">Id of the device</param>
public void DeleteDevice(Guid deviceId)
{
    deviceValidationService.ValidateDeviceId(deviceId);

    var deviceRepository = unitOfWork.GetRepository<Device>();

    Device device = deviceRepository.Get(deviceId);

    if (device == null)
        throw new NullReferenceException();

    var entityState = deviceRepository.Delete(device);

    if (entityState != EntityState.Deleted)   // Move this to validation service
        throw new DbUpdateException("Record Not deleted", innerException: null);

    unitOfWork.Commit();
}

Then call this method from the Delete action inside DeviceController

/// <summary>
/// 
/// </summary>
/// <param name="deviceId"></param>
/// <returns></returns>
[HttpDelete("{deviceId}")]
[SwaggerOperation("DeleteDevice")]
public IActionResult Delete([FromRoute]Guid deviceId)
{
       deviceService.DeleteDevice(deviceId);
       return new OkResult();
}

To delete a record use HttpDelete and pass DeviceId, TenanatId as parameters.

@Boriszn Boriszn self-assigned this Apr 23, 2019
@Boriszn
Copy link
Owner

Boriszn commented Apr 24, 2019

Thanks a lot @arunprasathv @rudreshgp for the question and input.

I will create PR based on this issue, as soon as I finish with the current my task "Integration with docker and kubernetes". ;)

Best,
Boris

@Boriszn Boriszn pinned this issue Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants