Skip to content

Commit

Permalink
Merge pull request #32 from EunChanNam/develop
Browse files Browse the repository at this point in the history
release: User 제외 모든 도메인 테스트 적용
  • Loading branch information
EunChanNam authored Nov 14, 2023
2 parents 038bcf7 + c437673 commit 36724ca
Show file tree
Hide file tree
Showing 107 changed files with 4,054 additions and 741 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.h2database:h2'

//Redis
implementation('it.ozimov:embedded-redis:0.7.2')
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

//네이버 클라우드
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

Expand Down Expand Up @@ -119,7 +123,8 @@ jacocoTestReport {
'**/*Application*',
"**/config/**",
"**/exception/**",
"**/dto/**"
"**/dto/**",
"**/Q*/**"
])
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class TokenReissueController {

private final TokenReissueService tokenReissueService;

@PostMapping("/token/reissue")
public ResponseEntity<TokenResponse> reissueToken(
final @FetchAuthInfo AuthInfo authInfo
) {
@PostMapping("/tokens/reissue")
public ResponseEntity<TokenResponse> reissueToken(@FetchAuthInfo AuthInfo authInfo) {
TokenResponse response = tokenReissueService.reissueToken(authInfo.userId(), authInfo.token());
return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
Expand All @@ -16,18 +15,9 @@
@Getter
public class BaseEntity {

@Column(
nullable = false,
insertable = false,
updatable = false,
columnDefinition = "datetime default CURRENT_TIMESTAMP")
@CreatedDate
protected LocalDateTime createdDate;

@Column(
nullable = false,
insertable = false,
columnDefinition = "datetime default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP")
@LastModifiedDate
private LocalDateTime updatedDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.inq.wishhair.wesharewishhair.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
public String host;
@Value("${spring.data.redis.port}")
public int port;

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

@Bean
public RedisTemplate<String, Long> cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.inq.wishhair.wesharewishhair.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SchedulerConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.inq.wishhair.wesharewishhair.review.presentation.ReviewController;
import com.inq.wishhair.wesharewishhair.review.presentation.ReviewSearchController;
import com.inq.wishhair.wesharewishhair.point.presentation.PointController;
import com.inq.wishhair.wesharewishhair.point.presentation.PointSearchController;
import com.inq.wishhair.wesharewishhair.user.presentation.UserController;
import com.inq.wishhair.wesharewishhair.user.presentation.UserInfoController;

Expand All @@ -26,7 +25,7 @@
ReviewController.class, WishHairController.class, AuthController.class,
TokenReissueController.class, MailAuthController.class,
UserInfoController.class, LikeReviewController.class, ReviewSearchController.class,
PointSearchController.class, PointController.class
PointController.class
})
public class ApiExceptionHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public enum ErrorCode {
FLASK_SERVER_EXCEPTION("FLASK_001", "Flask 서버 요청 간 에러가 발생하였습니다.", HttpStatus.INTERNAL_SERVER_ERROR),
FLASK_RESPONSE_ERROR("FLASK_002", "Flask 서버의 응답값의 형식이 올바르지 않습니다.", HttpStatus.INTERNAL_SERVER_ERROR),

AOP_GENERIC_EXCEPTION("AOP_001", "AOP 에서 발생한 Generic 에러 입니다.", HttpStatus.INTERNAL_SERVER_ERROR);
AOP_GENERIC_EXCEPTION("AOP_001", "AOP 에서 발생한 Generic 에러 입니다.", HttpStatus.INTERNAL_SERVER_ERROR),

REDIS_FAIL_ACQUIRE_LOCK("REDIS_001", "서버 일시적 오류입니다. 재시도 해주세요", HttpStatus.INTERNAL_SERVER_ERROR);

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public static Pageable generateSimplePageable(int size) {
public static Pageable generateDateDescPageable(int size) {
return PageRequest.of(0, size, Sort.by(Sort.Direction.DESC, DATE));
}

public static Pageable generateDateAscPageable(int size) {
return PageRequest.of(0, size, Sort.by(Sort.Direction.ASC, DATE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.inq.wishhair.wesharewishhair.global.utils;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {

private final RedisTemplate<String, Long> redisTemplate;
private final long expireTime;

public RedisUtils(
RedisTemplate<String, Long> redisTemplate,
@Value("${spring.data.redis.expire-time}") long expireTime
) {
this.redisTemplate = redisTemplate;
this.expireTime = expireTime;
}

public void setData(Long key, Long value) {
redisTemplate
.opsForValue()
.set(String.valueOf(key), value, expireTime, TimeUnit.MILLISECONDS);
}

public void increaseData(Long key) {
redisTemplate.opsForValue().increment(String.valueOf(key));
}

public void decreaseData(Long key) {
redisTemplate.opsForValue().decrement(String.valueOf(key));
}

public Optional<Long> getData(Long key) {
return Optional.ofNullable(
redisTemplate.opsForValue().get(String.valueOf(key))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HairStyleFindService {

private final HairStyleRepository hairStyleRepository;

public HairStyle findById(Long id) {
public HairStyle getById(Long id) {
return hairStyleRepository.findById(id)
.orElseThrow(() -> new WishHairException(ErrorCode.NOT_EXIST_KEY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
import com.inq.wishhair.wesharewishhair.global.dto.response.ResponseWrapper;
import com.inq.wishhair.wesharewishhair.global.exception.ErrorCode;
import com.inq.wishhair.wesharewishhair.global.exception.WishHairException;
import com.inq.wishhair.wesharewishhair.hairstyle.application.query.HairStyleQueryRepository;
import com.inq.wishhair.wesharewishhair.hairstyle.application.dto.response.HairStyleResponse;
import com.inq.wishhair.wesharewishhair.hairstyle.application.dto.response.HairStyleSimpleResponse;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyle;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyleQueryRepository;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyleRepository;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.hashtag.Tag;
import com.inq.wishhair.wesharewishhair.hairstyle.application.dto.response.HairStyleResponse;
import com.inq.wishhair.wesharewishhair.hairstyle.application.dto.response.HairStyleSimpleResponse;
import com.inq.wishhair.wesharewishhair.hairstyle.utils.HairRecommendCondition;
import com.inq.wishhair.wesharewishhair.user.domain.entity.User;
import com.inq.wishhair.wesharewishhair.user.application.UserFindService;
import com.inq.wishhair.wesharewishhair.user.domain.entity.User;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class HairStyleSearchService {

private final HairStyleRepository hairStyleRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inq.wishhair.wesharewishhair.global.exception.ErrorCode;
import com.inq.wishhair.wesharewishhair.global.exception.WishHairException;
import com.inq.wishhair.wesharewishhair.hairstyle.application.dto.response.WishHairResponse;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.wishhair.WishHair;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.wishhair.WishHairRepository;

import jakarta.persistence.EntityExistsException;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -18,55 +17,39 @@ public class WishHairService {

private final WishHairRepository wishHairRepository;

@Transactional
public void executeWish(
private boolean existWishHair(
final Long hairStyleId,
final Long userId
) {
validateDoesNotExistWishHair(hairStyleId, userId);

wishHairRepository.save(WishHair.createWishHair(userId, hairStyleId));
return wishHairRepository.existsByHairStyleIdAndUserId(hairStyleId, userId);
}

@Transactional
public void cancelWish(
final Long hairStyleId,
final Long userId
) {
validateDoesWishHairExist(hairStyleId, userId);

wishHairRepository.deleteByHairStyleIdAndUserId(hairStyleId, userId);
}

public WishHairResponse checkIsWishing(
public boolean executeWish(
final Long hairStyleId,
final Long userId
) {
return new WishHairResponse(existWishHair(hairStyleId, userId));
}

private boolean existWishHair(
final Long hairStyleId,
final Long userId
) {
return wishHairRepository.existsByHairStyleIdAndUserId(hairStyleId, userId);
try {
wishHairRepository.save(WishHair.createWishHair(userId, hairStyleId));
} catch (EntityExistsException e) {
return false;
}
return true;
}

private void validateDoesWishHairExist(
@Transactional
public boolean cancelWish(
final Long hairStyleId,
final Long userId
) {
if (!existWishHair(hairStyleId, userId)) {
throw new WishHairException(ErrorCode.WISH_HAIR_NOT_EXIST);
}
wishHairRepository.deleteByHairStyleIdAndUserId(hairStyleId, userId);
return true;
}

private void validateDoesNotExistWishHair(
public WishHairResponse checkIsWishing(
final Long hairStyleId,
final Long userId
) {
if (existWishHair(hairStyleId, userId)) {
throw new WishHairException(ErrorCode.WISH_HAIR_ALREADY_EXIST);
}
return new WishHairResponse(existWishHair(hairStyleId, userId));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.inq.wishhair.wesharewishhair.hairstyle.application.query;
package com.inq.wishhair.wesharewishhair.hairstyle.domain;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyle;
import com.inq.wishhair.wesharewishhair.hairstyle.utils.HairRecommendCondition;

public interface HairStyleQueryRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.inq.wishhair.wesharewishhair.hairstyle.domain.wishhair;

import java.time.LocalDateTime;

import com.inq.wishhair.wesharewishhair.global.auditing.BaseEntity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -22,16 +19,13 @@ public class WishHair extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@JoinColumn
private Long hairStyleId;

@JoinColumn
private Long userId;

private WishHair(final Long hairStyleId, final Long userId) {
this.hairStyleId = hairStyleId;
this.userId = userId;
this.createdDate = LocalDateTime.now();
}

//==Factory method==//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package com.inq.wishhair.wesharewishhair.hairstyle.infrastructure;

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 com.inq.wishhair.wesharewishhair.hairstyle.domain.wishhair.WishHair;
import com.inq.wishhair.wesharewishhair.hairstyle.domain.wishhair.WishHairRepository;

public interface WishHairJpaRepository extends WishHairRepository, JpaRepository<WishHair, Long> {

@Modifying
@Query("delete from WishHair w where w.hairStyleId = :hairStyleId and w.userId = :userId")
void deleteByHairStyleIdAndUserId(@Param("hairStyleId") Long hairStyleId,
@Param("userId") Long userId);
void deleteByHairStyleIdAndUserId(Long hairStyleId, Long userId);

boolean existsByHairStyleIdAndUserId(Long hairStyleId, Long userId);
}
Loading

0 comments on commit 36724ca

Please sign in to comment.