- 
                Notifications
    
You must be signed in to change notification settings  - Fork 22
 
Feature/comment #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feature/comment #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -11,31 +11,29 @@ | |
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| 
     | 
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/auth") | ||
| public class AuthenticationController { | ||
| 
     | 
||
| private final AuthenticationService authenticationService; | ||
| 
     | 
||
| @PostMapping("register") | ||
| @PostMapping("/api/auth/register") | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why you changed back to this pattern? We already have   | 
||
| public ResponseEntity<RegisterResponse> register(@RequestBody RegisterRequest request) { | ||
| return ResponseEntity.ok(authenticationService.register(request)); | ||
| } | ||
| 
     | 
||
| @PostMapping("login") | ||
| @PostMapping("/api/auth/login") | ||
| public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) { | ||
| return ResponseEntity.ok(authenticationService.login(request)); | ||
| } | ||
| 
     | 
||
| @PostMapping("forgot-password") | ||
| @PostMapping("/api/auth/forgot-password") | ||
| public ResponseEntity<ForgotPasswordResponse> getPasswordResetLink(@Valid @RequestBody ForgotPasswordRequest request){ | ||
| return ResponseEntity.ok(authenticationService.generatePasswordResetToken(request)); | ||
| } | ||
| 
     | 
||
| @PostMapping("reset-password") | ||
| @PostMapping("api/auth/reset-password") | ||
| public ResponseEntity<ResetPasswordResponse> resetPassword(@Valid @RequestBody ResetPasswordRequest request){ | ||
| return ResponseEntity.ok(authenticationService.verifyAndResetPassword(request)); | ||
| } | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.huseynovvusal.springblogapi.controller; | ||
| 
     | 
||
| import com.huseynovvusal.springblogapi.dto.CommentRequest; | ||
| import com.huseynovvusal.springblogapi.model.Comment; | ||
| import com.huseynovvusal.springblogapi.service.CommentService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.web.PageableDefault; | ||
| import org.springframework.web.bind.annotation.*; | ||
| 
     | 
||
| import java.util.List; | ||
| 
     | 
||
| @RestController | ||
| @RequestMapping("/api/comments") | ||
| public class CommentController { | ||
| @Autowired | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend using constructor injection (e.g., with Lombok's   | 
||
| private CommentService commentService; | ||
| 
     | 
||
| @PostMapping("/create") | ||
| public Comment createComments(@RequestBody Comment content){ | ||
| return commentService.addComments(content); | ||
| } | ||
| 
     | 
||
| @GetMapping("/listAll") | ||
| public List<Comment> listComments(@PageableDefault(page = 0, size = 10, sort = "id") Pageable pageable) { | ||
| return commentService.listComments(pageable); | ||
| } | ||
| 
     | 
||
| @DeleteMapping("/{id}") | ||
| public Comment deleteComments(@PathVariable Long id){ | ||
| return commentService.deleteComments(id); | ||
| } | ||
| 
     | 
||
| @PatchMapping("/{id}") | ||
| public Comment updateComments(@PathVariable Long id, @RequestBody CommentRequest dto){ | ||
| return commentService.updateComments(id, dto); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.huseynovvusal.springblogapi.dto; | ||
| 
     | 
||
| import com.huseynovvusal.springblogapi.model.User; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| 
     | 
||
| @Getter | ||
| @Setter | ||
| @AllArgsConstructor | ||
| public class CommentRequest { | ||
| private Long id; | ||
| private String content; | ||
| private User user; | ||
| private String username; | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.huseynovvusal.springblogapi.model; | ||
| 
     | 
||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
| 
     | 
||
| @Entity | ||
| @Table(name = "comments") | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| public class Comment { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(nullable = false) | ||
| private Long id; | ||
| 
     | 
||
| private String content; | ||
| 
     | 
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| @JsonIgnore | ||
| private User user; | ||
| 
     | 
||
| private String username; | ||
| 
     | 
||
| public Comment(Long id, String content, String username) { | ||
| this.id = id; | ||
| this.content = content; | ||
| this.username = username; | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.huseynovvusal.springblogapi.repository; | ||
| 
     | 
||
| import com.huseynovvusal.springblogapi.model.Comment; | ||
| import org.springframework.data.repository.PagingAndSortingRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| 
     | 
||
| @Repository | ||
| public interface CommentRepository extends PagingAndSortingRepository<Comment, Long> { | ||
| Comment deleteById(Long id); | ||
| 
     | 
||
| Comment save(Comment content); | ||
| 
     | 
||
| Comment findById(Long id); | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.huseynovvusal.springblogapi.service; | ||
| 
     | 
||
| import com.huseynovvusal.springblogapi.dto.CommentRequest; | ||
| import com.huseynovvusal.springblogapi.model.Comment; | ||
| import com.huseynovvusal.springblogapi.model.User; | ||
| import com.huseynovvusal.springblogapi.repository.CommentRepository; | ||
| import com.huseynovvusal.springblogapi.repository.UserRepository; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
| 
     | 
||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| 
     | 
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| 
     | 
||
| @Service | ||
| public class CommentService { | ||
| private CommentRepository commentRepository; | ||
| private UserRepository userRepository; | ||
| 
     | 
||
| public CommentService(CommentRepository commentRepository, UserRepository userRepository) { | ||
| this.commentRepository = commentRepository; | ||
| this.userRepository = userRepository; | ||
| } | ||
| 
     | 
||
| public Comment addComments(Comment content){ | ||
| Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
| String username = auth.getName(); | ||
| 
     | 
||
| User user = userRepository.findByUsername(username); | ||
| 
     | 
||
| content.setUser(user); | ||
| return commentRepository.save(content); | ||
| } | ||
| 
     | 
||
| public List<Comment> listComments(Pageable pageable){ | ||
| return commentRepository.findAll(pageable) | ||
| .stream() | ||
| .map(c -> new Comment(c.getId(), c.getContent(), c.getUser().getUsername())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| 
     | 
||
| public Comment deleteComments(Long id){ | ||
| return commentRepository.deleteById(id); | ||
| } | ||
| 
     | 
||
| public Comment updateComments(Long id, CommentRequest dto){ | ||
| Comment comment = commentRepository.findById(id); | ||
| 
     | 
||
| if(dto.getContent() != null){ | ||
| comment.setContent(dto.getContent()); | ||
| } | ||
| 
     | 
||
| return commentRepository.save(comment); | ||
| } | ||
| } | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose here?