Skip to content

Commit

Permalink
✨Feature/#3 아바타 생성 API / 방에 입장한 게스트 전체조회 API
Browse files Browse the repository at this point in the history
✨Feature/#3 아바타 생성 API / 방에 입장한 게스트 전체조회 API
  • Loading branch information
dongkyeomjang authored Sep 27, 2024
2 parents ca8b347 + 1993811 commit 23401ca
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Constants {
"/api/v1/no-auth/**", "api/v1/oauth/login",
"/api/v1/auth/sign-up",
"/api/v1/auth/id-duplicate",
"/api/v1/rooms/{roomId}/avartar",
"/api/v1/rooms/{roomId}/avatar",
"/api/v1/rooms/{roomId}/questions",
"/api/v1/rooms/{roomId}/guests",
"/api/v1/rooms/{roomId}/reviews",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
package com.gooaein.goojilgoojil.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gooaein.goojilgoojil.dto.global.ResponseDto;
import com.gooaein.goojilgoojil.dto.request.AvatarRequestDto;
import com.gooaein.goojilgoojil.service.GuestService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@Tag(name = "손님 API", description = "손님 관련 API")
@RequestMapping("/api/v1/rooms/{room_id}/")
public class GuestController {
private final GuestService guestService;
private final GuestService guestService;

@Operation(summary = "손님 아바타 생성하기", description = "손님에 대한 아바타를 생성합니다.")
@PostMapping("/avatar")
public ResponseDto<?> createGuestAvatar(@PathVariable("room_id") Long roomId,
@RequestBody AvatarRequestDto avatarRequestDto) {
return ResponseDto.ok(guestService.createAvatar(roomId, avatarRequestDto));
}

@Operation(summary = "방에 참가한 손님 전체조회", description = "현재 방에 참여하고 있는 손님의 id와 avatar를 전체 조회합니다.")
@GetMapping("/guests")
public ResponseDto<?> getGuests(@PathVariable("room_id") Long roomId) {
return ResponseDto.ok(guestService.getGuestsByRoomId(roomId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.gooaein.goojilgoojil.dto.request;

public record AvatarRequestDto(
String avatarBase64
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gooaein.goojilgoojil.dto.response;

import lombok.Builder;

@Builder
public record GuestResponseDto(
Long guestId,
String avatarBase64
) {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.gooaein.goojilgoojil.repository;

import com.gooaein.goojilgoojil.domain.Guest;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import com.gooaein.goojilgoojil.domain.Guest;
import com.gooaein.goojilgoojil.domain.Room;
import com.gooaein.goojilgoojil.dto.response.GuestResponseDto;

@Repository
public interface GuestRepository extends JpaRepository<Guest,Long> {
public interface GuestRepository extends JpaRepository<Guest, Long> {

@Query("SELECT new com.gooaein.goojilgoojil.dto.response.GuestResponseDto("
+ " g.id, g.avartarBase64) FROM Guest g WHERE g.room = :room")
List<GuestResponseDto> findGuestsByRoom(Room room);
}
75 changes: 73 additions & 2 deletions src/main/java/com/gooaein/goojilgoojil/service/GuestService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,86 @@
package com.gooaein.goojilgoojil.service;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gooaein.goojilgoojil.domain.Guest;
import com.gooaein.goojilgoojil.domain.Room;
import com.gooaein.goojilgoojil.domain.User;
import com.gooaein.goojilgoojil.dto.request.AvatarRequestDto;
import com.gooaein.goojilgoojil.dto.response.GuestResponseDto;
import com.gooaein.goojilgoojil.dto.response.JwtTokenDto;
import com.gooaein.goojilgoojil.dto.type.EProvider;
import com.gooaein.goojilgoojil.dto.type.ERole;
import com.gooaein.goojilgoojil.exception.CommonException;
import com.gooaein.goojilgoojil.exception.ErrorCode;
import com.gooaein.goojilgoojil.repository.GuestRepository;
import com.gooaein.goojilgoojil.repository.RoomRepository;
import com.gooaein.goojilgoojil.repository.UserRepository;
import com.gooaein.goojilgoojil.utility.JwtUtil;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class GuestService {
private final GuestRepository guestRepository;
private final GuestRepository guestRepository;
private final UserRepository userRepository;
private final RoomRepository roomRepository;
private final JwtUtil jwtUtil;

@Transactional
public JwtTokenDto createAvatar(Long roomId, AvatarRequestDto avatarRequestDto) {

User guestUser = saveUser();

Room room = getRoomByRoomId(roomId);

Guest guest = Guest.builder()
.user(guestUser)
.room(room)
.avartarBase64(avatarRequestDto.avatarBase64())
.build();

guestRepository.save(guest);

return jwtUtil.generateTokens(guestUser.getId(), ERole.GUEST);
}

// 임시 유저 저장
private User saveUser() {

String uuid = UUID.randomUUID().toString();
String nickname = "quest" + uuid.substring(0, 8);

User user = User.builder()
.serialId(uuid)
.provider(EProvider.DEFAULT)
.role(ERole.GUEST)
.nickname(nickname)
.password(uuid)
.build();

userRepository.save(user);

return user;
}

// 방 아이디로 방 조회
private Room getRoomByRoomId(Long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new CommonException(ErrorCode.NOT_FOUND_ROOM));
}

@Transactional(readOnly = true)
public List<GuestResponseDto> getGuestsByRoomId(Long roomId) {

Room room = getRoomByRoomId(roomId);

List<GuestResponseDto> guest = guestRepository.findGuestsByRoom(room);

return guest;
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/gooaein/goojilgoojil/utility/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public Claims validateToken(String token){
.parseClaimsJws(token)
.getBody();
}
public String generateToken(Long id, ERole role, Integer expiration){
public String generateToken(Long id, ERole role, Integer expiration, String tokenType){
Claims claims = Jwts.claims();
claims.put(Constants.CLAIM_USER_ID, id);
claims.put("token_type", tokenType);
if (role != null)
claims.put(Constants.CLAIM_USER_ROLE, role);


return Jwts.builder()
.setHeaderParam(Header.JWT_TYPE, Header.JWT_TYPE)
.setClaims(claims)
Expand All @@ -57,8 +59,8 @@ public String generateToken(Long id, ERole role, Integer expiration){
}
public JwtTokenDto generateTokens(Long id, ERole role){
return JwtTokenDto.of(
generateToken(id, role, accessExpiration),
generateToken(id, role, refreshExpiration)
generateToken(id, role, accessExpiration, "access"),
generateToken(id, role, refreshExpiration, "refresh")
);
}

Expand All @@ -70,7 +72,7 @@ public String generateAccessTokenFromRefreshToken(String refreshToken) {
ERole role = ERole.valueOf(claims.get(Constants.CLAIM_USER_ROLE, String.class));

// 새 Access Token 생성
return generateToken(userId, role, accessExpiration);
return generateToken(userId, role, accessExpiration, "access");
} else {
throw new IllegalArgumentException("Invalid Refresh Token");
}
Expand Down

0 comments on commit 23401ca

Please sign in to comment.