Skip to content

Commit

Permalink
[Refactor] 사용자 이름 return
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyunio committed Dec 26, 2024
1 parent df6a99e commit 0de98a8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jiyunio.todolist.domain.member.dto.req.SignInReq;
import com.jiyunio.todolist.domain.member.dto.req.SignUpReq;
import com.jiyunio.todolist.domain.member.dto.res.MemberRes;
import com.jiyunio.todolist.domain.member.dto.res.SignInRes;
import com.jiyunio.todolist.global.customError.ErrorDTO;
import com.jiyunio.todolist.global.jwt.CustomUserDetails;
import com.jiyunio.todolist.global.jwt.JwtDTO;
Expand Down Expand Up @@ -43,7 +44,7 @@ public ResponseEntity<MemberRes> signUp(@Valid @RequestBody SignUpReq signUpReq)
@ApiResponse(responseCode = "200", description = "로그인 성공", content = @Content(schema = @Schema(implementation = MemberRes.class)))
@ApiResponse(responseCode = "400", description = "빈칸", content = @Content(schema = @Schema(implementation = ErrorDTO.class)))
@ApiResponse(responseCode = "401", description = "회원 및 비밀번호 불일치", content = @Content(schema = @Schema(implementation = ErrorDTO.class)))
public ResponseEntity<JwtDTO> signIn(@Valid @RequestBody SignInReq signInReq) {
public ResponseEntity<SignInRes> signIn(@Valid @RequestBody SignInReq signInReq) {
return ResponseEntity.ok(memberService.signIn(signInReq));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.jiyunio.todolist.domain.member.dto.req.SignInReq;
import com.jiyunio.todolist.domain.member.dto.req.SignUpReq;
import com.jiyunio.todolist.domain.member.dto.res.MemberRes;
import com.jiyunio.todolist.domain.member.dto.res.SignInRes;
import com.jiyunio.todolist.domain.todo.TodoService;
import com.jiyunio.todolist.global.customError.CustomException;
import com.jiyunio.todolist.global.customError.ErrorCode;
import com.jiyunio.todolist.global.jwt.CustomAuthenticationProvider;
import com.jiyunio.todolist.global.jwt.JwtDTO;
import com.jiyunio.todolist.global.jwt.JwtProvider;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -65,10 +65,15 @@ public MemberRes signUp(@Valid SignUpReq signUpReq) {
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NOT_SAME_CONFIRM_PASSWORD);
}

public JwtDTO signIn(@Valid SignInReq signInReq) {
public SignInRes signIn(@Valid SignInReq signInReq) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(signInReq.getUserId(), signInReq.getUserPw());
Authentication authentication = authenticationProvider.authenticate(authenticationToken);
return jwtProvider.createToken(authentication);
String token = jwtProvider.createToken(authentication);

return SignInRes.builder()
.userId(memberRepository.findByUserId(signInReq.getUserId()).get().getNickname())
.token(token)
.build();
}

public MemberRes getMember(String userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class SignInRes {
@NotNull
@Schema(description = "회원의 id", example = "1")
private Long memberId;

@NotBlank
@Schema(description = "회원의 userId", example = "qwe123")
private String userId;

@NotBlank
@Schema(description = "회원의 token")
private String token;

@Builder
protected SignInRes(Long memberId, String userId, String token) {
this.memberId = memberId;
this.userId = userId;
this.token = token;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/jiyunio/todolist/domain/todo/TodoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public ResponseEntity<ResponseDTO> deleteTodo(@Parameter(description = "todo의
.msg("todo 삭제 성공")
.build());
}

@DeleteMapping("/{todoListId}")
@Operation(summary = "todo list 삭제")
@ApiResponse(responseCode = "200", description = "todo list 삭제 성공", content = @Content(schema = @Schema(implementation = ResponseDTO.class)))
public ResponseEntity<ResponseDTO> deleteTodoList(@Parameter(description = "todo의 id") @PathVariable Long todoListId) {
todoService.deleteTodoList(todoListId);
return ResponseEntity.ok(ResponseDTO.builder()
.msg("todolist 삭제 성공")
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.jiyunio.todolist.global.customError.CustomException;
import com.jiyunio.todolist.global.customError.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

Expand All @@ -22,6 +23,7 @@
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class TodoService {
Expand All @@ -48,6 +50,7 @@ public TodoRes createTodo(String userId, CreateTodoReq createTodo) {
.categoryColor(category.getColor())
.build();

todoRepository.save(todo);

TodoList todoList = todoListRepository.findByUserIdAndTodoListDate(member.getUserId(), createTodo.getSetDate()).orElseThrow(
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_TODOLIST)
Expand All @@ -67,7 +70,6 @@ public TodoListRes createTodoList(String userId, LocalDate todoListDate) {
TodoList todoList = TodoList.builder()
.userId(member.getUserId())
.todoListDate(todoListDate)
.isToday(checkToday(todoListDate))
.todos(new ArrayList<>())
.build();

Expand Down Expand Up @@ -125,8 +127,4 @@ public void deleteAllTodo(String userId) {
todoListRepository.deleteAllByUserId(userId);
}

public boolean checkToday(LocalDate localDate) {
LocalDate today = LocalDate.now();
return Objects.equals(today, localDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ public class TodoList {

private LocalDate todoListDate;

private boolean isToday;

@OneToMany(cascade = CascadeType.ALL)
private List<Todo> todos = new ArrayList<>();

@Builder
private TodoList(LocalDate todoListDate, String userId, boolean isToday, List<Todo> todos) {
private TodoList(LocalDate todoListDate, String userId, List<Todo> todos) {
this.todoListDate = todoListDate;
this.userId = userId;
this.isToday = isToday;
this.todos = todos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static TodoListRes from(TodoList todoList) {
return TodoListRes.builder()
.todoListId(todoList.getId())
.todoListDate(todoList.getTodoListDate())
.isToday(todoList.isToday())
.todoRes(todoList.getTodos().stream().map(TodoRes::from).toList())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SecretKey getSecretKey() {
return Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));
}

public JwtDTO createToken(Authentication authentication) {
public String createToken(Authentication authentication) {
Date now = new Date();
String authority = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
Expand All @@ -48,7 +48,7 @@ public JwtDTO createToken(Authentication authentication) {
.signWith(getSecretKey())
.compact();

return new JwtDTO(accessToken);
return accessToken;
}

//SecurityContext에 인증된 클라이언트의 Authentication를 저장하기 위해 사용됨
Expand Down

0 comments on commit 0de98a8

Please sign in to comment.