Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CategoryRequest categoryRequest)
        {
            Category category = ConvertFromDTO.FromCreateRequest(categoryRequest);

            _categoryRepository.Create(category);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = category.Id }, ConvertToDTO.ToCreateResponse(category)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] CreateCommentRequest commentRequest)
        {
            int id = int.Parse(HttpContext.User.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);

            if (_threadRepository.Find(commentRequest.ThreadId) != null)
            {
                Comment comment = ConvertFromDTO.FromCreateRequest(commentRequest);
                comment.UserId = id;
                _commentRepository.Create(comment);
                await _context.SaveChangesAsync();

                return(CreatedAtAction(nameof(GetByThread), new { threadId = comment.ThreadId }, ConvertToDTO.ToCreateResponse(comment)));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([FromBody] CreateThreadRequest threadRequest)
        {
            int id = int.Parse(HttpContext.User.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);

            if (_categoryRepository.Find(threadRequest.CategoryId) != null)
            {
                Thread thread = ConvertFromDTO.FromCreateRequest(threadRequest);
                thread.UserId = id;
                _threadRepository.Create(thread);
                await _context.SaveChangesAsync();

                return(CreatedAtAction(nameof(Get), new { id = thread.Id }, ConvertToDTO.ToCreateResponse(thread)));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Login([FromBody] LoginRequest request)
        {
            User user = await _userRepository.GetUser(request);

            if (user != null)
            {
                user.RefreshToken = GenerateRefreshToken();
                _userRepository.Update(user);
                await _context.SaveChangesAsync();

                var response = new LoginResponse()
                {
                    accessToken = GenerateJSONWebToken(user), refreshToken = user.RefreshToken
                };
                return(Ok(response));
            }
            else
            {
                return(Unauthorized());
            }
        }