Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions src/main/java/com/somemore/community/domain/CommunityBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.somemore.community.domain;

import com.somemore.global.common.BaseEntity;
import static lombok.AccessLevel.PROTECTED;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Table(name = "community_board")
public class CommunityBoard extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "writer_id", nullable = false, length = 16)
private UUID writerId;

@Column(name = "title", nullable = false)
private String title;

@Lob
@Column(name = "content", nullable = false)
private String content;

@Column(name = "img_url", nullable = false)
private String imgUrl;

@Builder
public CommunityBoard(UUID writerId, String title, String content, String imgUrl) {
this.writerId = writerId;
this.title = title;
this.content = content;
this.imgUrl = imgUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.somemore.community.dto.request;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.somemore.community.domain.CommunityBoard;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;

import java.util.UUID;

@JsonNaming(SnakeCaseStrategy.class)
@Builder
public record CommunityBoardCreateRequestDto(
@Schema(description = "커뮤니티 게시글 제목", example = "11/29 OO도서관 봉사 같이 갈 사람 모집합니다.")
@NotBlank(message = "게시글 제목은 필수 값입니다.")
String title,
@Schema(description = "커뮤니티 게시글 내용", example = "저 포함 5명이 같이 가면 좋을 거 같아요")
@NotBlank(message = "게시글 내용은 필수 값입니다.")
String content
) {
public CommunityBoard toEntity(UUID writerId, String imgUrl) {
return CommunityBoard.builder()
.writerId(writerId)
.title(title)
.content(content)
.imgUrl(imgUrl)
.build();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.somemore.community.repository;

import com.somemore.community.domain.CommunityBoard;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommunityBoardRepository extends JpaRepository<CommunityBoard, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.somemore.community.service.command;

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.command.CreateCommunityBoardUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@RequiredArgsConstructor
@Transactional
@Service
public class CreateCommunityBoardService implements CreateCommunityBoardUseCase {

private final CommunityBoardRepository communityBoardRepository;

@Override
public Long createCommunityBoard(CommunityBoardCreateRequestDto requestDto, UUID writerId, String imgUrl) {

CommunityBoard communityBoard = requestDto.toEntity(writerId, imgUrl == null ? "" : imgUrl);

communityBoardRepository.save(communityBoard);

return communityBoard.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.somemore.community.usecase.command;

import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;

import java.util.UUID;

public interface CreateCommunityBoardUseCase {
Long createCommunityBoard(
CommunityBoardCreateRequestDto requestDto,
UUID writerId,
String imgUrl);
}
29 changes: 0 additions & 29 deletions src/main/java/com/somemore/domains/CommunityBoard.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.somemore.community.service.command;

import static org.assertj.core.api.Assertions.assertThat;

import com.somemore.IntegrationTestSupport;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
import com.somemore.community.repository.CommunityBoardRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;
import java.util.UUID;

class CreateCommunityBoardServiceTest extends IntegrationTestSupport {
@Autowired
private CreateCommunityBoardService createCommunityBoardService;
@Autowired
private CommunityBoardRepository communityBoardRepository;

@AfterEach
void tearDown() {
communityBoardRepository.deleteAllInBatch();
}

@DisplayName("커뮤니티에 게시글을 등록한다.")
@Test
void createCommunityWithDto() {
//given
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

UUID writerId = UUID.randomUUID();
String imgUrl = "https://image.test.url/123";

//when
Long communityId = createCommunityBoardService.createCommunityBoard(dto, writerId, imgUrl);

//then
Optional<CommunityBoard> communityBoard = communityBoardRepository.findById(communityId);

assertThat(communityBoard).isPresent();
assertThat(communityBoard.get().getId()).isEqualTo(communityId);
assertThat(communityBoard.get().getWriterId()).isEqualTo(writerId);
assertThat(communityBoard.get().getTitle()).isEqualTo(dto.title());
assertThat(communityBoard.get().getContent()).isEqualTo(dto.content());
assertThat(communityBoard.get().getImgUrl()).isEqualTo(imgUrl);
}

@DisplayName("커뮤니티 게시글을 이미지 링크 없이 등록할 시 빈 문자열을 저장한다.")
@Test
void createCommunityWithoutImgUrl() {
//given
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

UUID writerId = UUID.randomUUID();
String imgUrl = null;

//when
Long communityId = createCommunityBoardService.createCommunityBoard(dto, writerId, imgUrl);

//then
Optional<CommunityBoard> communityBoard = communityBoardRepository.findById(communityId);

assertThat(communityBoard).isPresent();
assertThat(communityBoard.get().getId()).isEqualTo(communityId);
assertThat(communityBoard.get().getWriterId()).isEqualTo(writerId);
assertThat(communityBoard.get().getTitle()).isEqualTo(dto.title());
assertThat(communityBoard.get().getContent()).isEqualTo(dto.content());
assertThat(communityBoard.get().getImgUrl()).isEmpty();
}
}
Loading