findAllByOrderByModifiedAtDesc findAllByOrderByModifiedAtDesc는 Spring Data JPA에서 사용되는 메서드의 이름 규칙에 따른 메서드 이름입니다. 이 메서드는 Comment 엔티티에 대한 Spring Data JPA의 JpaRepository 인터페이스에서 정의된 메서드입니다.findAll: 이 메서드는 특정 조건 없이 해당 엔티티의 모든 레코드를 검색합니다.
By: 이 키워드는 조건을 지정할 때 사용됩니다. 즉, 어떤 속성에 대한 조건을 지정하겠다는 것을 나타냅니다.
OrderByModifiedAtDesc: 이 부분은 속성 이름과 정렬 방식을 나타냅니다.
- ModifiedAt: Comment 엔티티에 있는 modifiedAt 속성을 기준으로 정렬하겠다는 것을 나타냅니다. modifiedAt은 댓글이 수정된 시간을 나타내는 속성일 것으로 추측됩니다.
- Desc: 이 부분은 정렬 순서를 나타내며, "Desc"는 내림차순을 의미합니다. 즉, 최신 날짜 및 시간이 가장 먼저 나타납니다.
따라서, findAllByOrderByModifiedAtDesc 메서드는 Comment 엔티티의 모든 레코드를 modifiedAt 속성을 기준으로 내림차순으로 정렬하여 검색하는 기능을 제공합니다.
// Comment 다건 조회
@Transactional
public CommentListResponseDto getComments() {
List<CommentResponseDto> commentListResponseDto = commentRepository.findAllByOrderByModifiedAtDesc()
.stream().map(CommentResponseDto::of).collect(Collectors.toList());
return new CommentListResponseDto(commentListResponseDto);
}// Comment 다건 조회
@Transactional
public List<CommentResponseDto> getComments() {
List<CommentResponseDto> commentListResponseDto = commentRepository.findAllByOrderByModifiedAtDesc()
.stream().map(CommentResponseDto::of).collect(Collectors.toList());
return commentListResponseDto;
}// Comment 다건 조회
@Transactional
public List<CommentResponseDto> getComments() {
List<CommentResponseDto> commentResponseDtos = commentRepository.findAllByOrderByModifiedAtDesc()
.stream().map(CommentResponseDto::of).collect(Collectors.toList());
return commentResponseDtos;
}// Comment 다건 조회
@GetMapping("/comments")
public ResponseEntity<CommentListResponseDto> getComments() {
CommentListResponseDto commentListResponseDto = commentService.getComments();
return ResponseEntity.ok().body(commentListResponseDto);
}// Comment 다건 조회
@GetMapping("/comments")
public ResponseEntity<List<CommentResponseDto>> getComments() {
List<CommentResponseDto> commentListResponseDto = commentService.getComments();
return ResponseEntity.ok().body(commentListResponseDto);
}@GetMapping("/comments")
public ResponseEntity<List<CommentResponseDto>> getComments() {
List<CommentResponseDto> commentResponseDtos = commentService.getComments();
return ResponseEntity.ok().body(commentResponseDtos);
}Bearer ~
외부노출금지!!쿠키(Cookie):
세션(Session):
쿠키와 세션의 차이점:
웹 개발에서는 쿠키와 세션을 조합하여 사용자 경험을 최적화하고 필요한 정보를 안전하게 관리하는 데 활용합니다.
1. 데이터 저장 위치:
2. 데이터 용량:
3. 데이터 보안:
4. 수명과 만료:
5. 자원 사용:
요약하면, 쿠키는 작은 데이터를 클라이언트에 저장하는 반면 세션은 더 많은 데이터를 서버에 저장하여 보안성과 용량 면에서 더 강력한 해결책을 제공합니다.
쿠키의 예제:
가상의 웹 사이트에서 로그인 상태를 유지하기 위해 쿠키를 사용하는 예제를 생각해보겠습니다. 사용자가 로그인할 때, 서버는 로그인 정보를 확인하고 클라이언트에 쿠키를 설정하여 로그인 상태를 기억합니다.
plaintextCopy code
// 서버 응답
Set-Cookie: user_id=12345; expires=Wed, 24-Aug-2023 23:59:59 GMT; path=/
// 클라이언트 요청
GET /dashboard HTTP/1.1
Cookie: user_id=12345
위 예제에서, 서버가 user_id라는 쿠키를 설정하여 사용자를 식별합니다. 쿠키는 클라이언트에 저장되며, 만료 기한까지 유지됩니다. 클라이언트가 다시 서버에 요청을 보낼 때, 쿠키가 함께 전송되어 로그인 상태를 유지합니다.
세션의 예제:
웹 사이트에서 쇼핑 카트를 관리하기 위해 세션을 사용하는 예제를 살펴보겠습니다. 사용자가 상품을 카트에 추가할 때, 서버는 해당 사용자의 세션에 선택한 상품을 저장합니다.
pythonCopy code
# 서버에서의 세션 관리 (파이썬 예시)
from flask import Flask, session, request
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/add_to_cart', methods=['POST'])
def add_to_cart():
product_id = request.form['product_id']
if 'cart' not in session:
session['cart'] = []
session['cart'].append(product_id)
return 'Product added to cart!'
@app.route('/cart')
def view_cart():
if 'cart' in session:
cart_items = session['cart']
return f'Your cart: {cart_items}'
else:
return 'Your cart is empty.'
if __name__ == '__main__':
app.run()
위 예제에서, 사용자의 카트 정보는 서버의 세션에 저장됩니다. 사용자가 상품을 추가하면 세션 데이터에 반영되며, 세션은 서버 측에서 관리되므로 보안성이 높아집니다.
@Getter
@Setter
@Builder
public class CommentResponseDto extends ApiResponseDto {
private Long id;
private String content;
private String username;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
public static CommentResponseDto of(Comment comment) {
return CommentResponseDto.builder()
.id(comment.getId())
.content(comment.getContent())
.username(comment.getAuthor().getUsername())
.createdAt(comment.getCreatedAt())
.modifiedAt(comment.getModifiedAt())
.build();
}
}@Getter
@Builder
public class CommentRequestDto {
private String content;
public Comment toEntity(Post post, User user) {
Comment comment = Comment.builder()
.content(this.content)
.post(post)
.author(user)
.build();
return comment;
}
Comment comment = commentRequestDto.toEntity(post, user);