Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/situation' into dev-check
Browse files Browse the repository at this point in the history
  • Loading branch information
nohy6630 committed Nov 17, 2023
2 parents ccc4310 + 19bb7ab commit a7b140a
Show file tree
Hide file tree
Showing 21 changed files with 710 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.numberone.backend.domain.conversation.controller;

import com.numberone.backend.domain.conversation.dto.request.CreateChildConversationRequest;
import com.numberone.backend.domain.conversation.dto.request.CreateConversationRequest;
import com.numberone.backend.domain.conversation.dto.response.GetConversationResponse;
import com.numberone.backend.domain.conversation.service.ConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@Tag(name = "conversations", description = "대화(재난상황 댓글) 관련 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/conversations")
public class ConversationController {
private final ConversationService conversationService;

@Operation(summary = "(대댓글x) 대화 생성하기", description = """
대화 내용을 body에 담아 전달해주세요.
어떤 재난상황에 대한 대화인지 재난상황 id를 body에 같이 담아 전달해주세요.
""")
@PostMapping()
public void createConversation(Authentication authentication, @RequestBody @Valid CreateConversationRequest createConversationRequest) {
conversationService.createConversation(authentication.getName(), createConversationRequest);
}

@Operation(summary = "대댓글 대화 생성하기", description = """
대댓글 대화 내용을 body에 담아 전달해주세요.
어떤 대화(댓글)의 대댓글인지 대화 id를 파라미터로 전달해주세요.
""")
@PostMapping("/{conversationId}")
public void createChildConversation(Authentication authentication, @RequestBody @Valid CreateChildConversationRequest createConversationRequest, @PathVariable Long conversationId){
conversationService.createChildConversation(authentication.getName(), createConversationRequest, conversationId);
}

@Operation(summary = "(대댓글도 포함)대화 삭제하기", description = """
삭제할 대화 id를 파라미터로 전달해주세요.
""")
@DeleteMapping("/{conversationId}")
public void delete(@PathVariable Long conversationId) {
conversationService.delete(conversationId);
}

// 만들었는데 필요없는 api인것 같아서 일단 주석처리
// @Operation(summary = "대화 조회하기", description = """
// 조회할 대화 id를 파라미터로 전달해주세요.
// """)
// @GetMapping("/{conversationId}")
// public ResponseEntity<GetConversationResponse> get(Authentication authentication, @PathVariable Long conversationId) {
// return ResponseEntity.ok(conversationService.get(authentication.getName(), conversationId));
// }

@Operation(summary = "대화 좋아요 등록하기", description = """
사용자가 대화의 좋아요를 등록할 때 대화 id를 파라미터로 전달해주세요.
""")
@PostMapping("/like/{conversationId}")
public void increaseLike(Authentication authentication, @PathVariable Long conversationId) {
conversationService.increaseLike(authentication.getName(), conversationId);
}

@Operation(summary = "대화 좋아요 취소하기", description = """
사용자가 대화의 좋아요를 취소할 때 대화 id를 파라미터로 전달해주세요.
""")
@DeleteMapping("/like/{conversationId}")
public void decreaseLike(Authentication authentication, @PathVariable Long conversationId) {
conversationService.decreaseLike(authentication.getName(), conversationId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.numberone.backend.domain.conversation.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CreateChildConversationRequest {
@NotNull(message = "댓글 내용은 null일 수 없습니다.")
@Schema(defaultValue = "강남역 잠겼나요?")
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.numberone.backend.domain.conversation.dto.request;


import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CreateConversationRequest {
@NotNull(message = "댓글 내용은 null일 수 없습니다.")
@Schema(defaultValue = "강남역 잠겼나요?")
private String content;

@NotNull(message = "재난 상황 id는 null일 수 없습니다.")
@Schema(defaultValue = "2")
private Long disasterId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.numberone.backend.domain.conversation.dto.response;

import com.numberone.backend.domain.conversation.entity.Conversation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class GetConversationResponse {
@Schema(defaultValue = "67")
private Long conversationId;

@Schema(defaultValue = "생존전문가 ・ 5분전")
private String info;

@Schema(defaultValue = "강남역 잠겼나요?")
private String content;

@Schema(defaultValue = "2")
private Long like;

@Schema(defaultValue = "true")
private Boolean isLiked;

@Schema(defaultValue = "true")
private Boolean isEditable;

private List<GetConversationResponse> childs;

private static String makeInfo(String nickname, LocalDateTime createdAt) {
String info = nickname + " ・ ";
Duration duration = Duration.between(createdAt, LocalDateTime.now());
if (duration.toSeconds() < 60)
info += duration.toSeconds() + "초전";
else if (duration.toMinutes() < 60)
info += duration.toMinutes() + "분전";
else
info += duration.toHours() + "시간전";
return info;
}

public static GetConversationResponse of(Conversation conversation, Boolean isLiked, Boolean isEditable, List<GetConversationResponse> childs) {
return GetConversationResponse.builder()
.conversationId(conversation.getId())
.like(conversation.getLikeCnt())
.info(makeInfo(conversation.getMember().getNickName(), conversation.getCreatedAt()))
.content(conversation.getContent())
.isLiked(isLiked)
.isEditable(isEditable)
.childs(childs)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.numberone.backend.domain.conversation.entity;

import com.numberone.backend.config.basetime.BaseTimeEntity;
import com.numberone.backend.domain.disaster.entity.Disaster;
import com.numberone.backend.domain.like.entity.ConversationLike;
import com.numberone.backend.domain.member.entity.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Conversation extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Comment("내용")
private String content;

@Comment("댓글 depth {0: 댓글, 1: 대댓글}")
private Integer depth;

@Comment("좋아요 갯수")
private Long likeCnt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "disaster_id")
private Disaster disaster;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "conversation_id")
private Conversation parent;

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<Conversation> conversations = new ArrayList<>();

@OneToMany(mappedBy = "conversation", cascade = CascadeType.ALL)
private List<ConversationLike> likes = new ArrayList<>();

@Builder
public Conversation(String content, Long likeCnt, Integer depth, Member member, Disaster disaster, Conversation parent) {
this.content = content;
this.depth = depth;
this.member = member;
this.disaster = disaster;
this.parent = parent;
this.likeCnt = likeCnt;
}

public static Conversation of(String content, Member member, Disaster disaster) {
return Conversation.builder()
.content(content)
.member(member)
.disaster(disaster)
.depth(0)
.likeCnt(0L)
.build();
}

public static Conversation childOf(String content, Member member, Conversation parent) {
return Conversation.builder()
.content(content)
.member(member)
.parent(parent)
.depth(1)
.likeCnt(0L)
.build();
}

public void increaseLike() {
likeCnt++;
}

public void decreaseLike() {
likeCnt--;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.numberone.backend.domain.conversation.repository;


import com.numberone.backend.domain.conversation.entity.Conversation;
import com.numberone.backend.domain.disaster.entity.Disaster;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ConversationRepository extends JpaRepository<Conversation, Long> {

long countByDisaster(Disaster disaster);

long countByParent(Conversation parent);

List<Conversation> findAllByDisasterOrderByLikeCntDesc(Disaster disaster, Pageable pageable);

List<Conversation> findAllByDisasterOrderByLikeCntDesc(Disaster disaster);

List<Conversation> findAllByDisasterOrderByCreatedAtDesc(Disaster disaster);

List<Conversation> findAllByParentOrderByLikeCntDesc(Conversation parent);
}
Loading

0 comments on commit a7b140a

Please sign in to comment.