《Asp.Net Core3 + Vue3入坑教程》 - 3.AutoMapper DI (3)

代码如下:

using System.ComponentModel.DataAnnotations; namespace Simple_Asp.Net_Core.Dtos { public class CommandCreateDto { [Required] [MaxLength(250)] public string HowTo { get; set; } [Required] public string Line { get; set; } [Required] public string Platform { get; set; } } } using System.ComponentModel.DataAnnotations; namespace Simple_Asp.Net_Core.Dtos { public class CommandUpdateDto { [Required] [MaxLength(250)] public string HowTo { get; set; } [Required] public string Line { get; set; } [Required] public string Platform { get; set; } } } 修改CommandsProfile.cs

代码如下:

using AutoMapper; using Simple_Asp.Net_Core.Dtos; using Simple_Asp.Net_Core.Models; namespace Simple_Asp.Net_Core.Profiles { public class CommandsProfile : Profile { public CommandsProfile() { //Source -> Target CreateMap<Command, CommandReadDto>(); CreateMap<CommandCreateDto, Command>(); CreateMap<CommandUpdateDto, Command>(); CreateMap<Command, CommandUpdateDto>(); } } } 修改ICommanderRepo.cs

代码如下:

using Simple_Asp.Net_Core.Models; using System.Collections.Generic; namespace Simple_Asp.Net_Core.Data { public interface ICommanderRepo { bool SaveChanges(); IEnumerable<Command> GetAllCommands(); Command GetCommandById(int id); void CreateCommand(Command cmd); void UpdateCommand(Command cmd); void DeleteCommand(Command cmd); } } 修改MockCommanderRepo.cs

模拟仓储层我们就不做过多的实现,在下一章内容会与数据库Postgresql进行对接,到时候再实现!

代码如下:

using Simple_Asp.Net_Core.Models; using System.Collections.Generic; namespace Simple_Asp.Net_Core.Data { public class MockCommanderRepo : ICommanderRepo { public void CreateCommand(Command cmd) { throw new System.NotImplementedException(); } public void DeleteCommand(Command cmd) { throw new System.NotImplementedException(); } public IEnumerable<Command> GetAllCommands() { var commands = new List<Command> { new Command{Id=0, HowTo="Boil an egg", Line="Boil water", Platform="Kettle & Pan"}, new Command{Id=1, HowTo="Cut bread", Line="Get a knife", Platform="knife & chopping board"}, new Command{Id=2, HowTo="Make cup of tea", Line="Place teabag in cup", Platform="Kettle & cup"} }; return commands; } public Command GetCommandById(int id) { return new Command { Id = 0, HowTo = "Boil an egg", Line = "Boil water", Platform = "Kettle & Pan" }; } public bool SaveChanges() { throw new System.NotImplementedException(); } public void UpdateCommand(Command cmd) { throw new System.NotImplementedException(); } } } 修改CommandsController.cs

代码如下:

using AutoMapper; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc; using Simple_Asp.Net_Core.Data; using Simple_Asp.Net_Core.Dtos; using Simple_Asp.Net_Core.Models; using System.Collections.Generic; namespace Simple_Asp.Net_Core.Controllers { [Route("api/[controller]")] [ApiController] public class CommandsController : ControllerBase { private readonly ICommanderRepo _repository; private readonly IMapper _mapper; public CommandsController(ICommanderRepo repository, IMapper mapper) { _repository = repository; _mapper = mapper; } //GET api/commands [HttpGet] public ActionResult<IEnumerable<CommandReadDto>> GetAllCommmands() { var commandItems = _repository.GetAllCommands(); return Ok(_mapper.Map<IEnumerable<CommandReadDto>>(commandItems)); } //GET api/commands/{id} [HttpGet("{id}", Name = "GetCommandById")] public ActionResult<CommandReadDto> GetCommandById(int id) { var commandItem = _repository.GetCommandById(id); if (commandItem != null) { return Ok(_mapper.Map<CommandReadDto>(commandItem)); } return NotFound(); } //POST api/commands [HttpPost] public ActionResult<CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { var commandModel = _mapper.Map<Command>(commandCreateDto); _repository.CreateCommand(commandModel); _repository.SaveChanges(); var commandReadDto = _mapper.Map<CommandReadDto>(commandModel); return CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto); } //PUT api/commands/{id} [HttpPut("{id}")] public ActionResult UpdateCommand(int id, CommandUpdateDto commandUpdateDto) { var commandModelFromRepo = _repository.GetCommandById(id); if (commandModelFromRepo == null) { return NotFound(); } _mapper.Map(commandUpdateDto, commandModelFromRepo); _repository.UpdateCommand(commandModelFromRepo); _repository.SaveChanges(); return NoContent(); } //PATCH api/commands/{id} [HttpPatch("{id}")] public ActionResult PartialCommandUpdate(int id, JsonPatchDocument<CommandUpdateDto> patchDoc) { var commandModelFromRepo = _repository.GetCommandById(id); if (commandModelFromRepo == null) { return NotFound(); } var commandToPatch = _mapper.Map<CommandUpdateDto>(commandModelFromRepo); patchDoc.ApplyTo(commandToPatch); if (!TryValidateModel(commandToPatch)) { return ValidationProblem(ModelState); } _mapper.Map(commandToPatch, commandModelFromRepo); _repository.UpdateCommand(commandModelFromRepo); _repository.SaveChanges(); return NoContent(); } //DELETE api/commands/{id} [HttpDelete("{id}")] public ActionResult DeleteCommand(int id) { var commandModelFromRepo = _repository.GetCommandById(id); if (commandModelFromRepo == null) { return NotFound(); } _repository.DeleteCommand(commandModelFromRepo); _repository.SaveChanges(); return NoContent(); } } } 实现HttpPatch的时候需要引入JsonPath Nuget包,可以直接如图所示直接引入,也可以使用Nuget包管理界面进行引入

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspwfz.html