public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto cmdCreateDto) { var cmd = mapper.Map <Command>(cmdCreateDto); repo.CreateCommand(cmd); repo.SaveChanges(); // This also updates the context and the objects passed to it including cmd. var cmdReadDto = mapper.Map <CommandReadDto>(cmd); return(CreatedAtRoute(nameof(GetCommandById), new { Id = cmdReadDto.Id }, cmdReadDto)); }
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)); }
public ActionResult DeleteCommand(int id) { var commandModelFromRepo = _repository.GetCommandById(id); if (commandModelFromRepo == null) { return(NotFound()); } _repository.DeleteCommand(commandModelFromRepo); _repository.SaveChanges(); return(NoContent()); }
public ActionResult CreateCommand(CommandCreateDto dto) { var cmd = _mapper.Map <Command>(dto); _repo.CreateCommand(cmd); // this add Id to cmd (by reference) _repo.SaveChanges(); var cmdReadDto = _mapper.Map <CommandReadDto>(cmd); return(CreatedAtRoute(nameof(GetCommandById), new { Id = cmdReadDto.Id }, cmdReadDto)); }
public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { var commandModel = _mapper.Map <Command>(commandCreateDto); _repository.CreateCommand(commandModel); _repository.SaveChanges(); var commandReadDto = _mapper.Map <CommandReadDto>(commandModel); // this method return 201 status code, the newly created object and the uri of the object. // for nameof(GetCommandById) to work we need to name the GetCommandById action: Name="GetCommandById" in the httpget attribute return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto)); }
public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { var commandModel = _mapper.Map <Command>(commandCreateDto); _repository.CreateCommand(commandModel); _repository.SaveChanges(); // This also updated commandModel var to reflect a valid Id value var commandReadDto = _mapper.Map <CommandReadDto>(commandModel); return(CreatedAtRoute(nameof(GetCommandById), new { id = commandReadDto.Id }, commandReadDto)); }
public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { //insert var commandModel = mapper.Map <Command>(commandCreateDto); reposService.CreateCommand(commandModel); reposService.SaveChanges(); //read back var commandReadDto = mapper.Map <CommandReadDto>(commandModel); //The CreatedAtRoute method is intended to return a URI to the newly created resource when you invoke a POST method to store some new object. //So if you POST an order item for instance, you might return a route like 'api/order/11' (11 being the id of the order obviously). return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto)); }
public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto) { var commandModel = _mapper.Map <Command>(commandCreateDto); _repository.CreateCommand(commandModel); _repository.SaveChanges(); var commandReadDto = _mapper.Map <CommandReadDto>(commandModel); return(Ok(commandReadDto)); }
public async Task <ActionResult <CommandReadDto> > CreateCommand([FromBody] CommandCreateDto cmd) { Command newCommand = _mapper.Map <Command>(cmd); await Task.Run(() => _commandAPIRepo.CreateCommand(newCommand)); await Task.Run(() => _commandAPIRepo.SaveChanges()); CommandReadDto createdCommand = _mapper.Map <CommandReadDto>(newCommand); //return Created("Success", new { data = createdCommand }); return( CreatedAtRoute( nameof(GetCommandById), new { Id = createdCommand.Id }, new { data = createdCommand } ) ); }
public ActionResult PartialCommandUpdate(int id, JsonPatchDocument <CommandUpdateDto> patchDoc) { var commandFromRepo = _repository.GetCommandsById(id); if (commandFromRepo == null) { return(NotFound()); } var commandToPatch = _mapper.Map <CommandUpdateDto>(commandFromRepo); patchDoc.ApplyTo(commandToPatch, ModelState); if (!TryValidateModel(commandToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(commandToPatch, commandFromRepo); _repository.UpdateCommand(commandFromRepo); _repository.SaveChanges(); return(NoContent()); }
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("GetCommandById", new { Id = commandReadDTO.Id }, commandReadDTO)); }