Skip to content

Commit

Permalink
Update to p21
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbowl committed Jan 15, 2020
1 parent 362bef3 commit 50f1496
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __ASP.NET Core 3.0 实现 RESTful API 的学习笔记__
<br><br>
跟随杨旭老师(solenovex)的[博客](https://www.cnblogs.com/cgzl/p/11814971.html)[视频](https://www.bilibili.com/video/av77957694?from=search&seid=17664776753878261104)课程,学习 RESTful API 在ASP.NET Core 3.0 上的实现。
<br>
包含课程中搭建的项目与部分笔记,目前覆盖到视频 P19
包含课程中搭建的项目与部分笔记,目前覆盖到视频 P21
<br><br><br>
非常感谢杨老师 🤗
<br>
39 changes: 32 additions & 7 deletions Routine/Routine.APi/Controllers/CompaniesController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Routine.APi.DtoParameters;
using Routine.APi.Entities;
using Routine.APi.Models;
using Routine.APi.Services;
using System;
Expand All @@ -9,12 +10,18 @@
using System.Threading.Tasks;

/*
* HTTP请求:
* GET - 查询
* POST - 创建/添加
* PATCH - 局部修改/更新
* PUT - 如果存在就替换,不存在则创建
* DELETE - 移除/删除
* HTTP方法: | 安全 幂等
* |
* GET - 查询 | Y Y
* POST - 创建/添加 | N N
* PATCH - 局部修改/更新 | N N
* PUT - 如果存在就替换,不存在则创建 | N Y
* DELETE - 移除/删除 | N Y
* OPTIONS - 略 | Y Y
* HEAD - 略 | Y Y
*
* 安全性是指方法执行后并不会改变资源的表述
* 幂等性是指方法无论执行多少次都会得到同样的结果
*/

/*
Expand Down Expand Up @@ -100,7 +107,7 @@ public async Task<IActionResult> GetCompanies([FromQuery]CompanyDtoParameters pa
return Ok(companyDtos); //OK() 返回状态码200
}

[HttpGet("{companyId}")] //还可用 [Route("{companyId}")]
[HttpGet("{companyId}",Name =nameof(GetCompany))] //[Route("{companyId}")]
public async Task<IActionResult> GetCompany(Guid companyId)
{
var company = await _companyRepository.GetCompanyAsync(companyId);
Expand All @@ -111,5 +118,23 @@ public async Task<IActionResult> GetCompany(Guid companyId)
return Ok(_mapper.Map<CompanyDto>(company));
}

[HttpPost]
public async Task<IActionResult> CreateCompany([FromBody]CompanyAddDto company) //Task<IActionResult> = Task<ActionResult<CompanyDto>
{
//老版本需要使用以下代码:
//新版使用 ApiController 属性以后,无需再手动检查
//if (company == null)
//{
// return BadRequest(); //返回状态码400
//}

var entity = _mapper.Map<Company>(company);
_companyRepository.AddCompany(entity);
await _companyRepository.SaveAsync();
var returnDto = _mapper.Map<CompanyDto>(entity);
//返回状态码201
//通过使用 CreatedAtRoute 返回时可以在 Header 中添加一个地址(Loaction)
return CreatedAtRoute(nameof(GetCompany), new { companyId = returnDto.Id }, returnDto);
}
}
}
15 changes: 15 additions & 0 deletions Routine/Routine.APi/Models/CompanyAddDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Routine.APi.Models
{
//输入使用的Dto
//查询、插入、更新应该使用不同的Dto,便于业务升级与重构
public class CompanyAddDto
{
public string Name { get; set; }
public string Introduction { get; set; }
}
}
1 change: 1 addition & 0 deletions Routine/Routine.APi/Models/CompanyDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Routine.APi.Models
{
//输出使用的Dto
public class CompanyDto
{
public Guid Id { get; set; }
Expand Down
6 changes: 2 additions & 4 deletions Routine/Routine.APi/Profiles/CompanyProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Routine.APi.Models;

/// <summary>
/// AutoMapper 配置文件
/// AutoMapper 映射关系 配置文件
/// </summary>
namespace Routine.APi.Profiles
{
Expand All @@ -16,9 +16,7 @@ public CompanyProfile()
//属性名称一致时自动赋值
//自动忽略空引用
CreateMap<Company, CompanyDto>();

//手动映射举例,把 Company(src) 的 Name 映射到 CompanyDto(dest) 的 Name
//CreateMap<Company, CompanyDto>().ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
CreateMap<CompanyAddDto, Company>();
}
}
}
3 changes: 3 additions & 0 deletions Routine/Routine.APi/Profiles/EmployeeProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using Routine.APi.Models;
using System;

/// <summary>
/// AutoMapper 映射关系 配置文件
/// </summary>
namespace Routine.APi.Profiles
{
public class EmployeeProfile : Profile
Expand Down
8 changes: 5 additions & 3 deletions Routine/Routine.APi/Services/CompanyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public void AddCompany(Company company)
throw new ArgumentNullException(nameof(company));
}
company.Id = Guid.NewGuid();
foreach (var employee in company.Employees)
if (company.Employees != null)
{
employee.Id = Guid.NewGuid();
foreach (var employee in company.Employees)
{
employee.Id = Guid.NewGuid();
}
}

_context.Companies.Add(company);
}

Expand Down

0 comments on commit 50f1496

Please sign in to comment.