Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev backend #93

Merged
merged 25 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6a53852
feat: DebateRoom Entity ๊ตฌํ˜„
yeonjy Apr 21, 2024
8a2e0f7
feat: DebateMessage Entity ๊ตฌํ˜„
yeonjy Apr 22, 2024
b33e315
feat: Debate api ๊ตฌํ˜„
yeonjy Apr 27, 2024
bf6d396
feat: DebateRoomRepository ๊ตฌํ˜„
yeonjy Apr 27, 2024
431eb55
feat: DebateRoom ์ƒ์„ฑ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 27, 2024
d233a16
feat: DebateMessageRepository ๊ตฌํ˜„
yeonjy Apr 27, 2024
3adee85
feat: human debate message ์ €์žฅ api ๊ตฌํ˜„
yeonjy Apr 27, 2024
b4813a9
feat: DebateMessage @Builder์— sender ์ถ”๊ฐ€
yeonjy Apr 27, 2024
6e338a2
feat: Human DebateMessage ์ €์žฅ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 27, 2024
b9ef067
refactor: getDebateRoom ์ถ”์ถœ
yeonjy Apr 27, 2024
a6e0f4a
feat: AI DebateMessage ์ €์žฅ api ๊ตฌํ˜„
yeonjy Apr 27, 2024
ab6cac4
feat: AI DebateMessage ์ €์žฅ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 27, 2024
3ebe842
refactor: DebateMessage sender column ์ œ๊ฑฐ
yeonjy Apr 28, 2024
e81acab
feat: DebateRoom ์ „์ฒด ์กฐํšŒ api ๊ตฌํ˜„
yeonjy Apr 28, 2024
e8ad768
feat: DebateRoom์— Member ์ปฌ๋Ÿผ ์ถ”๊ฐ€
yeonjy Apr 28, 2024
b9c6b04
feat: DebateRoom ์ „์ฒด ์กฐํšŒ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 28, 2024
d7acbc1
feat: findAllByMember ์ถ”๊ฐ€
yeonjy Apr 28, 2024
cc9604d
feat: DebateRoom ์‚ญ์ œ api ๊ตฌํ˜„
yeonjy Apr 28, 2024
26d3d95
feat: DebateRoom ์‚ญ์ œ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 28, 2024
db3a828
feat: deleteAllDebateMessages ๊ตฌํ˜„
yeonjy Apr 28, 2024
808fa8d
feat: deleteAllDebateMessages ํ˜ธ์ถœ ์ถ”๊ฐ€
yeonjy Apr 28, 2024
3a7c20c
feat: DebateMessages ์กฐํšŒ api ๊ตฌํ˜„
yeonjy Apr 28, 2024
86ada1c
feat: DebateMessages ์กฐํšŒ ๋กœ์ง ๊ตฌํ˜„
yeonjy Apr 28, 2024
475bc2d
feat: findAllByDebateRoomId ์ถ”๊ฐ€
yeonjy Apr 28, 2024
b90d9c6
Merge pull request #90 from tukcomCD2024/feat/#81-backend-debate
yeonjy Apr 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
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();
}
}
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;
}
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;
}
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;
}
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;
}

}
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;
}
}
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
}
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);
}
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);
}
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);
}
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());
}
}
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);
}
}
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);
}
Loading