Skip to content

Commit

Permalink
[REFACT] README upload - @AuthenticationPrincipal로 UserId를 받아 사용하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
bbabbi committed Dec 2, 2024
1 parent 458b98d commit 71f8534
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package org.autorepo.server.domain.readme.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.net.httpserver.Authenticator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.autorepo.server.domain.readme.dto.request.ReadmeRequest;
import org.autorepo.server.domain.readme.dto.request.UploadReadmeRequest;
import org.autorepo.server.domain.readme.dto.response.ReadmeResponse;
import org.autorepo.server.domain.readme.entity.Readme;
import org.autorepo.server.domain.readme.service.CreateReadmeService;
import org.autorepo.server.domain.readme.service.UploadReadmeService;
import org.autorepo.server.domain.template.dto.request.ShareTemplateRequestDto;
import org.autorepo.server.domain.user.repository.UserRepository;
import org.autorepo.server.global.common.SuccessResponse;
import org.autorepo.server.global.error.ErrorCode;
import org.autorepo.server.global.error.exception.EntityNotFoundException;
import org.autorepo.server.global.utils.GitHubService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import static org.autorepo.server.global.error.ErrorCode.USER_NOT_FOUND;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/readme")
Expand All @@ -46,10 +37,11 @@ public ResponseEntity<ReadmeResponse> generateAndSaveReadme(@RequestBody ReadmeR
}

@PutMapping("/upload")
public ResponseEntity<SuccessResponse<?>> uploadReadme(
@RequestBody UploadReadmeRequest uploadReadmeRequest,
@AuthenticationPrincipal String userGithubId) {
uploadReadmeService.uploadReadme(uploadReadmeRequest, userGithubId);
public ResponseEntity<SuccessResponse<?>> uploadReadme(@RequestBody UploadReadmeRequest uploadReadmeRequest, @AuthenticationPrincipal Long userId) {
if (userId == null) {
throw new EntityNotFoundException(ErrorCode.USER_ID_MISSING);
}
uploadReadmeService.uploadReadme(uploadReadmeRequest, userId);
return SuccessResponse.created(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.autorepo.server.domain.readme.dto.request.UploadReadmeRequest;
import org.autorepo.server.domain.user.entity.User;
import org.autorepo.server.domain.user.repository.UserRepository;
import org.autorepo.server.global.error.ErrorCode;
import org.autorepo.server.global.error.exception.EntityNotFoundException;
import org.autorepo.server.global.error.exception.InternalServerException;
import org.autorepo.server.global.utils.GitHubService;
Expand All @@ -30,11 +31,16 @@ public class UploadReadmeService {

private static final String GITHUB_README_API = "https://api.github.com/repos/%s/%s/contents/README.md";

public void uploadReadme(UploadReadmeRequest uploadReadmeRequest, String githubLogin) {
// GitHub 'login'으로 사용자 조회
User user = userRepository.findByGithubId(githubLogin)
public void uploadReadme(UploadReadmeRequest uploadReadmeRequest, Long userId) {
// User ID로 사용자 조회
User user = userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));

String githubToken = user.getGithubToken();
if (githubToken == null || githubToken.isEmpty()) {
throw new EntityNotFoundException(ErrorCode.GITHUB_TOKEN_NOT_FOUND);
}

String repoUrl = uploadReadmeRequest.repoUrl();
String[] repoInfo = repoUrl.replace("https://github.com/", "").split("/");
if (repoInfo.length < 2) {
Expand All @@ -44,10 +50,9 @@ public void uploadReadme(UploadReadmeRequest uploadReadmeRequest, String githubL
String repo = repoInfo[1];

String content = uploadReadmeRequest.content();
String path = "README.md";

HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(user.getGithubToken());
headers.setBearerAuth(githubToken); // GitHub Token 설정
headers.setContentType(MediaType.APPLICATION_JSON);

String url = String.format(GITHUB_README_API, owner, repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum ErrorCode {
REFRESH_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND, "리프레쉬 토큰을 찾을 수 없습니다."),
TEMPLATE_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 템플릿을 찾을 수 없습니다."),
README_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 README를 찾을 수 없습니다."),
GITHUB_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 Gihub Token을 찾을 수 없습니다."),

/**
* 405 Method Not Allowed
Expand Down

0 comments on commit 71f8534

Please sign in to comment.