Exemplo n.º 1
0
        public async Task <IActionResult> CreateUserAsync([FromBody] RequestUserDto user, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }

            var userDto = await _userService.CreateUserAsync(user);

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

            var includeLinks = parsedMediaType.SubTypeWithoutSuffix.EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            if (includeLinks)
            {
                IEnumerable <LinkDto> links = new List <LinkDto>();
                links = CreateUserLinks(userDto.Id);
                var userWithLinks = _mapper.Map <ResponseUserLinksDto>(userDto);
                userWithLinks.Links = links;

                return(Ok(userWithLinks));
            }

            return(CreatedAtRoute("GetUser", new { userId = userDto.Id }, userDto));
        }
Exemplo n.º 2
0
        public async Task <ResponseUserDto> CreateUserAsync(RequestUserDto user)
        {
            var userEntity = _mapper.Map <User>(user);

            userEntity.Id = Guid.NewGuid();

            var createdEntity = await _userRepository.CreateUserAsync(userEntity);

            var responseDto = _mapper.Map <ResponseUserDto>(createdEntity);

            return(responseDto);
        }