Skip to content

Commit

Permalink
feat: 활동뱃지 관련 기능 구현 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
punchdrunkard authored Sep 24, 2024
1 parent 07ae285 commit d890a8b
Show file tree
Hide file tree
Showing 30 changed files with 920 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/odiga/fiesta/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.odiga.fiesta.auth.domain.Authority;
import com.odiga.fiesta.auth.dto.KakaoProfile;
import com.odiga.fiesta.badge.service.BadgeService;
import com.odiga.fiesta.common.error.exception.CustomException;
import com.odiga.fiesta.common.jwt.TokenProvider;
import com.odiga.fiesta.common.util.HttpClientUtil;
Expand All @@ -40,6 +40,7 @@ public class AuthService {

private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final BadgeService badgeService;

private final TokenProvider tokenProvider;
private final NicknameUtils nicknameUtils;
Expand Down Expand Up @@ -72,6 +73,9 @@ public LoginResponse kakaoLogin(String accessTokenByClient) {
String refreshToken = issueRefreshToken(user);
boolean isProfileRegistered = !isNull(user.getUserTypeId());

// 뱃지 수여
badgeService.giveUserBadge(user.getId());

return LoginResponse.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/odiga/fiesta/badge/domain/Badge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.odiga.fiesta.badge.domain;

import static jakarta.persistence.GenerationType.*;
import static lombok.AccessLevel.*;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@Entity
@AllArgsConstructor
@Getter
@SuperBuilder
@NoArgsConstructor(access = PROTECTED)
@ToString
@Table(name = "badge", indexes = {
@Index(name = "idx_badge_type", columnList = "type")
})
public class Badge {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "badge_id")
private Long id;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String description;

@Column(name = "image_url", nullable = false)
private String imageUrl;

@Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private BadgeType type;
}
29 changes: 29 additions & 0 deletions src/main/java/com/odiga/fiesta/badge/domain/BadgeConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.odiga.fiesta.badge.domain;

import static lombok.AccessLevel.*;

import lombok.NoArgsConstructor;

@NoArgsConstructor(access = PRIVATE)
public class BadgeConstants {

public static final Long USER_JOIN_BADGE_ID = 1L;

// 리뷰 작성 관련 뱃지
public static final Long FIRST_REVIEW_BADGE_ID = 2L;
public static final Long PASSIONATE_REVIEWER_BADGE_ID = 4L;
public static final Long HISTORY_LOVER_BADGE_ID = 5L;
public static final Long MUSIC_LOVER_BADGE_ID = 6L;
public static final Long ACTIVITY_BADGE_ID = 7L;
public static final Long FOODIE_LOVER_BADGE_ID = 8L;
public static final Long MOVIE_LOVER_BADGE_ID = 9L;
public static final Long FIREWORKS_LOVER_BADGE_ID = 10L;
public static final Long NATURE_LOVER_BADGE_ID = 11L;
public static final Long NIGHT_LOVER_BADGE_ID = 12L;
public static final Long ART_LOVER_BADGE_ID = 13L;
public static final Long CULTURE_LOVER_BADGE_ID = 14L;
public static final Long UNIQUE_LOVER_BADGE_ID = 15L;

// 페스티벌 등록 관련 뱃지
public static final Long FIRST_FESTIVAL_BADGE_ID = 3L;
}
6 changes: 6 additions & 0 deletions src/main/java/com/odiga/fiesta/badge/domain/BadgeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.odiga.fiesta.badge.domain;

public enum BadgeType {

USER, REVIEW, FESTIVAL
}
40 changes: 40 additions & 0 deletions src/main/java/com/odiga/fiesta/badge/domain/UserBadge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.odiga.fiesta.badge.domain;

import static jakarta.persistence.GenerationType.*;
import static lombok.AccessLevel.*;

import com.odiga.fiesta.common.domain.BaseEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Entity
@Getter
@AllArgsConstructor
@SuperBuilder
@NoArgsConstructor(access = PROTECTED)
@Table(name = "user_badge", indexes = {
@Index(name = "idx_user_badge_user_id", columnList = "user_id"),
@Index(name = "idx_user_badge_badge_id", columnList = "badge_id")
})
public class UserBadge extends BaseEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_badge_id")
private Long id;

@Column(name = "badge_id")
private Long badgeId;

@Column(name = "user_id")
private Long userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.odiga.fiesta.badge.repository;

import java.util.List;

import com.odiga.fiesta.user.dto.response.UserBadgeResponse;

public interface BadgeCustomRepository {

List<UserBadgeResponse> findUserBadges(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.odiga.fiesta.badge.repository;

import static com.odiga.fiesta.badge.domain.QBadge.*;
import static com.odiga.fiesta.badge.domain.QUserBadge.*;

import java.util.List;

import com.odiga.fiesta.user.dto.response.UserBadgeResponse;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class BadgeCustomRepositoryImpl implements BadgeCustomRepository {

private final JPAQueryFactory queryFactory;

@Override
public List<UserBadgeResponse> findUserBadges(Long userId) {

return queryFactory.select(
Projections.fields(
UserBadgeResponse.class,
badge.id.as("badgeId"),
badge.name.as("badgeName"),
badge.description,
badge.imageUrl,
new CaseBuilder()
.when(userBadge.isNull()) // userBadge가 null이면 false
.then(false)
.when(userBadge.userId.isNull())
.then(false)
.when(userBadge.userId.eq(userId))
.then(true)
.otherwise(false).as("isAcquired")
)
)
.from(badge)
.leftJoin(userBadge)
.on(badge.id.eq(userBadge.badgeId)) // 단순 조인
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.odiga.fiesta.badge.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.odiga.fiesta.badge.domain.Badge;
import com.odiga.fiesta.badge.domain.BadgeType;

public interface BadgeRepository extends JpaRepository<Badge, Long>, BadgeCustomRepository {

@Query("SELECT b.id FROM Badge b WHERE b.type = :type")
List<Long> findIdsByType(@Param("type") BadgeType type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.odiga.fiesta.badge.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.odiga.fiesta.badge.domain.UserBadge;

public interface UserBadgeRepository extends JpaRepository<UserBadge, Long> {

@Query("SELECT ub.badgeId FROM UserBadge ub WHERE ub.userId= :userId AND ub.badgeId IN :badgeIds")
List<Long> findBadgeIdByUserIdAndBadgeIdIn(@Param("userId") Long userId, @Param("badgeIds") List<Long> badgeIds);
}
Loading

0 comments on commit d890a8b

Please sign in to comment.