Skip to content

Commit

Permalink
Merge pull request #46 from TRIP-Side-Project/dev
Browse files Browse the repository at this point in the history
spring applicaiton 포트 변경
  • Loading branch information
gkfktkrh153 authored Dec 14, 2023
2 parents 62bdfe7 + d6e0ed7 commit 1251d03
Show file tree
Hide file tree
Showing 69 changed files with 2,200 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/work.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ jobs:
docker rm trip_app || true
docker pull ${{ secrets.DOCKER_REPO }}/trip
docker run --name=trip_app --restart unless-stopped \
-p 80:8080 -e TZ=Asia/Seoul -d ${{ secrets.DOCKER_REPO }}/trip
-p 8080:8080 -e TZ=Asia/Seoul -d ${{ secrets.DOCKER_REPO }}/trip
33 changes: 18 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,37 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.boot:spring-boot-starter-mail'

// aws
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.429'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'

// tika
implementation 'org.apache.tika:tika-core:2.9.1'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

//Querydsl 추가
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

// Jwt
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

// mysql
runtimeOnly 'com.mysql:mysql-connector-j'

// h2
runtimeOnly 'com.h2database:h2'

// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.projectlombok:lombok'

// Jwt
implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.api.trip.common.auditing.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class AuditingConfig {

// TODO: 회원가입 후 유저 정보를 가져와서 넣어주는 방법을 모르겠습니다...
/**
@Bean
public AuditorAware<String> auditorAware() {
return () -> Optional.of("user1");
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.api.trip.common.auditing.entity;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseTimeEntity {

@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt;

@LastModifiedDate
@Column(nullable = false)
private LocalDateTime modifiedAt;

private LocalDateTime deletedAt;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.api.trip.common.security;
package com.api.trip.common.security.config;

import com.api.trip.common.security.jwt.JwtTokenFilter;
import com.api.trip.common.security.jwt.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.trip.common.security;
package com.api.trip.common.security.jwt;


import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.api.trip.common.security;
package com.api.trip.common.security.jwt;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;
Expand Down Expand Up @@ -42,7 +41,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
String[] excludePath = {"/api/members/join", "/api/members/login"};
String[] excludePath = {
"/api/members/join", "/api/members/login",
"/api/members/send-email", "/api/members/auth-email"
};

String path = request.getRequestURI();
return Arrays.stream(excludePath).anyMatch(path::startsWith);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.api.trip.common.security;
package com.api.trip.common.security.jwt;


import com.api.trip.common.security.dto.AuthenticationMember;
import com.api.trip.domain.member.model.Member;
import com.api.trip.domain.member.repository.MemberRepository;
import io.jsonwebtoken.*;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.trip.common.security;
package com.api.trip.common.security.service;

import com.api.trip.common.security.dto.AuthenticationMember;
import com.api.trip.domain.member.model.Member;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/api/trip/common/security/util/SecurityUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.api.trip.common.security.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
public static String getCurrentUsername() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getName() == null) {
throw new RuntimeException("인증 정보가 없습니다!");
}
return authentication.getName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.api.trip.domain.article.controller;

import com.api.trip.domain.article.controller.dto.*;
import com.api.trip.domain.article.service.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
public class ArticleController {

private final ArticleService articleService;

@PostMapping
public ResponseEntity<Long> createArticle(@RequestBody CreateArticleRequest request) {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok(articleService.createArticle(request, email));
}

@PatchMapping("/{articleId}")
public ResponseEntity<Void> updateArticle(@PathVariable Long articleId, @RequestBody UpdateArticleRequest request) {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
articleService.updateArticle(articleId, request, email);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{articleId}")
public ResponseEntity<Void> deleteArticle(@PathVariable Long articleId) {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
articleService.deleteArticle(articleId, email);
return ResponseEntity.ok().build();
}

@GetMapping("/{articleId}")
public ResponseEntity<ReadArticleResponse> readArticle(@PathVariable Long articleId) {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok(articleService.readArticle(articleId, email));
}

@GetMapping
public ResponseEntity<GetArticlesResponse> getArticles(
@PageableDefault(size = 8) Pageable pageable,
@RequestParam(value = "filter", required = false) String filter
) {
return ResponseEntity.ok(articleService.getArticles(pageable, filter));
}

@GetMapping("/me")
public ResponseEntity<GetMyArticlesResponse> getMyArticles() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok(articleService.getMyArticles(email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.api.trip.domain.article.controller.dto;

import com.api.trip.domain.article.model.Article;
import com.api.trip.domain.member.model.Member;
import lombok.Getter;

@Getter
public class CreateArticleRequest {

private String title;
private String content;

public Article toEntity(Member writer) {
return Article.builder()
.writer(writer)
.title(title)
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.api.trip.domain.article.controller.dto;

import com.api.trip.domain.article.model.Article;
import com.api.trip.domain.member.model.Member;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.domain.Page;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@Builder
public class GetArticlesResponse {

private int totalPages;
private long totalElements;
private int page;
private boolean hasNext;
private boolean hasPrevious;
private int requestSize;
private int resultSize;
private List<ArticleDto> result;

public static GetArticlesResponse of(Page<Article> articlePage) {
Page<ArticleDto> articleDtoPage = articlePage.map(ArticleDto::of);
return builder()
.totalPages(articleDtoPage.getTotalPages())
.totalElements(articleDtoPage.getTotalElements())
.page(articleDtoPage.getNumber() + 1)
.hasNext(articleDtoPage.hasNext())
.hasPrevious(articleDtoPage.hasPrevious())
.requestSize(articleDtoPage.getSize())
.resultSize(articleDtoPage.getNumberOfElements())
.result(articleDtoPage.getContent())
.build();
}

@Getter
@Builder
private static class ArticleDto {

private Long articleId;
private String title;
private Long writerId;
private String writerNickname;
private String writerRole;
private long viewCount;
private long likeCount;
private LocalDateTime createdAt;

private static ArticleDto of(Article article) {
Member writer = article.getWriter();
return builder()
.articleId(article.getId())
.title(article.getTitle())
.writerId(writer.getId())
.writerNickname(writer.getNickname())
.writerRole(writer.getRole().name())
.viewCount(article.getViewCount())
.likeCount(article.getLikeCount())
.createdAt(article.getCreatedAt())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.api.trip.domain.article.controller.dto;

import com.api.trip.domain.article.model.Article;
import com.api.trip.domain.member.model.Member;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@AllArgsConstructor
public class GetMyArticlesResponse {

private List<ArticleDto> articles;

public static GetMyArticlesResponse of(List<Article> articles) {
List<ArticleDto> articleDtos = articles.stream()
.map(ArticleDto::of)
.toList();

return new GetMyArticlesResponse(articleDtos);
}

@Getter
@Builder
private static class ArticleDto {

private Long articleId;
private String title;
private Long writerId;
private String writerNickname;
private String writerRole;
private LocalDateTime createdAt;

private static ArticleDto of(Article article) {
Member writer = article.getWriter();
return builder()
.articleId(article.getId())
.title(article.getTitle())
.writerId(writer.getId())
.writerNickname(writer.getNickname())
.writerRole(writer.getRole().name())
.createdAt(article.getCreatedAt())
.build();
}
}
}
Loading

0 comments on commit 1251d03

Please sign in to comment.