Skip to content

Commit

Permalink
[merge] 도메인 리팩토링 (#110)
Browse files Browse the repository at this point in the history
도메인 리팩토링
  • Loading branch information
jinkonu authored Oct 7, 2024
2 parents 71c4d05 + 4199102 commit bf4bd37
Show file tree
Hide file tree
Showing 155 changed files with 3,755 additions and 1,889 deletions.
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'com.mysql:mysql-connector-j:8.3.0'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand All @@ -49,7 +49,7 @@ dependencies {
implementation platform("org.springframework.cloud:spring-cloud-dependencies:2023.0.1")
implementation "org.springframework.cloud:spring-cloud-starter-openfeign"

//Multipart file
// Multipart file
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
Expand All @@ -60,6 +60,18 @@ dependencies {
annotationProcessor 'com.querydsl:querydsl-apt:5.0.0:jakarta'
annotationProcessor("jakarta.persistence:jakarta.persistence-api")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")

// Validator
implementation 'org.hibernate.validator:hibernate-validator:7.0.1.Final'

// GEOLOGY
implementation 'org.hibernate:hibernate-core:6.0.0.Final'
implementation 'org.hibernate:hibernate-spatial:6.0.0.Final'
implementation 'org.locationtech.jts:jts-core:1.18.1'
implementation 'com.querydsl:querydsl-sql-spatial:4.1.4'

// EXHIBITION API DATA MAPPING
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.2'
}

tasks.named('test') {
Expand Down
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.recordy.server.common.domain.JpaMetaInfoEntity;
import org.recordy.server.record.domain.Record;
import org.recordy.server.record.domain.RecordEntity;
import org.recordy.server.user.domain.UserEntity;

Expand Down Expand Up @@ -50,7 +51,7 @@ public static BookmarkEntity from(Bookmark bookmark) {
public Bookmark toDomain() {
return Bookmark.builder()
.id(id)
.record(record.toDomain())
.record(Record.from(record))
.user(user.toDomain())
.createdAt(createdAt)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.recordy.server.bookmark.repository;

import org.recordy.server.bookmark.domain.Bookmark;
import org.recordy.server.keyword.domain.Keyword;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.Map;

public interface BookmarkRepository {
// command
Bookmark save(Bookmark bookmark);
Expand All @@ -17,5 +14,4 @@ public interface BookmarkRepository {
Slice<Bookmark> findAllByBookmarksOrderByIdDesc(long userId, Long cursor, Pageable pageable);
boolean existsByUserIdAndRecordId(Long userId, Long recordId);
long countByUserId(Long userId);
Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.recordy.server.bookmark.repository.impl;

import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.recordy.server.common.util.QueryDslUtils;
import org.recordy.server.bookmark.domain.BookmarkEntity;
import org.recordy.server.keyword.domain.KeywordEntity;
import org.recordy.server.user.domain.QUserEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
Expand All @@ -18,7 +14,6 @@

import static org.recordy.server.record.domain.QRecordEntity.recordEntity;
import static org.recordy.server.bookmark.domain.QBookmarkEntity.bookmarkEntity;
import static org.recordy.server.record.domain.QUploadEntity.uploadEntity;

@RequiredArgsConstructor
@Repository
Expand All @@ -41,21 +36,4 @@ public Slice<BookmarkEntity> findAllByUserOrderByIdDesc(long userId, Long cursor

return new SliceImpl<>(bookmarkEntities, pageable, QueryDslUtils.hasNext(pageable, bookmarkEntities));
}

public Map<KeywordEntity, Long> countAllByUserIdGroupByKeyword(long userId) {
List<Tuple> result = jpaQueryFactory
.select(uploadEntity.keyword, bookmarkEntity.count())
.from(bookmarkEntity)
.join(bookmarkEntity.record, recordEntity)
.join(recordEntity.uploads, uploadEntity)
.where(bookmarkEntity.user.id.eq(userId))
.groupBy(uploadEntity.keyword)
.fetch();

return result.stream()
.collect(Collectors.toMap(
tuple -> tuple.get(uploadEntity.keyword),
tuple -> tuple.get(bookmarkEntity.count())
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import org.recordy.server.bookmark.domain.Bookmark;
import org.recordy.server.bookmark.domain.BookmarkEntity;
import org.recordy.server.bookmark.repository.BookmarkRepository;
import org.recordy.server.keyword.domain.Keyword;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Repository
public class BookmarkRepositoryImpl implements BookmarkRepository {
Expand Down Expand Up @@ -52,14 +48,4 @@ public boolean existsByUserIdAndRecordId(Long userId, Long recordId) {
public long countByUserId(Long userId) {
return bookmarkJpaRepository.countAllByUserId(userId);
}

@Override
public Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId) {
return bookmarkQueryDslRepository.countAllByUserIdGroupByKeyword(userId)
.entrySet().stream()
.collect(Collectors.toMap(
entry -> entry.getKey().toDomain(),
Map.Entry::getValue
));
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/recordy/server/common/config/GeometryConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.recordy.server.common.config;

import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GeometryConfig {

private static final int WGS84_SRID = 4326;

@Bean
public GeometryFactory geometryFactory() {
return new GeometryFactory(new PrecisionModel(), WGS84_SRID);
}
}
18 changes: 16 additions & 2 deletions src/main/java/org/recordy/server/common/config/QueryDslConfig.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package org.recordy.server.common.config;

import com.querydsl.jpa.JPQLTemplates;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.spatial.SpatialOps;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueryDslConfig {

@PersistenceContext
private EntityManager entityManager;

private static class RecordyJPQLTemplates extends JPQLTemplates {

public RecordyJPQLTemplates() {
add(SpatialOps.BUFFER, "ST_Buffer({0}, {1})");
add(SpatialOps.CONTAINS, "ST_Contains({0}, {1})");
}
}

@Bean
public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(new RecordyJPQLTemplates(), entityManager);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.recordy.server.common.handler;

import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.recordy.server.common.dto.response.ErrorResponse;
import org.recordy.server.common.exception.RecordyException;
Expand Down Expand Up @@ -67,6 +68,13 @@ protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(fi
.body(ErrorResponse.of(HttpStatus.BAD_REQUEST.value(), fieldError.getDefaultMessage()));
}

@ExceptionHandler(ConstraintViolationException.class)
protected ResponseEntity<ErrorResponse> handleConstraintViolationException(final ConstraintViolationException e) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ErrorResponse.of(HttpStatus.BAD_REQUEST.value(), e.getMessage()));
}

// COMMON - SERVER ERROR
@ExceptionHandler(Exception.class)
protected ResponseEntity<ErrorResponse> handleException(final Exception error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ public enum ErrorMessage {
/**
* RECORD
*/

FORBIDDEN_DELETE_RECORD(HttpStatus.FORBIDDEN, "삭제가 불가능한 기록입니다."),
RECORD_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 레코드입니다."),

/**
* EXHIBITION
*/
EXHIBITION_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 전시회입니다."),

/**
* PLACE
*/
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 장소입니다."),
PLACE_GOOGLE_NO_RESULT(HttpStatus.NOT_FOUND, "구글 지도 검색 결과가 없습니다.")
;

private final HttpStatus httpStatus;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/recordy/server/common/util/DomainUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.recordy.server.common.util;

import java.util.Objects;
import java.util.function.Function;

public class DomainUtils {

public static <T> T updateIfNotNull(T existing, T update) {
if (Objects.nonNull(update)) {
return update;
}

return existing;
}

public static String updateIfNotEmpty(String existing, String update) {
if (Objects.nonNull(update) && !update.isEmpty()) {
return update;
}

return existing;
}


public static <T, R> R mapIfNotNull(T entity, Function<T, R> mapper) {
if (Objects.nonNull(entity)) {
return mapper.apply(entity);
}

return null;
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/recordy/server/common/util/QueryDslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.NumberPath;
import org.recordy.server.record.domain.RecordEntity;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand All @@ -21,4 +20,13 @@ public static <T> boolean hasNext(Pageable pageable, List<T> content) {

return false;
}

public static <T> boolean hasNext(int size, List<T> content) {
if (content.size() > size) {
content.remove(size);
return true;
}

return false;
}
}
Loading

0 comments on commit bf4bd37

Please sign in to comment.