|
1 | 1 | using AutoMapper; |
2 | 2 | using Microsoft.AspNetCore.JsonPatch; |
3 | 3 | using Microsoft.AspNetCore.Mvc; |
| 4 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Options; |
4 | 7 | using Routine.APi.Entities; |
5 | 8 | using Routine.APi.Models; |
6 | 9 | using Routine.APi.Services; |
@@ -168,20 +171,56 @@ public async Task<IActionResult> PartiallyUpdateEmployeeForCompany(Guid companyI |
168 | 171 | var employeeEntity = await _companyRepository.GetEmployeeAsync(companyId, employeeId); |
169 | 172 | if (employeeEntity == null) |
170 | 173 | { |
171 | | - return NotFound(); |
172 | | - } |
| 174 | + //不允许客户端生成 Guid |
| 175 | + //return NotFound(); |
173 | 176 |
|
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 | + } |
175 | 185 |
|
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); |
177 | 191 |
|
| 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) |
178 | 199 | patchDocument.ApplyTo(dtoToPatch); |
| 200 | + //验证模型 |
| 201 | + if (!TryValidateModel(dtoToPatch)) |
| 202 | + { |
| 203 | + return ValidationProblem(ModelState); //返回状态码与错误信息 |
| 204 | + } |
179 | 205 | _mapper.Map(dtoToPatch, employeeEntity); |
180 | 206 | _companyRepository.UpdateEmployee(employeeEntity); |
181 | 207 | await _companyRepository.SaveAsync(); |
182 | 208 | return NoContent(); //返回状态码204 |
183 | 209 | } |
184 | 210 |
|
| 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 | + |
185 | 224 | [HttpOptions] |
186 | 225 | public IActionResult GetCompaniesOptions() |
187 | 226 | { |
|
0 commit comments