Ejemplo n.º 1
0
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreate)
        {
            try
            {
                //Get platform
                var platform = _repository.GetPlatformById(commandCreate.PlatformId);
                if (platform == null)
                {
                    return(NotFound());
                }

                //Maper
                var commandModel = _mapper.Map <Command>(commandCreate);
                commandModel.Platform = platform;

                //Create
                _repository.CreateCommand(commandModel);
                _repository.SaveChanges();

                //Read
                var commandReadDto = _mapper.Map <CommandReadDto>(commandModel);
                return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
                // return Created(commandReadDto, );
            }
            catch (Exception)
            {
                return(NotFound());
            }
        }
Ejemplo n.º 2
0
        public ActionResult <CommandDtoRead> CreateNewCommand(CommandDtoWrite commandDtoWrite)
        {
            var command = _mapper.Map <Command>(commandDtoWrite);

            _repo.CreateCommand(command);

            try
            {
                var result = _repo.SaveChanges();

                if (result)
                {
                    var respondData = _mapper.Map <CommandDtoRead>(command);
                    return(CreatedAtRoute(nameof(GetCommandById), new { Id = respondData.Id }, respondData));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (System.Exception)
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <CommandReadDto> > CreateCommand(CommandCreateDto commandCreateDto)
        {
            Command command = _mapper.Map <Command>(commandCreateDto);
            await _repository.CreateCommand(command);

            CommandReadDto commandReadDto = _mapper.Map <CommandReadDto>(command);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
        }
Ejemplo n.º 4
0
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
        {
            var command = _mapper.Map <Command>(commandCreateDto);

            _commandRepo.CreateCommand(command);

            var commandReadDto = _mapper.Map <CommandReadDto>(command);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
        }
Ejemplo n.º 5
0
        public ActionResult <CommandCreateDto> CreateCommand(CommandCreateDto commandCreate)
        {
            var commandModel = _mapper.Map <Command>(commandCreate);

            _repository.CreateCommand(commandModel);
            _repository.SaveChanges();
            var dtoCommandRead = _mapper.Map <CommandReadDto>(commandModel);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = dtoCommandRead.Id }, dtoCommandRead));
        }
Ejemplo n.º 6
0
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
        {
            var commandToCreate = _mapper.Map <Command>(commandCreateDto);

            _repository.CreateCommand(commandToCreate);
            _repository.SaveChanges();

            var commandToReturn = _mapper.Map <CommandReadDto>(commandToCreate);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandToReturn.Id }, commandToReturn));
        }
Ejemplo n.º 7
0
        public ActionResult <CommandReadDto> CreateCommand([FromForm] CommandCreateDto commandCreateDto)
        {
            var commandModel = _mapper.Map <Command>(commandCreateDto);

            //validations
            _repository.CreateCommand(commandModel);
            _repository.SaveChanges();

            var commandReadDto = _mapper.Map <CommandReadDto>(commandModel);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
        }
Ejemplo n.º 8
0
        public ActionResult <CommandCreateDto> CreateCommand(CommandCreateDto commandCreateDto) // not Command cmd? worth noting
        {                                                                                       // we want to map to the object (Command), and the source is 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)); // 201 Created, it gives full url to the file
                                                                                                            // to check this stuff checkout docs.microsoft.net about ApiController.CreatedAtRoute
                                                                                                            //    return Ok(commandReadDto); // not final
        }
Ejemplo n.º 9
0
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto command)
        {
            var commanModal = _mapper.Map <Command>(command);

            _repo.CreateCommand(commanModal);
            _repo.SaveChanges();

            var commandReadDto = _mapper.Map <CommandReadDto>(commanModal);

            //return Ok(commandReadDto);

            return(CreatedAtRoute(nameof(getCommand), new{ Id = commandReadDto.Id }, commandReadDto));
        }
Ejemplo n.º 10
0
        public ActionResult<CommandReadDto> CreateCommand(CreateCommandDto createCommandDto)
        {
            //mapping CreateCommnad to Command resource ....Map-> createCommandDto to Command
            var commnad = _mapper.Map<Command>(createCommandDto);
            _repository.CreateCommand(commnad);
            _repository.SaveChanges();

            var commandReadDto = _mapper.Map<CommandReadDto>(commnad);

            //getting a single resource location -getbyId and creatng anonymous object ->new{id=commandReadDto.Id} and passing dto object
            return CreatedAtRoute(nameof(GetById), new { id = commandReadDto.Id }, commandReadDto);

            //  return Ok(commandReadDto);
        }
Ejemplo n.º 11
0
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
        {
            //Convert API request body(Mapping model) into the Coomand domain model
            var commandModel = _mapper.Map <Command>(commandCreateDto);

            //The converted model (Domain model) to the CreateCommand()
            _repository.CreateCommand(commandModel);
            _repository.SaveChanges();
            var commandReadDto = _mapper.Map <CommandReadDto>(commandModel);

            //return uri resource
            //localhost:44388/api/Commands/5
            //It is really important to pass back the uir resource in POST method, which is the principle of REST
            //routeName(string), routeValues(Obj), content(Obj)
            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
        }
Ejemplo n.º 12
0
        public ActionResult <CommandReadDto> CreateCommandForPlatform(int platformId, CommandCreateDto commandCreateDto)
        {
            Console.WriteLine($"--> Hit {nameof(CreateCommandForPlatform)}, platformId: {platformId}");

            if (!_repo.PlatformExists(platformId))
            {
                return(NotFound());
            }

            var command = _mapper.Map <Command>(commandCreateDto);

            _repo.CreateCommand(platformId, command);

            var commandReadDto = _mapper.Map <CommandReadDto>(command);

            return(CreatedAtRoute(
                       nameof(GetCommandForPlatform),
                       new { platformId, commandId = command.Id }, commandReadDto));
        }