当前位置:网站首页>Restfulapi learning notes -- father son resources (4)

Restfulapi learning notes -- father son resources (4)

2020-11-08 13:18:43 Little flower in dream

Preface

Earlier in the series mentioned , To reflect a structure of resources , So how to embody the structure ? For example, get emproyee, Should write /api/companies/1/emproyees, This can reflect its structure .

So how to set this way ?

Text

[ApiController]
[Route("api/companies/{companyId}/employees")]
public class EmployeesController : ControllerBase
{
  private readonly IMapper _mapper;
  private readonly ICompanyRepository _companyRepository;

  public EmployeesController(IMapper mapper, ICompanyRepository companyRepository)
   {
         _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
         _companyRepository = companyRepository ?? throw new ArgumentNullException(nameof(companyRepository));
   }
   [HttpGet]
    public async Task<ActionResult<IEnumerable<EmployeeDto>>>
	GetEmployeesForCompany(Guid companyId,
		[FromQuery] EmployeeDtoParameters parameters){
	if (!await _companyRepository.CompanyExistsAsync(companyId))
	{
		return NotFound();
	}

	var employees = await _companyRepository
		.GetEmployeesAsync(companyId, parameters);

	var employeeDtos = _mapper.Map<IEnumerable<EmployeeDto>>(employees);

	return Ok(employeeDtos);
   }
}

1. Configured route is [Route("api/companies/{companyId}/employees")], such companyId It's going to be a variable , that Guid companyId Will get .
2. Here's the logic ,employees It's from company From the service of , Instead of building a single employees service .
3. It is also worth noting that the acquisition is employees , It's a collection , Even if it's empty , This set also exists , So there is no 404 That is to say , But if companyid It doesn't exist, so this is 404, Because there may not be a specific company .

At this time, if you return 404 Then we can clearly know that the company does not exist , Not for other reasons .

This is to get a collection , How about getting a single employee .

It's the same as before , as follows :

[HttpGet("{employeeId}"]
public async Task<ActionResult<EmployeeDto>>
	GetEmployeeForCompany(Guid companyId, Guid employeeId)
{
	if (!await _companyRepository.CompanyExistsAsync(companyId))
	{
		return NotFound();
	}

	var employee = await _companyRepository.GetEmployeeAsync(companyId, employeeId);
	if (employee == null)
	{
		return NotFound();
	}

	var employeeDto = _mapper.Map<EmployeeDto>(employee);

	return Ok(employeeDto);
}

The only thing worth noting is 404, Maybe the company doesn't exist , It may also be that employees don't exist , Here you can go back to 404 When you do, write down some specific reasons , But it doesn't have to be .

Because whether the company does not exist or the employees do not exist , It's the employee who didn't find , Prompt the corresponding error directly . There's another reason , In general, this kind of link is automatically generated get request , So if there is no problem of manual selection , So if 404, The front-end approach is that employees don't exist , Without knowing if the company doesn't exist .

版权声明
本文为[Little flower in dream]所创,转载请带上原文链接,感谢