Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
req -> req
.requestMatchers("/api/auth/**")
.requestMatchers("/api/**")
Copy link
Owner

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?

.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you changed back to this pattern? We already have RequestMapping.

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));
}
Expand Down
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend using constructor injection (e.g., with Lombok's @RequiredArgsConstructor) instead of field injection using @Autowired. It is preferred option for testing.

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;
}
32 changes: 32 additions & 0 deletions src/main/java/com/huseynovvusal/springblogapi/model/Comment.java
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
@@ -1,9 +1,7 @@
package com.huseynovvusal.springblogapi.model;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.*;

import java.util.List;

Expand Down
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);
}
}