Exemplo 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());
            }
        }
Exemplo 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());
            }
        }
Exemplo n.º 3
0
        public ActionResult <PlatformReadDto> CreatePlatform(PlatformCreateDto platformCreate)
        {
            try
            {
                //Validations
                if (platformCreate == null)
                {
                    return(NotFound());
                }

                //Create
                var platform = _mapper.Map <Platform>(platformCreate);
                _repository.CreatePlatform(platform);
                _repository.SaveChanges();

                //Read
                var platformRead = _repository.GetPlatformById(platform.Id);

                //Response
                return(CreatedAtRoute(nameof(GetPlatformById), new { Id = platformRead.Id }, platformRead));
            }
            catch (Exception)
            {
                return(NotFound());
            }
        }
        public ActionResult <CommandReadDto> CreateCommand(CommandCreateDto commandCreateDto)
        {
            var createCommand = _mapper.Map <Command>(commandCreateDto);

            _repo.CreateCommand(createCommand);
            _repo.SaveChanges();
            var commandReadDto = _mapper.Map <CommandReadDto>(createCommand);

            return(CreatedAtRoute(nameof(GetCommandById), new { Id = commandReadDto.Id }, commandReadDto));
        }
Exemplo 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));
        }
        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));
        }
Exemplo 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));
        }
Exemplo 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
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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));
        }
Exemplo n.º 11
0
        public async Task <ActionResult> UpdateCommand(int id, CommandUpdateDto commandUpdateDto)
        {
            Command command = await _repository.GetCommand(id);

            if (command == null)
            {
                return(NotFound());
            }

            _mapper.Map(commandUpdateDto, command);

            await _repository.SaveChanges();

            return(NoContent());
        }