Skip to content

Commit

Permalink
#55 [MERGE] 아바타 관련 API (#68)
Browse files Browse the repository at this point in the history
## ☀️ 작업 사항 
- 유저 아바타 정보(현재 아바타, 해금한 아바타들) 불러오기 API
 - 아바타 해금 API
 - 유저 아바타 변경 API

## ☀️ 관련 이슈
- resolved : #55 

## ☀️ 참고사항
> **ERD 변경이 있습니다! 확인 부탁드립니다.**
- avatar table 변경 후 작업하려고 하셨던 코드에 오류가 나서 임시로 수정해두었습니다! avatarTheme enum
value 관련 에러였습니다. 편하신대로 수정 부탁드립니다.
  • Loading branch information
05AM authored Oct 28, 2023
2 parents f43b5c1 + 1df66d0 commit 597a6bc
Show file tree
Hide file tree
Showing 23 changed files with 485 additions and 438 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public class AlarmService {
private final UserRepository userRepository;
private final MissionRepository missionRepository;
private final MissionObjectRepository missionObjectRepository;
private final RoomRepository roomRepository;

// 알람 생성
@Transactional
public Alarm createAlarm(Integer userId, PostAlarmReq postAlarmReq) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(Status.USER_NOT_FOUND));
Room room = postAlarmReq.getRoomId() != null
? roomRepository.findById(postAlarmReq.getRoomId())
.orElseThrow(() -> new BaseException(Status.ROOM_NOT_FOUND))
: null;

Mission mission = null;
MissionObject missionObject = null;

