Skip to content

Commit

Permalink
Merge pull request #93 from tukcomCD2024/Dev-backend
Browse files Browse the repository at this point in the history
Dev backend
  • Loading branch information
yeonjy authored Apr 28, 2024
2 parents 0eabf40 + b90d9c6 commit 75a630d
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 0 deletions.
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);
}

0 comments on commit 75a630d

Please sign in to comment.