-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Feature/#3 아바타 생성 API / 방에 입장한 게스트 전체조회 API
✨Feature/#3 아바타 생성 API / 방에 입장한 게스트 전체조회 API
- Loading branch information
Showing
7 changed files
with
134 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 27 additions & 2 deletions
29
src/main/java/com/gooaein/goojilgoojil/controller/GuestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/gooaein/goojilgoojil/dto/request/AvatarRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/gooaein/goojilgoojil/dto/response/GuestResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
14 changes: 11 additions & 3 deletions
14
src/main/java/com/gooaein/goojilgoojil/repository/GuestRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
src/main/java/com/gooaein/goojilgoojil/service/GuestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters