Skip to content

Commit

Permalink
Merge pull request #26 from olmangjolmang/OMJM-45-myquestion
Browse files Browse the repository at this point in the history
티클문답 - 마이페이지에서 수정/삭제
  • Loading branch information
lej8924 authored Jul 15, 2024
2 parents b0e4f42 + 653d2af commit 5e52fe0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.authorizeHttpRequests(authorize ->
authorize.requestMatchers("/users/sign-in", "/users/sign-up", "/swagger-ui/**", "/v3/api-docs/**", "/global/health-check", "/users/**", "/post/**", "/mypage/**").permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/users/**").hasRole("USER")
.requestMatchers("/users/**","/mypage/**").hasRole("USER")
.anyRequest().authenticated())
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,44 @@
import com.ticle.server.global.dto.ResponseTemplate;
import com.ticle.server.mypage.dto.MyQuestionDto;
import com.ticle.server.mypage.service.MyPageService;
import com.ticle.server.user.jwt.JwtTokenProvider;
import com.ticle.server.user.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name="MyPage",description = "마이페이지 관련 API")
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/mypage")
public class MyQuestionController {

private final MyPageService myPageService;
private final JwtTokenProvider jwtTokenProvider;

@Operation(summary = "마이물어봥",description = "userId를 RequestParam에 넣어서 물어봥 질문들을 가져옴")
@Operation(summary = "마이물어봥",description = "Jwt token을 통해 userId를 가져온 후 물어봥 질문들을 가져옴")
@GetMapping("/my-question")
public ResponseEntity<ResponseTemplate<Object>> getMyQuestions(@RequestParam("userid") Long userId){
public ResponseEntity<ResponseTemplate<Object>> getMyQuestions(@AuthenticationPrincipal CustomUserDetails customUserDetails){

// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// if (authentication == null || !authentication.isAuthenticated()) {
// return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ResponseTemplate.EMPTY_RESPONSE);
// }

Long userId = customUserDetails.getUserId();

List<MyQuestionDto> myQuestionDtos;
myQuestionDtos = myPageService.getMyQuestions(userId);
List
Expand All @@ -34,8 +52,8 @@ public ResponseEntity<ResponseTemplate<Object>> getMyQuestions(@RequestParam("us
}
@Operation(summary = "마이물어봥 수정",description = "question_id에서 수정하기")
@PutMapping("/my-question/{id}")
public ResponseEntity<ResponseTemplate<Object>> updateQuestion(@PathVariable("id") Long questionId,
@RequestBody UpdateQuestionDto updateQuestionDto) {
public ResponseEntity<ResponseTemplate<Object>> updateQuestion(@PathVariable("id") Long questionId, @RequestBody UpdateQuestionDto updateQuestionDto) {

myPageService.updateQuestion(questionId, updateQuestionDto.getQuestionContent());

return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class SavedTicleController {
@GetMapping("/saved-ticles")
public ResponseEntity<ResponseTemplate<Object>> getSavedTicles(@AuthenticationPrincipal CustomUserDetails customUserDetails, @RequestParam(value = "category",required = false) Category category, @PageableDefault(page=0,size=9,sort="id",direction = Sort.Direction.DESC)Pageable pageable){
Long userId = customUserDetails.getUserId();

List<SavedTicleDto> savedTicleDtos;

if(category != null){
savedTicleDtos = myPageService.getSavedArticlesByCategory(userId,category,pageable);
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class EmailController {
@PostMapping("/emailSend")
public String mailConfirm(@RequestParam(value = "email", required = false) String email) throws Exception{
String code = emailService.sendSimpleMessage(email);
System.out.println("인증코드 : " + code);
return code;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ public Authentication getAuthentication(String accessToken){
if(claims.get("auth") == null){
throw new RuntimeException("권한 정보가 없는 토큰입니다");
}
// Long userId = Long.parseLong(claims.getSubject());
Collection<? extends GrantedAuthority> authorities =
Arrays.stream(claims.get("auth").toString().split(","))
.map(SimpleGrantedAuthority::new)
.collect(toList());

UserDetails principal = new User(claims.getSubject(),"",authorities);
CustomUserDetails principal = new CustomUserDetails(Long.parseLong(claims.getSubject()), "", authorities);
return new UsernamePasswordAuthenticationToken(principal,"",authorities);
}
//토큰 정보를 검증하는 메소드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public UserDto signUp(JoinRequest joinRequest){
String encodedPassword = passwordEncoder.encode(joinRequest.getPassword());
List<String> roles = new ArrayList<>();
roles.add("USER");
System.out.println("test" + joinRequest);
return UserDto.toDto(userRepository.save(joinRequest.toEntity(encodedPassword,roles)));
}

Expand Down

0 comments on commit 5e52fe0

Please sign in to comment.