From a26f2a6e2f145265149e0e31e04bfb7a1d00e4cc Mon Sep 17 00:00:00 2001 From: Sniij <120099321+Sniij@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:53:39 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20Refactor=20=EC=A7=84=ED=96=89?= =?UTF-8?q?=EC=A4=91=20-=20Comment=20DB=EC=97=90=20user=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=B6=94=EA=B0=80=ED=95=B4=EC=95=BC?= =?UTF-8?q?=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 125 +++++++++--------- server/build.gradle | 17 +-- .../SebMainProject009Application.java | 4 + .../auth/controller/JwtController.java | 5 +- .../handler/OAuth2UserSuccessHandler.java | 2 +- .../auth/handler/UserAccessDeniedHandler.java | 1 - .../auth/jwt/JwtTokenizer.java | 8 +- .../comment/controller/CommentController.java | 5 +- .../comment/dto/CommentPostDto.java | 4 +- .../comment/dto/CommentResponseDto.java | 2 +- .../comment/entity/Comment.java | 23 ++-- .../comment/mapper/CommentMapper.java | 2 - .../repository/CommentCustomRepository.java | 11 ++ .../comment/repository/CommentRepository.java | 5 +- .../repository/CommentRepositoryImpl.java | 35 +++++ .../comment/service/CommentService.java | 18 ++- .../commu/controller/CommuController.java | 14 +- .../commu/dto/CommuImageResponseDto.java | 2 +- .../commu/dto/CommuPatchDto.java | 4 +- .../commu/dto/CommuPostDto.java | 4 +- .../commu/dto/CommuResponseDto.java | 7 +- .../commu/dto/CommuResponsesDto.java | 6 +- .../sebmainproject009/commu/entity/Commu.java | 25 +--- .../commu/mapper/CommuMapper.java | 5 +- .../repository/CommuCustomRepository.java | 11 ++ .../commu/repository/CommuRepository.java | 8 +- .../commu/repository/CommuRepositoryImpl.java | 31 +++++ .../commu/service/CommuService.java | 25 ++-- .../handler/LambdaRequestStreamHandler.java | 40 ++++++ .../s3/client/CustomS3Client.java | 1 - .../s3/controller/S3Controller.java | 2 +- .../search/controller/SearchController.java | 5 +- .../search/dto/ItemResponse.java | 3 - .../survey/Dto/SurveyDto.java | 2 +- .../survey/controller/SurveyController.java | 5 +- .../user/controller/UserController.java | 13 +- .../user/dto/UserPatchDto.java | 2 +- .../user/dto/UserResponseDto.java | 2 +- .../user/entity/SessionUser.java | 2 +- .../sebmainproject009/user/entity/User.java | 32 ++--- .../user/repository/UserCustomRepository.java | 11 ++ .../user/repository/UserRepository.java | 5 +- .../user/repository/UserRepositoryImpl.java | 34 +++++ .../user/service/UserService.java | 14 +- 44 files changed, 360 insertions(+), 222 deletions(-) create mode 100644 server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentCustomRepository.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepositoryImpl.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuCustomRepository.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepositoryImpl.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/lambda/handler/LambdaRequestStreamHandler.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/user/repository/UserCustomRepository.java create mode 100644 server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepositoryImpl.java diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 18f70c09..8fd3cc1c 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,62 +1,63 @@ -name: Java CI with Gradle - -on: - push: - branches: [ "123" ] - -permissions: - contents: read - -env: - AWS_REGION: ap-northeast-2 - S3_BUCKET_NAME: dowajoyak.store - CODE_DEPLOY_APPLICATION_NAME: dowajoyak-CodeDeploy - CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: dowajoyak-CodeDeploy-group - -jobs: - build: - runs-on: ubuntu-latest - env: - working-directory: ./server - - steps: - - uses: actions/checkout@v3 - - name: Set up JDK 11 - uses: actions/setup-java@v3 - with: - java-version: '11' - distribution: 'temurin' - - - name: Add permission - run: chmod +x gradlew - working-directory: ${{ env.working-directory }} - - - name: Build with Gradle - run: ./gradlew clean build - working-directory: ${{ env.working-directory }} - - - # build한 후 프로젝트를 압축합니다. - - name: Make zip file - run: zip -r ./seb-main-project-009.zip . - shell: bash - - # Access Key와 Secret Access Key를 통해 권한을 확인합니다. - # 아래 코드에 Access Key와 Secret Key를 직접 작성하지 않습니다. - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} # 등록한 Github Secret이 자동으로 불려옵니다. - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # 등록한 Github Secret이 자동으로 불려옵니다. - aws-region: ap-northeast-2 - - # 압축한 프로젝트를 S3로 전송합니다. - - name: Upload to AWS S3 - run: aws s3 cp --region ap-northeast-2 ./seb-main-project-009.zip s3://$S3_BUCKET_NAME/seb-main-project-009.zip - # CodeDeploy에게 배포 명령을 내립니다. - - name: Code Deploy - run: > - aws deploy create-deployment --application-name $CODE_DEPLOY_APPLICATION_NAME - --deployment-config-name CodeDeployDefault.AllAtOnce - --deployment-group-name $CODE_DEPLOY_DEPLOYMENT_GROUP_NAME - --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=seb-main-project-009.zip +# +#name: Java CI with Gradle +# +#on: +# push: +# branches: [ "123" ] +# +#permissions: +# contents: read +# +#env: +# AWS_REGION: ap-northeast-2 +# S3_BUCKET_NAME: dowajoyak.store +# CODE_DEPLOY_APPLICATION_NAME: dowajoyak-CodeDeploy +# CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: dowajoyak-CodeDeploy-group +# +#jobs: +# build: +# runs-on: ubuntu-latest +# env: +# working-directory: ./server +# +# steps: +# - uses: actions/checkout@v3 +# - name: Set up JDK 11 +# uses: actions/setup-java@v3 +# with: +# java-version: '11' +# distribution: 'temurin' +# +# - name: Add permission +# run: chmod +x gradlew +# working-directory: ${{ env.working-directory }} +# +# - name: Build with Gradle +# run: ./gradlew clean build +# working-directory: ${{ env.working-directory }} +# +# +# # build한 후 프로젝트를 압축합니다. +# - name: Make zip file +# run: zip -r ./seb-main-project-009.zip . +# shell: bash +# +# # Access Key와 Secret Access Key를 통해 권한을 확인합니다. +# # 아래 코드에 Access Key와 Secret Key를 직접 작성하지 않습니다. +# - name: Configure AWS credentials +# uses: aws-actions/configure-aws-credentials@v1 +# with: +# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} # 등록한 Github Secret이 자동으로 불려옵니다. +# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # 등록한 Github Secret이 자동으로 불려옵니다. +# aws-region: ap-northeast-2 +# +# # 압축한 프로젝트를 S3로 전송합니다. +# - name: Upload to AWS S3 +# run: aws s3 cp --region ap-northeast-2 ./seb-main-project-009.zip s3://$S3_BUCKET_NAME/seb-main-project-009.zip +# # CodeDeploy에게 배포 명령을 내립니다. +# - name: Code Deploy +# run: > +# aws deploy create-deployment --application-name $CODE_DEPLOY_APPLICATION_NAME +# --deployment-config-name CodeDeployDefault.AllAtOnce +# --deployment-group-name $CODE_DEPLOY_DEPLOYMENT_GROUP_NAME +# --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=seb-main-project-009.zip diff --git a/server/build.gradle b/server/build.gradle index 8ae137ad..ce7b6b36 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -6,7 +6,7 @@ plugins { group = 'com.codestates' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '11' +sourceCompatibility = '17' bootJar{ // jar file name 변경 @@ -35,29 +35,25 @@ dependencies { // AWS paramStore 사용 Spring boot 버전 호환 상세 : https://spring.io/projects/spring-cloud - implementation "org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config:2.2.6.RELEASE" - implementation "io.awspring.cloud:spring-cloud-starter-aws-parameter-store-config:2.3.0" + //implementation "org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config:2.2.6.RELEASE" + //implementation "io.awspring.cloud:spring-cloud-starter-aws-parameter-store-config:2.3.0" implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' - runtimeOnly 'mysql:mysql-connector-java:8.0.28' + implementation 'org.mapstruct:mapstruct:1.5.2.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final' //security - implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' - implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'com.google.code.gson:gson' - implementation 'org.springframework.boot:spring-boot-starter-mail' // JWT 기능을 위한 jjwt 라이브러리 implementation 'io.jsonwebtoken:jjwt-api:0.11.5' @@ -71,6 +67,11 @@ dependencies { implementation 'org.json:json:20230227' + + implementation 'org.mongodb:mongodb-driver-core' + implementation 'org.springframework.boot:spring-boot-starter-data-mongodb' + + implementation 'com.amazonaws.serverless:aws-serverless-java-container-springboot2:1.9.4' } diff --git a/server/src/main/java/com/codestates/sebmainproject009/SebMainProject009Application.java b/server/src/main/java/com/codestates/sebmainproject009/SebMainProject009Application.java index c871363f..d22d178c 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/SebMainProject009Application.java +++ b/server/src/main/java/com/codestates/sebmainproject009/SebMainProject009Application.java @@ -2,12 +2,16 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @SpringBootApplication +@EnableMongoRepositories public class SebMainProject009Application { public static void main(String[] args) { + + SpringApplication.run(SebMainProject009Application.class, args); } diff --git a/server/src/main/java/com/codestates/sebmainproject009/auth/controller/JwtController.java b/server/src/main/java/com/codestates/sebmainproject009/auth/controller/JwtController.java index 90eee4d7..3233092a 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/auth/controller/JwtController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/auth/controller/JwtController.java @@ -8,7 +8,10 @@ import io.jsonwebtoken.Jws; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.HashMap; diff --git a/server/src/main/java/com/codestates/sebmainproject009/auth/handler/OAuth2UserSuccessHandler.java b/server/src/main/java/com/codestates/sebmainproject009/auth/handler/OAuth2UserSuccessHandler.java index 17ddaa50..b7ed7c36 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/auth/handler/OAuth2UserSuccessHandler.java +++ b/server/src/main/java/com/codestates/sebmainproject009/auth/handler/OAuth2UserSuccessHandler.java @@ -2,8 +2,8 @@ import com.codestates.sebmainproject009.auth.jwt.JwtTokenizer; import com.codestates.sebmainproject009.auth.utils.CustomAuthorityUtils; -import com.codestates.sebmainproject009.user.service.UserService; import com.codestates.sebmainproject009.user.entity.User; +import com.codestates.sebmainproject009.user.service.UserService; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; diff --git a/server/src/main/java/com/codestates/sebmainproject009/auth/handler/UserAccessDeniedHandler.java b/server/src/main/java/com/codestates/sebmainproject009/auth/handler/UserAccessDeniedHandler.java index 2ac657f3..d8646c28 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/auth/handler/UserAccessDeniedHandler.java +++ b/server/src/main/java/com/codestates/sebmainproject009/auth/handler/UserAccessDeniedHandler.java @@ -5,7 +5,6 @@ import org.springframework.http.HttpStatus; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.web.access.AccessDeniedHandler; -import org.springframework.stereotype.Component; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; diff --git a/server/src/main/java/com/codestates/sebmainproject009/auth/jwt/JwtTokenizer.java b/server/src/main/java/com/codestates/sebmainproject009/auth/jwt/JwtTokenizer.java index 332c397b..553f5e0d 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/auth/jwt/JwtTokenizer.java +++ b/server/src/main/java/com/codestates/sebmainproject009/auth/jwt/JwtTokenizer.java @@ -130,7 +130,7 @@ public String extractTokenFromHeader(String authorizationHeader){ return null; } - public Long extractUserIdFromToken(String requestToken){ + public String extractUserIdFromToken(String requestToken){ if(requestToken!=null) { Jws claims = getClaims(requestToken, encodeBase64SecretKey(secretKey)); @@ -138,7 +138,7 @@ public Long extractUserIdFromToken(String requestToken){ if(userIdString!=null){ try{ - return Long.parseLong(userIdString); + return userIdString; } catch (NumberFormatException e){ throw new IllegalArgumentException(e.getMessage()); } @@ -177,9 +177,9 @@ public String getToken(String authorizationHeader) { return token; } - public Long getUserId(String token) { + public String getUserId(String token) { - Long userId; + String userId; if (token != null) { userId = extractUserIdFromToken(token); diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/controller/CommentController.java b/server/src/main/java/com/codestates/sebmainproject009/comment/controller/CommentController.java index 5a34ec84..97d22f8a 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/controller/CommentController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/controller/CommentController.java @@ -8,7 +8,10 @@ import com.codestates.sebmainproject009.user.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentPostDto.java b/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentPostDto.java index 8e9575b9..a8e423af 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentPostDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentPostDto.java @@ -8,8 +8,8 @@ @Getter @Setter public class CommentPostDto { - private Long userId; - private Long commuId; + private String userId; + private String commuId; @NotBlank private String comment; } diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentResponseDto.java b/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentResponseDto.java index a53c1797..aa1170ca 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentResponseDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/dto/CommentResponseDto.java @@ -8,7 +8,7 @@ @Getter @Setter public class CommentResponseDto { - private Long commentId; + private String commentId; private String comment; private String displayName; private LocalDateTime createAt = LocalDateTime.now(); diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/entity/Comment.java b/server/src/main/java/com/codestates/sebmainproject009/comment/entity/Comment.java index 8ef4fb1a..9425172d 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/entity/Comment.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/entity/Comment.java @@ -2,41 +2,34 @@ import com.codestates.sebmainproject009.commu.entity.Commu; import com.codestates.sebmainproject009.user.entity.User; -import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Getter; import lombok.Setter; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; -import javax.persistence.*; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; -@Entity +@Document("comment") @Getter @Setter public class Comment { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long commentId; + private String objectId; + + private String commentId; - @Column private String comment; - @Column private String displayName; - @Column private LocalDateTime createAt = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDateTime(); - @ManyToOne - @JoinColumn(name = "commuId") - @JsonIgnore - private Commu commu; - @ManyToOne - @JsonIgnore - @JoinColumn(name = "userId") + private String commuId; + private User user; diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/mapper/CommentMapper.java b/server/src/main/java/com/codestates/sebmainproject009/comment/mapper/CommentMapper.java index 87ffa1f6..f00e598c 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/mapper/CommentMapper.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/mapper/CommentMapper.java @@ -3,8 +3,6 @@ import com.codestates.sebmainproject009.comment.dto.CommentPostDto; import com.codestates.sebmainproject009.comment.dto.CommentResponseDto; import com.codestates.sebmainproject009.comment.entity.Comment; -import com.codestates.sebmainproject009.commu.dto.CommuResponsesDto; -import com.codestates.sebmainproject009.commu.entity.Commu; import org.mapstruct.Mapper; import java.util.List; diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentCustomRepository.java b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentCustomRepository.java new file mode 100644 index 00000000..4a779003 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentCustomRepository.java @@ -0,0 +1,11 @@ +package com.codestates.sebmainproject009.comment.repository; + +import com.codestates.sebmainproject009.comment.entity.Comment; + + +public interface CommentCustomRepository { + Comment findByCommentId(String id); + + void deleteByCommentId(String id); + +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepository.java b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepository.java index 52a3e453..a8c837bd 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepository.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepository.java @@ -1,9 +1,8 @@ package com.codestates.sebmainproject009.comment.repository; import com.codestates.sebmainproject009.comment.entity.Comment; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.mongodb.repository.MongoRepository; -import java.util.List; -public interface CommentRepository extends JpaRepository { +public interface CommentRepository extends MongoRepository, CommentCustomRepository{ } diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepositoryImpl.java b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepositoryImpl.java new file mode 100644 index 00000000..f7596eb0 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/repository/CommentRepositoryImpl.java @@ -0,0 +1,35 @@ +package com.codestates.sebmainproject009.comment.repository; + +import com.codestates.sebmainproject009.comment.entity.Comment; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; + +import java.util.List; +import java.util.Optional; + +public class CommentRepositoryImpl implements CommentCustomRepository{ + + private final MongoTemplate mongoTemplate; + + public CommentRepositoryImpl(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + @Override + public Comment findByCommentId(String id) { + + Query query = new Query(Criteria.where("commentId").is(id)); + + return mongoTemplate.findOne(query, Comment.class); + + } + + @Override + public void deleteByCommentId(String id) { + Query query = new Query(Criteria.where("commentId").is(id)); + mongoTemplate.remove(query, Comment.class); + } + + +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/comment/service/CommentService.java b/server/src/main/java/com/codestates/sebmainproject009/comment/service/CommentService.java index 05f2348f..43b3ecee 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/comment/service/CommentService.java +++ b/server/src/main/java/com/codestates/sebmainproject009/comment/service/CommentService.java @@ -43,14 +43,14 @@ public Comment createComment(CommentPostDto commentPostDto){ Comment comment = mapper.commentPostDtoToComment(commentPostDto); comment.setUser(findUser); comment.setDisplayName(findUser.getDisplayName()); - comment.setCommu(findCommu); + comment.setCommuId(findCommu.getCommuId()); findCommu.addComment(comment); return commentRepository.save(comment); } - public void deleteCommentsByCommuId(long commuId){ + public void deleteCommentsByCommuId(String commuId){ Commu foundCommu = commuService.findCommu(commuId); @@ -62,13 +62,17 @@ public void deleteCommentsByCommuId(long commuId){ } - public void deleteComment(long commentId){ - commentRepository.deleteById(commentId); + public void deleteComment(String commentId){ + commentRepository.deleteByCommentId(commentId); } - public Comment findVerifiedComment(long commentId){ - Optional optionalComment = commentRepository.findById(commentId); + public Comment findVerifiedComment(String commentId){ + Comment comment = commentRepository.findByCommentId(commentId); - return optionalComment.orElseThrow(()-> new NoSuchMessageException("댓글이 없습니다.")); + return comment; } + + + + } diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/controller/CommuController.java b/server/src/main/java/com/codestates/sebmainproject009/commu/controller/CommuController.java index 472e6eaf..a4725e1e 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/controller/CommuController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/controller/CommuController.java @@ -56,7 +56,7 @@ public ResponseEntity postCommu(@RequestBody CommuPostDto commuPostDto) { public ResponseEntity postCommu(@RequestParam("image") @Nullable MultipartFile image, @RequestParam("title") String title, @RequestParam("content") String content, - @RequestParam("userId") Long userId) { + @RequestParam("userId") String userId) { // 이미지 업로드 및 S3 URL 가져오는 로직 String imageUrl = null; @@ -92,7 +92,7 @@ public ResponseEntity getCommus(@RequestParam(required = false, defaultValue = " } @GetMapping("/{commuId}") - public ResponseEntity getCommu(@PathVariable long commuId){ + public ResponseEntity getCommu(@PathVariable String commuId){ Commu commu = commuService.findCommu(commuId); User user = commu.getUser(); @@ -104,13 +104,13 @@ public ResponseEntity getCommu(@PathVariable long commuId){ } @PatchMapping("/{commuId}") - public ResponseEntity patchCommu(@PathVariable long commuId, + public ResponseEntity patchCommu(@PathVariable String commuId, @RequestBody CommuPatchDto commuPatchDto, @RequestHeader("Authorization") String authorizationHeader){ String token = jwtTokenizer.extractTokenFromHeader(authorizationHeader); - Long userId; + String userId; if(token != null){ userId = jwtTokenizer.extractUserIdFromToken(token); }else { @@ -129,14 +129,14 @@ public ResponseEntity patchCommu(@PathVariable long commuId, } @DeleteMapping("/{commuId}") - public ResponseEntity deleteCommu(@PathVariable long commuId, + public ResponseEntity deleteCommu(@PathVariable String commuId, @RequestHeader("Authorization") String authorizationHeader){ String token = jwtTokenizer.extractTokenFromHeader(authorizationHeader); - Long userId; + String userId; if(token != null){ - userId = jwtTokenizer.extractUserIdFromToken(token); + userId = String.valueOf(jwtTokenizer.extractUserIdFromToken(token)); }else { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuImageResponseDto.java b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuImageResponseDto.java index cf6b91d4..e78b46c8 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuImageResponseDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuImageResponseDto.java @@ -13,7 +13,7 @@ @AllArgsConstructor public class CommuImageResponseDto { - private Long commuId; + private String commuId; private String title; private String content; private LocalDateTime createAt = LocalDateTime.now(); diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPatchDto.java b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPatchDto.java index dc24c124..06282a1f 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPatchDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPatchDto.java @@ -6,8 +6,8 @@ @Getter @Setter public class CommuPatchDto { - private Long commuId; + private String commuId; private String title; private String content; - private Long userId; + private String userId; } diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPostDto.java b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPostDto.java index 06f784a9..2ac61589 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPostDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuPostDto.java @@ -3,12 +3,10 @@ import lombok.Getter; import lombok.Setter; -import java.time.LocalDateTime; - @Getter @Setter public class CommuPostDto { private String title; private String content; - private Long userId; + private String userId; } diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponseDto.java b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponseDto.java index 72bef016..8f28d4d2 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponseDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponseDto.java @@ -1,13 +1,10 @@ package com.codestates.sebmainproject009.commu.dto; -import com.codestates.sebmainproject009.comment.dto.CommentResponseDto; import com.codestates.sebmainproject009.comment.entity.Comment; -import com.codestates.sebmainproject009.user.entity.User; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; - import java.time.LocalDateTime; import java.util.List; @@ -15,7 +12,7 @@ @Setter @AllArgsConstructor public class CommuResponseDto { - private Long commuId; + private String commuId; private String title; private String content; private LocalDateTime createAt = LocalDateTime.now(); @@ -23,7 +20,7 @@ public class CommuResponseDto { private String displayName; private List comments; - private Long userId; + private String userId; private String imageUrl; diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponsesDto.java b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponsesDto.java index 0e85acdc..b9059600 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponsesDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/dto/CommuResponsesDto.java @@ -1,18 +1,14 @@ package com.codestates.sebmainproject009.commu.dto; -import com.codestates.sebmainproject009.comment.entity.Comment; import lombok.Getter; -import lombok.RequiredArgsConstructor; import lombok.Setter; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; @Getter @Setter public class CommuResponsesDto { - private Long commuId; + private String commuId; private String title; private LocalDateTime createAt = LocalDateTime.now(); private int view; diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/entity/Commu.java b/server/src/main/java/com/codestates/sebmainproject009/commu/entity/Commu.java index 0109d062..2dc011f4 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/entity/Commu.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/entity/Commu.java @@ -2,50 +2,39 @@ import com.codestates.sebmainproject009.comment.entity.Comment; import com.codestates.sebmainproject009.user.entity.User; -import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Getter; import lombok.Setter; -import org.hibernate.annotations.ColumnDefault; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; -import javax.persistence.*; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; -@Entity +@Document("commu") @Getter @Setter -@Table(name = "commu") public class Commu { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long commuId; + private String objectId; + + private String commuId; - @Column(length = 30) private String title; - @Column(length = 3000) private String content; - @OneToMany(mappedBy = "commu", cascade = {CascadeType.ALL}) - @JsonIgnore private List comments = new ArrayList<>(); - @Column private LocalDateTime createAt = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDateTime(); - @Column - @ColumnDefault("0") + private int view; - @ManyToOne - @JoinColumn(name = "userId") private User user; - @Column private String imageUrl; public void setUser(User user) { diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/mapper/CommuMapper.java b/server/src/main/java/com/codestates/sebmainproject009/commu/mapper/CommuMapper.java index f60afd8f..eb5637ab 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/mapper/CommuMapper.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/mapper/CommuMapper.java @@ -1,6 +1,5 @@ package com.codestates.sebmainproject009.commu.mapper; -import com.codestates.sebmainproject009.comment.dto.CommentResponseDto; import com.codestates.sebmainproject009.comment.entity.Comment; import com.codestates.sebmainproject009.commu.dto.*; import com.codestates.sebmainproject009.commu.entity.Commu; @@ -22,13 +21,13 @@ default CommuResponseDto commuToCommuResponseDto(Commu commu){ return null; } - Long commuId = null; + String commuId = null; String title = null; String content = null; LocalDateTime createAt = null; int view = 0; List comments = null; - Long userId = null; + String userId = null; String imageUrl = null; String userProfileImageUrl = null; diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuCustomRepository.java b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuCustomRepository.java new file mode 100644 index 00000000..1f775550 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuCustomRepository.java @@ -0,0 +1,11 @@ +package com.codestates.sebmainproject009.commu.repository; + +import com.codestates.sebmainproject009.commu.entity.Commu; + + +public interface CommuCustomRepository { + + Commu findByCommuId(String id); + + void deleteByCommuId(String id); +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepository.java b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepository.java index 8db5308c..2ec5658b 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepository.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepository.java @@ -1,12 +1,8 @@ package com.codestates.sebmainproject009.commu.repository; -import com.codestates.sebmainproject009.comment.entity.Comment; import com.codestates.sebmainproject009.commu.entity.Commu; -import com.codestates.sebmainproject009.user.entity.User; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.mongodb.repository.MongoRepository; -import java.util.List; -import java.util.Optional; -public interface CommuRepository extends JpaRepository { +public interface CommuRepository extends MongoRepository, CommuCustomRepository { } diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepositoryImpl.java b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepositoryImpl.java new file mode 100644 index 00000000..a59f1b32 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/repository/CommuRepositoryImpl.java @@ -0,0 +1,31 @@ +package com.codestates.sebmainproject009.commu.repository; + +import com.codestates.sebmainproject009.commu.entity.Commu; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; + +import java.util.Optional; + +public class CommuRepositoryImpl implements CommuCustomRepository{ + + private final MongoTemplate mongoTemplate; + + public CommuRepositoryImpl(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + + @Override + public Commu findByCommuId(String id) { + Query query = new Query(Criteria.where("commuId").is(id)); + + return mongoTemplate.findOne(query, Commu.class); + } + @Override + public void deleteByCommuId(String id) { + Query query = new Query(Criteria.where("commuId").is(id)); + + mongoTemplate.remove(query, Commu.class); + } +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/commu/service/CommuService.java b/server/src/main/java/com/codestates/sebmainproject009/commu/service/CommuService.java index 8c4aaed8..2fdf3a0f 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/commu/service/CommuService.java +++ b/server/src/main/java/com/codestates/sebmainproject009/commu/service/CommuService.java @@ -40,11 +40,10 @@ public CommuService(CommuRepository commuRepository, CommuMapper mapper, UserSer public Commu createCommu(CommuPostDto commuPostDto){ User user = userService.findVerifiedUser(commuPostDto.getUserId()); Commu commu = mapper.commuPostDtoToCommu(commuPostDto); + commu.setView(0); + commu.setUser(user); - Commu cratedCommu = commu; - cratedCommu.setUser(user); - - return commuRepository.save(cratedCommu); + return commuRepository.save(commu); } public Commu updateCommu(Commu commu){ @@ -58,7 +57,7 @@ public Commu updateCommu(Commu commu){ return commuRepository.save(findCommu); } - public Commu findCommu(long commuId){ + public Commu findCommu(String commuId){ Commu foundCommu = findVerifiedCommu(commuId); foundCommu.setView(foundCommu.getView()+1); @@ -73,31 +72,31 @@ public List findCommuList(){ return commuRepository.findAll(); } - public void deleteCommu(long commuId){ + public void deleteCommu(String commuId){ Commu foundCommu = findVerifiedCommu(commuId); List commentList = foundCommu.getComments(); for(Comment comment : commentList){ commentRepository.delete(comment); } - commuRepository.deleteById(commuId); + commuRepository.deleteByCommuId(commuId); } - public Commu findVerifiedCommu(long commuId){ - OptionaloptionalCommu = commuRepository.findById(commuId); + public Commu findVerifiedCommu(String commuId){ + Commu commu = commuRepository.findByCommuId(commuId); - return optionalCommu.orElseThrow(()-> new NoSuchMessageException("게시글이 없습니다.")); + return commu; } - public boolean isSameWriter(Long userId, long commuId) { + public boolean isSameWriter(String userId, String commuId) { Commu foundCommu = findVerifiedCommu(commuId); - Long writerId = foundCommu.getUser().getUserId(); + String writerId = foundCommu.getUser().getUserId(); return writerId.equals(userId); } - public Commu createCommuCustom(String title, String content, String imageUrl,Long userId) { + public Commu createCommuCustom(String title, String content, String imageUrl,String userId) { User user = userService.findVerifiedUser(userId); Commu commu =new Commu(); diff --git a/server/src/main/java/com/codestates/sebmainproject009/lambda/handler/LambdaRequestStreamHandler.java b/server/src/main/java/com/codestates/sebmainproject009/lambda/handler/LambdaRequestStreamHandler.java new file mode 100644 index 00000000..77b4a7ae --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/lambda/handler/LambdaRequestStreamHandler.java @@ -0,0 +1,40 @@ +package com.codestates.sebmainproject009.lambda.handler; + +import com.amazonaws.serverless.exceptions.ContainerInitializationException; +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; +import com.amazonaws.serverless.proxy.model.AwsProxyResponse; +import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler; +import com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder; +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestStreamHandler; +import com.codestates.sebmainproject009.SebMainProject009Application; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class LambdaRequestStreamHandler implements RequestStreamHandler { + + private static final SpringBootLambdaContainerHandler handler; + + + static { + try { + handler = new SpringBootProxyHandlerBuilder() + .defaultProxy() + .asyncInit() + .springBootApplication(SebMainProject009Application.class) + .buildAndInitialize(); + } catch (ContainerInitializationException e) { + + e.printStackTrace(); + throw new RuntimeException("Could not initialize Spring Boot application", e); + } + } + + @Override + public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) + throws IOException { + handler.proxyStream(inputStream, outputStream, context); + } +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/s3/client/CustomS3Client.java b/server/src/main/java/com/codestates/sebmainproject009/s3/client/CustomS3Client.java index 6d9a94fa..e66b27af 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/s3/client/CustomS3Client.java +++ b/server/src/main/java/com/codestates/sebmainproject009/s3/client/CustomS3Client.java @@ -10,7 +10,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; - import java.nio.file.Files; import java.nio.file.Path; diff --git a/server/src/main/java/com/codestates/sebmainproject009/s3/controller/S3Controller.java b/server/src/main/java/com/codestates/sebmainproject009/s3/controller/S3Controller.java index e1ef7edd..226b6076 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/s3/controller/S3Controller.java +++ b/server/src/main/java/com/codestates/sebmainproject009/s3/controller/S3Controller.java @@ -26,7 +26,7 @@ public S3Controller(CustomS3Client customS3Client, UserService userService) { @PostMapping(value = "/postsToS3", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity postToS3(@RequestParam("image") MultipartFile image, - @RequestParam("userId") Long userId){ + @RequestParam("userId") String userId){ try { String imageUrl = null; if(image!=null){ diff --git a/server/src/main/java/com/codestates/sebmainproject009/search/controller/SearchController.java b/server/src/main/java/com/codestates/sebmainproject009/search/controller/SearchController.java index d14b074e..8b0e9cad 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/search/controller/SearchController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/search/controller/SearchController.java @@ -1,9 +1,9 @@ package com.codestates.sebmainproject009.search.controller; -import com.codestates.sebmainproject009.json.service.JSONService; import com.codestates.sebmainproject009.api.service.APIServiceImpl; import com.codestates.sebmainproject009.auth.jwt.JwtTokenizer; +import com.codestates.sebmainproject009.json.service.JSONService; import com.codestates.sebmainproject009.search.entity.Item; import com.codestates.sebmainproject009.search.entity.ItemList; import com.codestates.sebmainproject009.search.service.SearchService; @@ -19,9 +19,8 @@ import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotNull; - import java.net.URL; -import java.util.*; +import java.util.List; @RestController diff --git a/server/src/main/java/com/codestates/sebmainproject009/search/dto/ItemResponse.java b/server/src/main/java/com/codestates/sebmainproject009/search/dto/ItemResponse.java index e05c14d3..02dcda07 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/search/dto/ItemResponse.java +++ b/server/src/main/java/com/codestates/sebmainproject009/search/dto/ItemResponse.java @@ -1,10 +1,7 @@ package com.codestates.sebmainproject009.search.dto; import com.codestates.sebmainproject009.search.entity.Item; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.http.StreamingHttpOutputMessage; -import java.util.Collection; import java.util.List; public class ItemResponse { diff --git a/server/src/main/java/com/codestates/sebmainproject009/survey/Dto/SurveyDto.java b/server/src/main/java/com/codestates/sebmainproject009/survey/Dto/SurveyDto.java index 0318455f..1c5079e1 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/survey/Dto/SurveyDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/survey/Dto/SurveyDto.java @@ -6,7 +6,7 @@ @Getter @AllArgsConstructor public class SurveyDto { - private long userId; + private String userId; private String disease; private String allergy; } diff --git a/server/src/main/java/com/codestates/sebmainproject009/survey/controller/SurveyController.java b/server/src/main/java/com/codestates/sebmainproject009/survey/controller/SurveyController.java index d4352f40..ef22aae2 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/survey/controller/SurveyController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/survey/controller/SurveyController.java @@ -4,7 +4,10 @@ import com.codestates.sebmainproject009.user.entity.User; import com.codestates.sebmainproject009.user.repository.UserRepository; import com.codestates.sebmainproject009.user.service.UserService; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/controller/UserController.java b/server/src/main/java/com/codestates/sebmainproject009/user/controller/UserController.java index 9b30772c..9c2dc658 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/controller/UserController.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/controller/UserController.java @@ -17,6 +17,7 @@ import javax.validation.Valid; import javax.validation.constraints.Positive; import java.util.List; +import java.util.Objects; @RestController @@ -48,7 +49,7 @@ public ResponseEntity postUser(@Valid @RequestBody UserPostDto userPostDto){ } @PatchMapping("/{userId}") - public ResponseEntity patchUser(@PathVariable @Positive long userId, + public ResponseEntity patchUser(@PathVariable @Positive String userId, @RequestBody UserPatchDto userPatchDto){ userPatchDto.setUserId(userId); @@ -58,8 +59,8 @@ public ResponseEntity patchUser(@PathVariable @Positive long userId, } @GetMapping("/{userId}") - public ResponseEntity getUser(@PathVariable @Positive long userId){ - User user = userService.findUser(userId); + public ResponseEntity getUser(@PathVariable @Positive String userId){ + User user = userService.findVerifiedUser(userId); UserResponseDto userResponseDto = mapper.userToUserResponseDto(user); userResponseDto.setProfileImgUrl(user.getProfileImgUrl()); @@ -72,14 +73,14 @@ public ResponseEntity getUser(@PathVariable @Positive long userId){ } @DeleteMapping("/{userId}") - public ResponseEntity deleteUser(@PathVariable @Positive long userId){ + public ResponseEntity deleteUser(@PathVariable @Positive String userId){ List foundCommu = commuService.findCommuList(); for(Commu data: foundCommu){ - long found = data.getUser().getUserId(); + String found = data.getUser().getUserId(); - if(found==userId){ + if(Objects.equals(found, userId)){ commuService.deleteCommu(data.getCommuId()); // comment 는 글 삭제 메서드에 포함되어 있음 diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserPatchDto.java b/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserPatchDto.java index 800a04d8..979fd446 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserPatchDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserPatchDto.java @@ -10,7 +10,7 @@ @Setter public class UserPatchDto { - private long userId; + private String userId; @NotBlank(message = "이름을 적어주세요.") private String displayName; diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserResponseDto.java b/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserResponseDto.java index 2c5c0e23..7bb74cc7 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserResponseDto.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/dto/UserResponseDto.java @@ -7,7 +7,7 @@ @Getter @Setter public class UserResponseDto { - private long userId; + private String userId; private String email; private String displayName; private String allergy; diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/entity/SessionUser.java b/server/src/main/java/com/codestates/sebmainproject009/user/entity/SessionUser.java index cc92635f..4d5a4e6c 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/entity/SessionUser.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/entity/SessionUser.java @@ -10,7 +10,7 @@ public class SessionUser implements Serializable { SessionUser(){} public SessionUser(User user){ - this.displayName = user.displayName; + this.displayName = user.getDisplayName(); this.email = user.getEmail(); } diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/entity/User.java b/server/src/main/java/com/codestates/sebmainproject009/user/entity/User.java index f17a899a..824e29a3 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/entity/User.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/entity/User.java @@ -1,35 +1,31 @@ package com.codestates.sebmainproject009.user.entity; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Getter; import lombok.Setter; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; -import javax.persistence.*; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; - -@Entity @Getter @Setter -@Table(name = "user") +@Document("user") public class User { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - Long userId; - @Column(updatable = false, nullable = false, unique = true) - String email; - @Column(nullable = false) - String displayName; - @Column(nullable = false) - String password; - @Column + private String objectId; + private String userId; + private String email; + private String displayName; + private String password; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "Asia/Seoul") private LocalDateTime createAt = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDateTime(); - @ElementCollection(fetch = FetchType.EAGER) private List roles = new ArrayList<>(); public User(String email, String displayName) { @@ -37,12 +33,10 @@ public User(String email, String displayName) { this.displayName = displayName; } - @Enumerated(value = EnumType.STRING) - @Column(length = 20, nullable = true) + private WorriedOrgan worriedOrgan = WorriedOrgan.NONE; - @Enumerated(value = EnumType.STRING) - @Column(length = 20, nullable = true) + private Allergy allergy = Allergy.NONE; private String otherAllergy; diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserCustomRepository.java b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserCustomRepository.java new file mode 100644 index 00000000..03bdc4d9 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserCustomRepository.java @@ -0,0 +1,11 @@ +package com.codestates.sebmainproject009.user.repository; + +import com.codestates.sebmainproject009.user.entity.User; + + +public interface UserCustomRepository { + + User findByUserId(String id); + + void deleteByUserId(Long id); +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepository.java b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepository.java index c1e0eb11..fa602d82 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepository.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepository.java @@ -1,12 +1,11 @@ package com.codestates.sebmainproject009.user.repository; import com.codestates.sebmainproject009.user.entity.User; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.mongodb.repository.MongoRepository; import java.util.Optional; - -public interface UserRepository extends JpaRepository { +public interface UserRepository extends MongoRepository, UserCustomRepository { Optional findByEmail(String email); diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepositoryImpl.java b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepositoryImpl.java new file mode 100644 index 00000000..24a0e9d8 --- /dev/null +++ b/server/src/main/java/com/codestates/sebmainproject009/user/repository/UserRepositoryImpl.java @@ -0,0 +1,34 @@ +package com.codestates.sebmainproject009.user.repository; + +import com.codestates.sebmainproject009.user.entity.User; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; + +import java.util.Optional; + + +public class UserRepositoryImpl implements UserCustomRepository{ + + + private final MongoTemplate mongoTemplate; + + public UserRepositoryImpl(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + + @Override + public User findByUserId(String id) { + Query query = new Query(Criteria.where("userId").is(id)); + + return mongoTemplate.findOne(query, User.class); + } + + @Override + public void deleteByUserId(Long id) { + Query query = new Query(Criteria.where("userId").is(id)); + mongoTemplate.remove(query, User.class); + } + +} diff --git a/server/src/main/java/com/codestates/sebmainproject009/user/service/UserService.java b/server/src/main/java/com/codestates/sebmainproject009/user/service/UserService.java index 42e31817..b31928fb 100644 --- a/server/src/main/java/com/codestates/sebmainproject009/user/service/UserService.java +++ b/server/src/main/java/com/codestates/sebmainproject009/user/service/UserService.java @@ -58,28 +58,22 @@ public User updateUser(User user){ return userRepository.save(findUser); } - @Transactional(readOnly = true) - public User findUser(long userId){ - return findVerifiedUser(userId); - } public User findUser(String email){ Optional optionalUser = userRepository.findByEmail(email); return optionalUser.orElseThrow(); } - public void deleteUser(long userId){ + public void deleteUser(String userId){ User findUser = findVerifiedUser(userId); userRepository.delete(findUser); } @Transactional(readOnly = true) - public User findVerifiedUser(long userId){ - OptionaloptionalUser = userRepository.findById(userId); - - return optionalUser.orElseThrow(()-> - new BusinessLogicException(ExceptionCode.MEMBER_NOT_FOUND)); + public User findVerifiedUser(String userId){ + User user = userRepository.findByUserId(userId); + return user; } protected void verifyExistsEmail(String email) {