zoukankan      html  css  js  c++  java
  • ASP.NET Core 3.x RESTful API学习记录--获取父子关系资源

        //获取父子资源关系实例 要在路由上表示出来父子关系
       [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);
            }
    
            [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);
            }

    知识来源草根专栏 感谢杨旭大佬

  • 相关阅读:
    Layui数据表格用法
    初识Vue
    使用NPOI导出Excel表
    使用NPOI将Excel表导入到数据库中
    新随笔
    AX2012/D365 SSRS报表开发
    AX2012自定义注释脚本开发
    D365做文件导入导出CSV
    Azure文件上传下载删除(D365可以直接用)
    关于D365/AX2012/C#中的那些json、对象、字符串类型间的转换
  • 原文地址:https://www.cnblogs.com/cqqinjie/p/13370487.html
Copyright © 2011-2022 走看看