Chapter10:WORKING WITH DELETE REQUESTS

10 WORKING WITH DELETE REQUESTS

Let’s start this section by deleting a child resource first. So, let’s modify the IEmployeeRepository interface:

让我们先删除子资源,从本节开始。因此,让我们修改 IEmployeeRepository 接口:

public interface IEmployeeRepository
{
    IEnumerable<Employee> GetEmployees(Guid companyId, bool trackChanges);
    Employee GetEmployee(Guid companyId, Guid id, bool trackChanges);

    void CreateEmployeeForCompany(Guid companyId, Employee employee);

    void DeleteEmployee(Employee employee);
}

The next step for us is to modify the EmployeeRepository class:
下一步是修改 EmployeeRepository 类:

public void DeleteEmployee(Employee employee) { Delete(employee); }

Finally, we can add a delete action to the controller class:
最后,我们可以向控制器类添加一个删除操作:

[HttpDelete("{id}")]
public IActionResult DeleteEmployeeForCompany(Guid companyId, Guid id)
{
    var company = _repository.Company.GetCompany(companyId, trackChanges: false);
    if (company == null)
    {
        _logger.LogInfo($"Company with id: {companyId} doesn't exist in the database.");
        return NotFound();
    }
    var employeeForCompany = _repository.Employee.GetEmployee(companyId, id, trackChanges: false);
    if (employeeForCompany == null)
    {
        _logger.LogInfo($"Employee with id: {id} doesn't exist in the database.");
        return NotFound();
    }
    _repository.Employee.DeleteEmployee(employeeForCompany);
    _repository.Save(); return NoContent();
}

There is nothing new with this action. We collect the companyId from the root route and the employee’s id from the passed argument. We have to check if the company exists. Then, we check for the employee entity.
此操作没有什么新鲜事。我们从根路由收集公司 ID,从传递的参数中收集员工的 ID。我们必须检查公司是否存在。然后,我们检查员工实体。

Finally, we delete our employee and return the NoContent() method, which returns the status code 204 No Content.
最后,我们删除我们的员工并返回 NoContent() 方法,该方法返回状态代码 204 No Content。

Let’s test this:
让我们测试一下:

https://localhost:5001/api/companies/0AD5B971-FF51-414D-AF01-34187E407557/employees/DE662003-ACC3-4F9F-9D82-0A74F64594C1
Alt text

Excellent. It works great.
非常好。效果很好。

You can try to get that employee from the database, but you will get 404 for sure:
您可以尝试从数据库中获取该员工,但您肯定会得到 404:

https://localhost:5001/api/companies/0AD5B971-FF51-414D-AF01-34187E407557/employees/DE662003-ACC3-4F9F-9D82-0A74F64594C1

Alt text

We can see that the DELETE request isn’t safe because it deletes the resource, thus changing the resource representation. But if we try to send this delete request one or even more times, we would get the same 404 result because the resource doesn’t exist anymore. That’s what makes the DELETE request idempotent.
我们可以看到 DELETE 请求是不安全的,因为它删除了资源,从而改变了资源表示形式。但是,如果我们尝试发送此删除请求一次甚至多次,我们将得到相同的 404 结果,因为资源不再存在。这就是使 DELETE 请求具有幂等性的原因。

10.1 Deleting a Parent Resource with its CHILDREN

With Entity Framework Core, this action is pretty simple. With the basic configuration, cascade deleting is enabled, which means deleting a parent resource will automatically delete all of its children. We can confirm that from the migration file:
使用实体框架核心,此操作非常简单。使用基本配置时,将启用级联删除,这意味着删除父资源将自动删除其所有子资源。我们可以从迁移文件中确认:

Alt text

So, all we have to do is to create a logic for deleting the parent resource.
因此,我们所要做的就是创建一个用于删除父资源的逻辑。

Well, let’s do that following the same steps as in a previous example:
好吧,让我们按照与前面示例中相同的步骤执行此操作:

public interface ICompanyRepository
{
    IEnumerable<Company> GetAllCompanies(bool trackChanges);
    Company GetCompany(Guid companyId, bool trackChanges);
    void CreateCompany(Company company);

    IEnumerable<Company> GetByIds(IEnumerable<Guid> ids, bool trackChanges);

    void DeleteCompany(Company company);
}

Then let’s modify the repository class:
然后让我们修改存储库类:

public void DeleteCompany(Company company) { Delete(company); }

Finally, let’s modify our controller:
最后,让我们修改我们的控制器:

[HttpDelete("{id}")]
public IActionResult DeleteCompany(Guid id)
{
    var company = _repository.Company.GetCompany(id, trackChanges: false); 
    if (company == null)
    {
        _logger.LogInfo($"Company with id: {id} doesn't exist in the database."); 
        return NotFound();
    }
    _repository.Company.DeleteCompany(company);
    _repository.Save();
    return NoContent();
}

And let’s test our action:
让我们测试一下我们的操作:
https://localhost:5001/api/companies/0AD5B971-FF51-414D-AF01-34187E407557

Alt text

It works.
它有效。

You can check in your database that this company alongside its children doesn’t exist anymore.
您可以在数据库中检查这家公司及其子公司是否不再存在。

There we go. We have finished working with DELETE requests and we are ready to continue to the PUT requests.
我们开始吧。我们已经完成了对 DELETE 请求的处理,我们已准备好继续处理 PUT 请求。

For the PUT requests, we are going to inspect our console window for the SQL generated commands. If you can’t see those, then just add this code in the appsettings.json file:
对于 PUT 请求,我们将检查控制台窗口中是否有 SQL 生成的命令。如果您看不到这些,则只需在appsettings.json文件中添加此代码:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore": "Information"
    }
  },

发表评论