-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from tukcomCD2024/Dev-backend
Dev backend
- Loading branch information
Showing
14 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...core/src/main/java/com/rollthedice/backend/domain/debate/controller/DebateController.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,59 @@ | ||
package com.rollthedice.backend.domain.debate.controller; | ||
|
||
import com.rollthedice.backend.domain.debate.dto.request.DebateMessageRequest; | ||
import com.rollthedice.backend.domain.debate.dto.request.DebateRoomRequest; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateMessageResponse; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateRoomResponse; | ||
import com.rollthedice.backend.domain.debate.service.DebateMessageService; | ||
import com.rollthedice.backend.domain.debate.service.DebateRoomService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("debates") | ||
public class DebateController { | ||
private final DebateRoomService debateRoomService; | ||
private final DebateMessageService debateMessageService; | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping("") | ||
public void saveDebateRoom(@RequestBody @Valid final DebateRoomRequest request) { | ||
debateRoomService.saveDebateRoom(request); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("") | ||
public List<DebateRoomResponse> getDebateRooms(final Pageable pageable) { | ||
return debateRoomService.getDebateRooms(pageable); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@DeleteMapping("/{roomId}") | ||
public void deleteDebateRoom(@PathVariable final Long roomId) { | ||
debateRoomService.deleteDebateRoom(roomId); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping("/{roomId}/human") | ||
public void saveHumanDebateMessage(@PathVariable final Long roomId, @RequestBody final DebateMessageRequest request) { | ||
debateMessageService.saveHumanDebateMessage(roomId, request); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping("/{roomId}/ai") | ||
public void saveAIDebateMessage(@PathVariable final Long roomId, @RequestBody final DebateMessageRequest request) { | ||
debateMessageService.saveAIDebateMessage(roomId, request); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("/{roomId}") | ||
public List<DebateMessageResponse> getDebateMessages(@PathVariable final Long roomId) { | ||
return debateMessageService.getDebateMessages(roomId); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...src/main/java/com/rollthedice/backend/domain/debate/dto/request/DebateMessageRequest.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,33 @@ | ||
package com.rollthedice.backend.domain.debate.dto.request; | ||
|
||
import com.rollthedice.backend.domain.debate.entity.DebateMessage; | ||
import com.rollthedice.backend.domain.debate.entity.DebateRoom; | ||
import com.rollthedice.backend.domain.debate.entity.SenderType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class DebateMessageRequest { | ||
private String message; | ||
|
||
public DebateMessage toHumanMessageEntity(DebateRoom debateRoom) { | ||
return DebateMessage.builder() | ||
.message(this.message) | ||
.senderType(SenderType.HUMAN) | ||
.debateRoom(debateRoom) | ||
.build(); | ||
} | ||
|
||
public DebateMessage toAIMessageEntity(DebateRoom debateRoom) { | ||
return DebateMessage.builder() | ||
.message(this.message) | ||
.senderType(SenderType.AI) | ||
.debateRoom(debateRoom) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...re/src/main/java/com/rollthedice/backend/domain/debate/dto/request/DebateRoomRequest.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,15 @@ | ||
package com.rollthedice.backend.domain.debate.dto.request; | ||
|
||
import com.rollthedice.backend.domain.debate.entity.DebateRoom; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class DebateRoomRequest { | ||
private String topic; | ||
} |
16 changes: 16 additions & 0 deletions
16
...c/main/java/com/rollthedice/backend/domain/debate/dto/response/DebateMessageResponse.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,16 @@ | ||
package com.rollthedice.backend.domain.debate.dto.response; | ||
|
||
import com.rollthedice.backend.domain.debate.entity.SenderType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DebateMessageResponse { | ||
private String message; | ||
private SenderType senderType; | ||
} |
15 changes: 15 additions & 0 deletions
15
.../src/main/java/com/rollthedice/backend/domain/debate/dto/response/DebateRoomResponse.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,15 @@ | ||
package com.rollthedice.backend.domain.debate.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DebateRoomResponse { | ||
private Long id; | ||
private String topic; | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/core/src/main/java/com/rollthedice/backend/domain/debate/entity/DebateMessage.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,35 @@ | ||
package com.rollthedice.backend.domain.debate.entity; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.global.config.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class DebateMessage extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String message; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private SenderType senderType; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "debate_room_id") | ||
private DebateRoom debateRoom; | ||
|
||
@Builder | ||
public DebateMessage(String message, SenderType senderType, DebateRoom debateRoom) { | ||
this.message = message; | ||
this.senderType = senderType; | ||
this.debateRoom = debateRoom; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
backend/core/src/main/java/com/rollthedice/backend/domain/debate/entity/DebateRoom.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,30 @@ | ||
package com.rollthedice.backend.domain.debate.entity; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.global.config.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class DebateRoom extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String topic; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@Builder | ||
public DebateRoom(Member member, String topic) { | ||
this.member = member; | ||
this.topic = topic; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/core/src/main/java/com/rollthedice/backend/domain/debate/entity/SenderType.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,9 @@ | ||
package com.rollthedice.backend.domain.debate.entity; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum SenderType { | ||
HUMAN, | ||
AI | ||
} |
12 changes: 12 additions & 0 deletions
12
.../core/src/main/java/com/rollthedice/backend/domain/debate/mapper/DebateMessageMapper.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,12 @@ | ||
package com.rollthedice.backend.domain.debate.mapper; | ||
|
||
import com.rollthedice.backend.domain.debate.dto.request.DebateMessageRequest; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateMessageResponse; | ||
import com.rollthedice.backend.domain.debate.entity.DebateMessage; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingConstants; | ||
|
||
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING) | ||
public interface DebateMessageMapper { | ||
DebateMessageResponse toResponse(DebateMessage debateMessage); | ||
} |
16 changes: 16 additions & 0 deletions
16
...end/core/src/main/java/com/rollthedice/backend/domain/debate/mapper/DebateRoomMapper.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,16 @@ | ||
package com.rollthedice.backend.domain.debate.mapper; | ||
|
||
import com.rollthedice.backend.domain.debate.dto.request.DebateRoomRequest; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateRoomResponse; | ||
import com.rollthedice.backend.domain.debate.entity.DebateRoom; | ||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingConstants; | ||
|
||
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING) | ||
public interface DebateRoomMapper { | ||
|
||
DebateRoom toEntity(final Member member, final DebateRoomRequest request); | ||
|
||
DebateRoomResponse toResponse(final DebateRoom room); | ||
} |
14 changes: 14 additions & 0 deletions
14
.../src/main/java/com/rollthedice/backend/domain/debate/repository/DebateRoomRepository.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,14 @@ | ||
package com.rollthedice.backend.domain.debate.repository; | ||
|
||
import com.rollthedice.backend.domain.debate.entity.DebateRoom; | ||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface DebateRoomRepository extends JpaRepository<DebateRoom, Long> { | ||
List<DebateRoom> findAllByMember(Member member, Pageable pageable); | ||
} |
47 changes: 47 additions & 0 deletions
47
...ore/src/main/java/com/rollthedice/backend/domain/debate/service/DebateMessageService.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,47 @@ | ||
package com.rollthedice.backend.domain.debate.service; | ||
|
||
import com.rollthedice.backend.domain.debate.dto.request.DebateMessageRequest; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateMessageResponse; | ||
import com.rollthedice.backend.domain.debate.entity.DebateRoom; | ||
import com.rollthedice.backend.domain.debate.mapper.DebateMessageMapper; | ||
import com.rollthedice.backend.domain.debate.repository.DebateRoomRepository; | ||
import com.rollthedice.backend.domain.news.repository.DebateMessageRepository; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class DebateMessageService { | ||
private final DebateRoomRepository debateRoomRepository; | ||
private final DebateMessageRepository debateMessageRepository; | ||
private final DebateMessageMapper debateMessageMapper; | ||
|
||
@Transactional | ||
public void saveHumanDebateMessage(final Long roomId, DebateMessageRequest request) { | ||
debateMessageRepository.save(request.toHumanMessageEntity(getDebateRoom(roomId))); | ||
} | ||
|
||
public void saveAIDebateMessage(Long roomId, DebateMessageRequest request) { | ||
debateMessageRepository.save(request.toAIMessageEntity(getDebateRoom(roomId))); | ||
} | ||
|
||
private DebateRoom getDebateRoom(final Long roomId) { | ||
return debateRoomRepository.findById(roomId).orElseThrow(EntityNotFoundException::new); | ||
} | ||
|
||
@Transactional | ||
public void deleteAllDebateMessages(Long roomId) { | ||
debateMessageRepository.deleteAllInBatchByDebateRoomId(roomId); | ||
} | ||
|
||
public List<DebateMessageResponse> getDebateMessages(Long roomId) { | ||
return debateMessageRepository.findAllByDebateRoomId(roomId) | ||
.stream().map(debateMessageMapper::toResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...d/core/src/main/java/com/rollthedice/backend/domain/debate/service/DebateRoomService.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,45 @@ | ||
package com.rollthedice.backend.domain.debate.service; | ||
|
||
import com.rollthedice.backend.domain.debate.dto.request.DebateRoomRequest; | ||
import com.rollthedice.backend.domain.debate.dto.response.DebateRoomResponse; | ||
import com.rollthedice.backend.domain.debate.mapper.DebateRoomMapper; | ||
import com.rollthedice.backend.domain.debate.repository.DebateRoomRepository; | ||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.domain.member.query.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class DebateRoomService { | ||
private final AuthService authService; | ||
private final DebateRoomMapper debateRoomMapper; | ||
private final DebateRoomRepository debateRoomRepository; | ||
private final DebateMessageService debateMessageService; | ||
|
||
|
||
@Transactional | ||
public void saveDebateRoom(DebateRoomRequest request) { | ||
final Member member = authService.getMember(); | ||
debateRoomRepository.save(debateRoomMapper.toEntity(member, request)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<DebateRoomResponse> getDebateRooms(Pageable pageable) { | ||
final Member member = authService.getMember(); | ||
return debateRoomRepository.findAllByMember(member, pageable).stream() | ||
.map(debateRoomMapper::toResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void deleteDebateRoom(Long roomId) { | ||
debateMessageService.deleteAllDebateMessages(roomId); | ||
debateRoomRepository.deleteById(roomId); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...src/main/java/com/rollthedice/backend/domain/news/repository/DebateMessageRepository.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,14 @@ | ||
package com.rollthedice.backend.domain.news.repository; | ||
|
||
import com.rollthedice.backend.domain.debate.entity.DebateMessage; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface DebateMessageRepository extends JpaRepository<DebateMessage, Long> { | ||
void deleteAllInBatchByDebateRoomId(final Long id); | ||
|
||
List<DebateMessage> findAllByDebateRoomId(final Long id); | ||
} |