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
1 change: 1 addition & 0 deletions src/main/java/org/example/expert/config/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
httpRequest.setAttribute("userId", Long.parseLong(claims.getSubject()));
httpRequest.setAttribute("email", claims.get("email"));
httpRequest.setAttribute("userRole", claims.get("userRole"));
httpRequest.setAttribute("nickName", claims.get("nickName")); // 닉네임을 사용하기 위해 request에 담기

if (url.startsWith("/admin")) {
// 관리자 권한이 없는 경우 403을 반환합니다.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/example/expert/config/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public void init() {
key = Keys.hmacShaKeyFor(bytes);
}

public String createToken(Long userId, String email, UserRole userRole) {
public String createToken(Long userId, String email, UserRole userRole, String nickName) {
Date date = new Date();

return BEARER_PREFIX +
Jwts.builder()
.setSubject(String.valueOf(userId))
.claim("email", email)
.claim("userRole", userRole)
.claim("nickName", nickName) //JWT에서 닉네임을 보여주기 위해 닉네임을 넣음
.setExpiration(new Date(date.getTime() + TOKEN_TIME))
.setIssuedAt(date) // 발급일
.signWith(key, signatureAlgorithm) // 암호화 알고리즘
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequiredArgsConstructor
public class TodoController {
Expand All @@ -29,9 +31,12 @@ public ResponseEntity<TodoSaveResponse> saveTodo(
@GetMapping("/todos")
public ResponseEntity<Page<TodoResponse>> getTodos(
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String weather,
@RequestParam LocalDate startDate,
@RequestParam LocalDate endDate
) {
return ResponseEntity.ok(todoService.getTodos(page, size));
return ResponseEntity.ok(todoService.getTodos(page, size, weather, startDate, endDate));
}

@GetMapping("/todos/{todoId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public class TodoSaveRequest {
private String title;
@NotBlank
private String contents;
@NotBlank
private String nickName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ public class TodoResponse {
private final Long id;
private final String title;
private final String contents;
private String nickName;
private final String weather;
private final UserResponse user;
private final LocalDateTime createdAt;
private final LocalDateTime modifiedAt;

public TodoResponse(Long id, String title, String contents, String weather, UserResponse user, LocalDateTime createdAt, LocalDateTime modifiedAt) {
public TodoResponse(Long id, String title, String contents,String nickName, String weather, UserResponse user, LocalDateTime createdAt, LocalDateTime modifiedAt) {
this.id = id;
this.title = title;
this.contents = contents;
this.nickName = nickName;
this.weather = weather;
this.user = user;
this.createdAt = createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Todo extends Timestamped {
private String title;
private String contents;
private String weather;
private String nickName;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.Optional;

public interface TodoRepository extends JpaRepository<Todo, Long> {

@Query("SELECT t FROM Todo t LEFT JOIN FETCH t.user u ORDER BY t.modifiedAt DESC")
Page<Todo> findAllByOrderByModifiedAtDesc(Pageable pageable);
@Query("SELECT t FROM Todo t LEFT JOIN FETCH t.user u" +
"WHERE(:weather IS NULL OR t.weather = :weather) " +
"AND (:startDate IS NULL OR t.modifiedAt >= :startDate)" +
"AND (:endDate IS NULL OR t.modifiedAt <= :endDate)" +
"ORDER BY t.modifiedAt DESC")
Page<Todo> findAllByOrderByModifiedAtDesc(
@Param("weather") String weather,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate,
Pageable pageable
);

@Query("SELECT t FROM Todo t " +
"LEFT JOIN t.user " +
"WHERE t.id = :todoId")
Optional<Todo> findByIdWithUser(@Param("todoId") Long todoId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)

public class TodoService {

private final TodoRepository todoRepository;
Expand Down Expand Up @@ -47,7 +47,7 @@ public TodoSaveResponse saveTodo(AuthUser authUser, TodoSaveRequest todoSaveRequ
);
}

public Page<TodoResponse> getTodos(int page, int size) {
public Page<TodoResponse> getTodos(int page, int size, String weather, String startDate, String endDate) {
Pageable pageable = PageRequest.of(page - 1, size);

Page<Todo> todos = todoRepository.findAllByOrderByModifiedAtDesc(pageable);
Expand All @@ -57,6 +57,7 @@ public Page<TodoResponse> getTodos(int page, int size) {
todo.getTitle(),
todo.getContents(),
todo.getWeather(),
todo.getUser().getNickName(),
new UserResponse(todo.getUser().getId(), todo.getUser().getEmail()),
todo.getCreatedAt(),
todo.getModifiedAt()
Expand All @@ -74,6 +75,7 @@ public TodoResponse getTodo(long todoId) {
todo.getTitle(),
todo.getContents(),
todo.getWeather(),
todo.getUser().getNickName(),
new UserResponse(user.getId(), user.getEmail()),
todo.getCreatedAt(),
todo.getModifiedAt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.expert.domain.user.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.expert.domain.common.dto.AuthUser;
Expand All @@ -21,10 +22,16 @@ public class User extends Timestamped {
@Enumerated(EnumType.STRING)
private UserRole userRole;

public User(String email, String password, UserRole userRole) {
@NotBlank
@Column(unique = false)
private String nickName;


public User(String email, String password, UserRole userRole, String nickName) {
this.email = email;
this.password = password;
this.userRole = userRole;
this.nickName = nickName;
}

private User(Long id, String email, UserRole userRole) {
Expand Down