Expand Down Expand Up @@ -73,7 +69,6 @@ public Alarm createAlarm(Integer userId, PostAlarmReq postAlarmReq) {
.user(user)
.mission(mission)
.missionObject(missionObject)
.room(room)
.build();

return alarmRepository.save(alarm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,4 @@ public class PostAlarmReq {

@Schema(description = "미션 객체 id", requiredMode = RequiredMode.REQUIRED, example = "1")
private Integer missionObjectId;

@Schema(description = "그룹 id", requiredMode = RequiredMode.REQUIRED, example = "1")
private Integer roomId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
@Entity
@Table(name = "alarm")
@DynamicInsert
@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Alarm {
Expand Down Expand Up @@ -73,7 +72,7 @@ public class Alarm {
public Alarm(String name, String icon, Integer snoozeInterval, Integer snoozeCnt, String alarmTime,
Boolean monday, Boolean tuesday, Boolean wednesday, Boolean thursday, Boolean friday,
Boolean saturday, Boolean sunday, Boolean isSnoozeActivated, Boolean isVibrate,
User user, Mission mission, MissionObject missionObject, Room room) {
User user, Mission mission, MissionObject missionObject) {
this.name = name;
this.icon = icon;
this.snoozeInterval = snoozeInterval;
Expand All @@ -91,7 +90,6 @@ public Alarm(String name, String icon, Integer snoozeInterval, Integer snoozeCnt
this.user = user;
this.mission = mission;
this.missionObject = missionObject;
this.room = room;
}

// patch
Expand Down Expand Up @@ -143,6 +141,7 @@ public void modifyProperties(String name, String icon,
setMission(mission);
setMissionObject(missionObject);
}

public boolean getDayOfWeekValue(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case MONDAY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
import com.wakeUpTogetUp.togetUp.api.auth.LoginType;
import com.wakeUpTogetUp.togetUp.api.auth.dto.request.SocialLoginReq;
import com.wakeUpTogetUp.togetUp.api.auth.dto.response.SocialUserRes;
import com.wakeUpTogetUp.togetUp.api.avatar.AvatarRepository;
import com.wakeUpTogetUp.togetUp.api.avatar.model.Avatar;
import com.wakeUpTogetUp.togetUp.api.users.UserAvatarPurchaseLogRepository;
import com.wakeUpTogetUp.togetUp.api.users.UserAvatarRepository;
import com.wakeUpTogetUp.togetUp.api.users.UserAvatarService;
import com.wakeUpTogetUp.togetUp.api.users.model.User;
import com.wakeUpTogetUp.togetUp.api.users.model.UserAvatar;
import com.wakeUpTogetUp.togetUp.api.users.model.UserAvatarPurchaseLog;
import com.wakeUpTogetUp.togetUp.common.Status;
import com.wakeUpTogetUp.togetUp.exception.BaseException;
import com.wakeUpTogetUp.togetUp.utils.JwtService;
import com.wakeUpTogetUp.togetUp.api.users.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -34,31 +27,31 @@
@RequiredArgsConstructor
@RequestMapping("/app/auth")
public class AuthService {

private final List<SocialLoginService> loginServices;
private final JwtService jwtService;
private final UserRepository userRepository;
private final AvatarRepository avatarRepository;
private final UserAvatarRepository userAvatarRepository;
private final UserAvatarPurchaseLogRepository userAvatarPurchaseLogRepository;
private final UserAvatarService userAvatarService;
private final AppleLoginServiceImpl appleLoginService;
private final UserRepository userRepository;


@Transactional
public LoginRes socialLogin(SocialLoginReq socialLoginReq) {
public LoginRes socialLogin(SocialLoginReq socialLoginReq) {

//각 소셜로그인에 맞는 서비스 가져오기
SocialLoginService loginService = this.getLoginService(socialLoginReq.getLoginType());

//소셜로그인 유저 정보 가져오기
SocialUserRes socialUserRes = loginService.getUserInfo(socialLoginReq.getOauthAccessToken());
SocialUserRes socialUserRes = loginService.getUserInfo(
socialLoginReq.getOauthAccessToken());
log.info("socialUserResponse {} ", socialUserRes.toString());


if(socialLoginReq.getLoginType().equals(LoginType.APPLE))
if (socialLoginReq.getLoginType().equals(LoginType.APPLE)) {
socialUserRes.setName(socialLoginReq.getUserName());
}

//저장된 유저 or 저장한유저의 id 가져오기
Integer userId = this.getSignedUserId(socialUserRes,socialLoginReq.getLoginType());
Integer userId = this.getSignedUserId(socialUserRes, socialLoginReq.getLoginType());

//accessToken 만들기
String accessToken = jwtService.generateAccessToken(userId);
Expand All @@ -70,8 +63,9 @@ public LoginRes socialLogin(SocialLoginReq socialLoginReq) {
.accessToken(accessToken)
.build();
}
private SocialLoginService getLoginService(LoginType loginType){
for (SocialLoginService loginService: loginServices) {

private SocialLoginService getLoginService(LoginType loginType) {
for (SocialLoginService loginService : loginServices) {
if (loginType.equals(loginService.getServiceName())) {
log.info("login service name: {}", loginService.getServiceName());
return loginService;
Expand All @@ -80,11 +74,13 @@ private SocialLoginService getLoginService(LoginType loginType){
//todo 수정
return loginServices.get(0);
}
private Integer getSignedUserId(SocialUserRes socialUserRes,LoginType loginType ) {
User savedUser =userRepository.findBySocialId(socialUserRes.getId()).orElse(null);

private Integer getSignedUserId(SocialUserRes socialUserRes, LoginType loginType) {
User savedUser = userRepository.findBySocialId(socialUserRes.getId()).orElse(null);
//유저가 있으면 아이디 반환
if(Objects.nonNull(savedUser))
if (Objects.nonNull(savedUser)) {
return savedUser.getId();
}

// 유저 저장
User user = userRepository.save(
Expand All @@ -96,23 +92,9 @@ private Integer getSignedUserId(SocialUserRes socialUserRes,LoginType loginType
.build()
);

// 기본 아바타 가져오기
Avatar defaultAvatar = avatarRepository.findById(DEFAULT_AVATAR_ID)
.orElseThrow(() -> new BaseException(Status.AVATAR_NOT_FOUND));

// 기본 아바타 구매 내역 row 생성
UserAvatarPurchaseLog userAvatarPurchaseLog = UserAvatarPurchaseLog.builder()
.user(user)
.avatar(defaultAvatar)
.build();
userAvatarPurchaseLogRepository.save(userAvatarPurchaseLog);

// 기본 아바타 설정 row 생성
UserAvatar userAvatar = UserAvatar.builder()
.user(user)
.avatar(defaultAvatar)
.build();
userAvatarRepository.save(userAvatar);
// 유저 기본 아바타 설정
userAvatarService.createUserAvatar(user, DEFAULT_AVATAR_ID);
userAvatarService.changeUserAvatar(user.getId(), DEFAULT_AVATAR_ID);

return user.getId();
}
Expand All @@ -121,11 +103,8 @@ private Integer getSignedUserId(SocialUserRes socialUserRes,LoginType loginType
@Transactional
public void disconnectApple(String authCode) throws IOException {


AppleTokenRes appleToken = appleLoginService.getAppleToken(authCode);
appleLoginService.revoke(appleToken.getAccessToken());
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum AvatarTheme {
Expand All @@ -15,6 +12,4 @@ public enum AvatarTheme {
CUTE_BUNNY("깜찍한 토끼");

private final String value;


}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.wakeUpTogetUp.togetUp.api.avatar.model;

import com.wakeUpTogetUp.togetUp.api.avatar.AvatarTheme;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.sql.Timestamp;
import java.time.Instant;
import org.hibernate.annotations.DynamicInsert;

@Setter
@Getter
@Entity
@Table(name="avatar")
@DynamicInsert
@NoArgsConstructor
public class Avatar {

Expand All @@ -20,20 +21,22 @@ public class Avatar {
@Column(columnDefinition = "INT UNSIGNED")
private Integer id;

private String theme;

@Column(columnDefinition = "TEXT")
private String avatarImgLink;
@Enumerated(EnumType.STRING)
private AvatarTheme theme;

@Column(columnDefinition = "INT UNSIGNED")
private Integer price;

@Column(name = "createdAt")
private Timestamp createdAt;
@Column(columnDefinition = "INT UNSIGNED")
private Integer unlockLevel;

// TODO : 더 좋은 방법 찾기
@Column(columnDefinition = "TIMESTAMP")
private String createdAt;

@PrePersist
void createdAt() {
this.createdAt = Timestamp.from(Instant.now());
}
// @PrePersist
// void createdAt() {
// this.createdAt = Timestamp.from(Instant.now());
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.wakeUpTogetUp.togetUp.api.avatar.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Schema(description = "유저 아바타 목록 가져오기")
@Getter
@Setter
@NoArgsConstructor
public class UserAvatarData {

@Schema(description = "아바타 Id")
private int avatarId;

@Schema(description = "아바타 테마 이름")
private String theme;

@Schema(description = "가격")
private int price;

@Schema(description = "해금 레벨")
private int unlockLevel;

@Schema(description = "유저 보유 여부")
private Boolean isUnlocked;
}

This file was deleted.

Loading

0 comments on commit 597a6bc

Please sign in to comment.