Skip to content

Commit 8d11886

Browse files
committed
Patch P32 整理using
1 parent 0bd4740 commit 8d11886

File tree

11 files changed

+49
-47
lines changed

11 files changed

+49
-47
lines changed

Routine/Routine.APi/Controllers/CompaniesController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Routine.APi.Services;
77
using System;
88
using System.Collections.Generic;
9-
using System.Linq;
109
using System.Threading.Tasks;
1110

1211
/*

Routine/Routine.APi/Controllers/EmployeesController.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using AutoMapper;
22
using Microsoft.AspNetCore.JsonPatch;
33
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.AspNetCore.Mvc.ModelBinding;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Options;
47
using Routine.APi.Entities;
58
using Routine.APi.Models;
69
using Routine.APi.Services;
@@ -168,20 +171,56 @@ public async Task<IActionResult> PartiallyUpdateEmployeeForCompany(Guid companyI
168171
var employeeEntity = await _companyRepository.GetEmployeeAsync(companyId, employeeId);
169172
if (employeeEntity == null)
170173
{
171-
return NotFound();
172-
}
174+
//不允许客户端生成 Guid
175+
//return NotFound();
173176

174-
var dtoToPatch = _mapper.Map<EmployeeUpdateDto>(employeeEntity);
177+
//允许客户端生成 Guid
178+
var employeeDto = new EmployeeUpdateDto();
179+
//传入 ModelState 进行验证
180+
patchDocument.ApplyTo(employeeDto, ModelState);
181+
if (!TryValidateModel(employeeDto))
182+
{
183+
return ValidationProblem(ModelState);
184+
}
175185

176-
//此处需要处理验证错误,待完成
186+
var employeeToAdd = _mapper.Map<Employee>(employeeDto);
187+
employeeToAdd.Id = employeeId;
188+
_companyRepository.AddEmployee(companyId, employeeToAdd);
189+
await _companyRepository.SaveAsync();
190+
var dtoToReturn = _mapper.Map<Employee>(employeeToAdd);
177191

192+
return CreatedAtAction(nameof(GetEmployeeForCompany),
193+
new { companyId = companyId, employeeId = employeeId },
194+
dtoToReturn);
195+
}
196+
197+
var dtoToPatch = _mapper.Map<EmployeeUpdateDto>(employeeEntity);
198+
//将 Patch 应用到 dtoToPatch(EmployeeUpdateDto)
178199
patchDocument.ApplyTo(dtoToPatch);
200+
//验证模型
201+
if (!TryValidateModel(dtoToPatch))
202+
{
203+
return ValidationProblem(ModelState); //返回状态码与错误信息
204+
}
179205
_mapper.Map(dtoToPatch, employeeEntity);
180206
_companyRepository.UpdateEmployee(employeeEntity);
181207
await _companyRepository.SaveAsync();
182208
return NoContent(); //返回状态码204
183209
}
184210

211+
/// <summary>
212+
/// 重写 ValidationProblem
213+
/// 使 PartiallyUpdateEmployeeForCompany 中的 ValidationProblem() 返回状态码422而不是400
214+
/// </summary>
215+
/// <param name="modelStateDictionary"></param>
216+
/// <returns></returns>
217+
public override ActionResult ValidationProblem(ModelStateDictionary modelStateDictionary)
218+
{
219+
var options = HttpContext.RequestServices
220+
.GetRequiredService<IOptions<ApiBehaviorOptions>>();
221+
return (ActionResult)options.Value.InvalidModelStateResponseFactory(ControllerContext);
222+
}
223+
185224
[HttpOptions]
186225
public IActionResult GetCompaniesOptions()
187226
{

Routine/Routine.APi/Data/RoutineDbContext.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Routine.APi.Entities;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
74

85
namespace Routine.APi.Data
96
{

Routine/Routine.APi/DtoParameters/CompanyDtoParameters.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Routine.APi.DtoParameters
1+
namespace Routine.APi.DtoParameters
72
{
83
public class CompanyDtoParameters
94
{

Routine/Routine.APi/Entities/Company.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
53

64
namespace Routine.APi.Entities
75
{

Routine/Routine.APi/Models/CompanyDto.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
52

63
namespace Routine.APi.Models
74
{

Routine/Routine.APi/Models/EmployeeAddDto.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using Routine.APi.Entities;
2-
using Routine.APi.ValidationAttributes;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.ComponentModel.DataAnnotations;
6-
7-
namespace Routine.APi.Models
1+
namespace Routine.APi.Models
82
{
93
public class EmployeeAddDto : EmployeeAddOrUpdateDto
104
{

Routine/Routine.APi/Models/EmployeeUpdateDto.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
using Routine.APi.Entities;
2-
using Routine.APi.ValidationAttributes;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.ComponentModel.DataAnnotations;
6-
using System.Linq;
7-
using System.Threading.Tasks;
8-
9-
namespace Routine.APi.Models
1+
namespace Routine.APi.Models
102
{
113
public class EmployeeUpdateDto : EmployeeAddOrUpdateDto
124
{

Routine/Routine.APi/Program.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using Microsoft.AspNetCore.Hosting;
62
using Microsoft.EntityFrameworkCore;
7-
using Microsoft.Extensions.Configuration;
83
using Microsoft.Extensions.DependencyInjection;
94
using Microsoft.Extensions.Hosting;
105
using Microsoft.Extensions.Logging;
116
using Routine.APi.Data;
7+
using System;
128

139
namespace Routine.APi
1410
{

Routine/Routine.APi/Services/ICompanyRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Routine.APi.Entities;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Threading.Tasks;
76

87
namespace Routine.APi.Services

0 commit comments

Comments
 (0)