Skip to content

Commit

Permalink
Merge branch 'dev/be' into test/767-category-controller-test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyum-q committed Oct 17, 2024
2 parents 829b024 + 7535f77 commit 116c087
Show file tree
Hide file tree
Showing 38 changed files with 106 additions and 85 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/backend_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ jobs:
mysql version: ${{ secrets.MYSQL_VERSION }}
mysql database: ${{ secrets.MYSQL_DATABASE }}
mysql root password: ${{ secrets.MYSQL_PASSWORD }}

- name: DB 설정 파일 가져오기
working-directory: ./backend/src/main/resources
run: echo "${{ secrets.APPLICATION_DB_YAML }}" > application-db.yml


- name: gradle 캐싱
uses: gradle/actions/setup-gradle@v4

Expand All @@ -36,6 +32,10 @@ jobs:
java-version: 17
distribution: temurin

- name: 환경변수 주입
run: ${{ secrets.APPLICATION_YML }}
working-directory: ./backend/src/test/resources

- name: 테스트 코드 실행
run: ./gradlew test
working-directory: ./backend
2 changes: 1 addition & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ out/
.DS_Store

### YAML ###
application-db.yml
src/test/resources/application.yml

### compose ###
docker/app/*.jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

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

import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.lang.Nullable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
Expand All @@ -16,6 +18,9 @@
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonMappingException.Reference;

import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -51,6 +56,23 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
.body(codeZapException.toProblemDetail());
}

@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
String exceptionMessage = "잘못된 JSON 형식입니다.";
if (ex.getCause() instanceof JsonMappingException jsonMappingException) {
exceptionMessage = jsonMappingException.getPath().stream()
.map(Reference::getFieldName)
.collect(Collectors.joining(" ")) + " 필드의 형식이 잘못되었습니다.";
}

CodeZapException codeZapException =
new CodeZapException(ErrorCode.INVALID_REQUEST, String.join("\n", exceptionMessage));

return ResponseEntity.status(codeZapException.getErrorCode().getHttpStatus())
.body(codeZapException.toProblemDetail());
}

@ExceptionHandler
public ResponseEntity<ProblemDetail> handleException(Exception exception) {
log.error("[Exception] 예상치 못한 오류 {} 가 발생했습니다.", exception.getClass().getName(), exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

@Configuration
@RequiredArgsConstructor
@Profile("prod")
@EnableJpaRepositories(basePackages = "codezap")
public class DataSourceConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface LikesJpaRepository extends LikesRepository, JpaRepository<Likes

@Modifying(clearAutomatically = true)
@Query("DELETE FROM Likes l WHERE l.template.id in :templateIds")
void deleteByTemplateIds(@Param(value = "templateIds") List<Long> templateIds);
void deleteAllByTemplateIds(@Param(value = "templateIds") List<Long> templateIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface LikesRepository {

void deleteByMemberAndTemplate(Member member, Template template);

void deleteByTemplateIds(List<Long> templateIds);
void deleteAllByTemplateIds(List<Long> templateIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public void cancelLike(Member member, long templateId) {

@Transactional
public void deleteAllByTemplateIds(List<Long> templateIds) {
likesRepository.deleteByTemplateIds(templateIds);
likesRepository.deleteAllByTemplateIds(templateIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ public interface TemplateTagJpaRepository extends TemplateTagRepository, JpaRepo
JOIN TemplateTag tt ON t.id = tt.id.tagId
WHERE tt.template = :template
""")
List<Tag> findAllTagsByTemplate(Template template);
List<Tag> findAllTagsByTemplate(@Param("template") Template template);

@Query("""
SELECT tt, t
FROM TemplateTag tt
JOIN FETCH tt.tag t
WHERE tt.id.templateId = :templateId
""")
List<TemplateTag> findAllByTemplateId(Long templateId);
List<TemplateTag> findAllByTemplateId(@Param("templateId") Long templateId);

@Query("""
SELECT tt, t
FROM TemplateTag tt
JOIN FETCH tt.tag t
WHERE tt.id.templateId in :templateIds
""")
List<TemplateTag> findAllByTemplateIdsIn(List<Long> templateIds);
List<TemplateTag> findAllByTemplateIdsIn(@Param("templateIds") List<Long> templateIds);

@Query("""
SELECT DISTINCT t
Expand All @@ -49,9 +49,9 @@ WHERE tt.id.templateId IN (
)
)
""")
List<Tag> findAllTagDistinctByMemberId(Long memberId);
List<Tag> findAllTagDistinctByMemberId(@Param("memberId") Long memberId);

@Modifying(clearAutomatically = true)
@Query("DELETE FROM TemplateTag t WHERE t.template.id in :templateIds")
void deleteByTemplateIds(@Param("templateIds") List<Long> templateIds);
void deleteAllByTemplateIds(@Param("templateIds") List<Long> templateIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface TemplateTagRepository {

void deleteAllByTemplateId(Long templateId);

void deleteByTemplateIds(List<Long> templateIds);
void deleteAllByTemplateIds(List<Long> templateIds);
}
2 changes: 1 addition & 1 deletion backend/src/main/java/codezap/tag/service/TagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public void updateTags(Template template, List<String> tags) {

@Transactional
public void deleteAllByTemplateIds(List<Long> templateIds) {
templateTagRepository.deleteByTemplateIds(templateIds);
templateTagRepository.deleteAllByTemplateIds(templateIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ResponseEntity<Void> deleteTemplates(
@AuthenticationPrinciple Member member,
@PathVariable List<Long> ids
) {
templateApplicationService.deleteByMemberAndIds(member, ids);
templateApplicationService.deleteAllByMemberAndTemplateIds(member, ids);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import codezap.global.exception.CodeZapException;
import codezap.global.exception.ErrorCode;
Expand Down Expand Up @@ -37,5 +38,5 @@ default SourceCode fetchByTemplateAndOrdinal(Template template, int ordinal) {

@Modifying(clearAutomatically = true)
@Query("DELETE FROM SourceCode s WHERE s.template.id in :templateIds")
void deleteByTemplateIds(List<Long> templateIds);
void deleteAllByTemplateIds(@Param("templateIds") List<Long> templateIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface SourceCodeRepository {

void deleteById(Long id);

void deleteByTemplateIds(List<Long> templateIds);
void deleteAllByTemplateIds(List<Long> templateIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import codezap.global.exception.CodeZapException;
import codezap.global.exception.ErrorCode;
Expand All @@ -28,20 +29,20 @@ default Thumbnail fetchByTemplate(Template template) {
join fetch t.sourceCode sc
WHERE t.template = :template
""")
Optional<Thumbnail> findByTemplate(Template template);
Optional<Thumbnail> findByTemplate(@Param("template") Template template);


@Modifying(clearAutomatically = true)
@Query("DELETE FROM Thumbnail t WHERE t.template.id in :templateIds")
void deleteByTemplateIds(List<Long> templateIds);
void deleteAllByTemplateIds(@Param("templateIds") List<Long> templateIds);

@Query("""
SELECT t, sc
FROM Thumbnail t
join fetch t.sourceCode sc
WHERE t.template.id IN :templateIds
""")
List<Thumbnail> findAllByTemplateIn(List<Long> templateIds);
List<Thumbnail> findAllByTemplateIn(@Param("templateIds") List<Long> templateIds);

void deleteByTemplateId(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public interface ThumbnailRepository {

Thumbnail save(Thumbnail thumbnail);

void deleteByTemplateIds(List<Long> ids);
void deleteAllByTemplateIds(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void validateSourceCodesCount(Template template, UpdateTemplateRequest u
}

@Transactional
public void deleteByTemplateIds(List<Long> templateIds) {
sourceCodeRepository.deleteByTemplateIds(templateIds);
public void deleteAllByTemplateIds(List<Long> templateIds) {
sourceCodeRepository.deleteAllByTemplateIds(templateIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ExploreTemplatesResponse findAll() {
}

@Transactional
public void deleteByTemplateIds(List<Long> templateIds) {
thumbnailRepository.deleteByTemplateIds(templateIds);
public void deleteAllByTemplateIds(List<Long> templateIds) {
thumbnailRepository.deleteAllByTemplateIds(templateIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ public void update(Member member, Long templateId, UpdateTemplateRequest updateT
}

@Transactional
public void deleteByMemberAndIds(Member member, List<Long> ids) {
thumbnailService.deleteByTemplateIds(ids);
sourceCodeService.deleteByTemplateIds(ids);
tagService.deleteAllByTemplateIds(ids);
likesService.deleteAllByTemplateIds(ids);
templateService.deleteByMemberAndIds(member, ids);
public void deleteAllByMemberAndTemplateIds(Member member, List<Long> templateIds) {
thumbnailService.deleteAllByTemplateIds(templateIds);
sourceCodeService.deleteAllByTemplateIds(templateIds);
tagService.deleteAllByTemplateIds(templateIds);
likesService.deleteAllByTemplateIds(templateIds);
templateService.deleteByMemberAndIds(member, templateIds);
}
}
23 changes: 23 additions & 0 deletions backend/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,28 @@ spring:
output:
ansi:
enabled: always
datasource:
write:
jdbc-url: ${MYSQL_WRITER_URL}
username: ${MYSQL_USER}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
read:
jdbc-url: ${MYSQL_READER_URL}
username: ${MYSQL_USER}
password: ${MYSQL_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
flyway:
enabled: false
locations: classpath:db/migration
jpa:
# open-in-view: false
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: codezap.template.repository.FullTextSearchMySQLDialect
data.web.pageable.one-indexed-parameters: true

cors:
allowed-origins: http://localhost:3000
1 change: 0 additions & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ spring:
profiles:
active:
- local
- db
4 changes: 2 additions & 2 deletions backend/src/main/resources/logger/file/error-appender.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<included>
<appender name="error-appender" class="ch.qos.logback.core.rolling.RollingFileAppender">

<file>${LOG_FILE_PATH}/${DATE_FORMAT}/error.log</file>
<file>${LOG_FILE_PATH}/error.log</file>

<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
Expand All @@ -16,7 +16,7 @@
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/error/error%i.log</fileNamePattern>
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/error.log</fileNamePattern>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>30</maxHistory>
</rollingPolicy>
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/resources/logger/file/info-appender.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<included>
<appender name="info-appender" class="ch.qos.logback.core.rolling.RollingFileAppender">

<file>${LOG_FILE_PATH}/${DATE_FORMAT}/info.log</file>
<file>${LOG_FILE_PATH}/info.log</file>

<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
Expand All @@ -16,7 +16,7 @@
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/info/info%i.log</fileNamePattern>
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/info.log</fileNamePattern>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>30</maxHistory>
</rollingPolicy>
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/resources/logger/file/warn-appender.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<included>
<appender name="warn-appender" class="ch.qos.logback.core.rolling.RollingFileAppender">

<file>${LOG_FILE_PATH}/${DATE_FORMAT}/warn.log</file>
<file>${LOG_FILE_PATH}/warn.log</file>

<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
Expand All @@ -16,7 +16,7 @@
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/warn/warn%i.log</fileNamePattern>
<fileNamePattern>${LOG_FILE_PATH}/%d{yyyy-MM-dd}/warn.log</fileNamePattern>
<totalSizeCap>5GB</totalSizeCap>
<maxHistory>30</maxHistory>
</rollingPolicy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

import codezap.global.DatabaseIsolation;
import codezap.global.auditing.JpaAuditingConfiguration;
import codezap.global.rds.DataSourceConfig;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@DataJpaTest
@DatabaseIsolation
@Import(JpaAuditingConfiguration.class)
@Import({JpaAuditingConfiguration.class, DataSourceConfig.class})
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public @interface JpaRepositoryTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public void deleteByMemberAndTemplate(Member member, Template template) {
}

@Override
public void deleteByTemplateIds(List<Long> templateIds) {
public void deleteAllByTemplateIds(List<Long> templateIds) {
}
}
Loading

0 comments on commit 116c087

Please sign in to comment.