Skip to content

Commit

Permalink
[�SKRB-159] feat, test: Club 생성 관련 dto 구체화 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
choi5798 authored Oct 26, 2023
2 parents a7e0db2 + 70098d8 commit 4e72fca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public class ClubController {

@PostMapping("/clubs")
public ResponseEntity<String> createClub(@RequestBody CreateClubRequest request) {
return ResponseEntity.created(URI.create("api/v1/clubs/1")).build();
Club newClub = request.toEntity();
Club createdClub = service.createClub(newClub);
Long id = createdClub.getId();

return ResponseEntity.created(URI.create("api/v1/clubs/" + id)).build();
}

@GetMapping("/clubs/{clubId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package com.spaceclub.club.controller.dto;

public record CreateClubRequest(String requiredInfo) {
import com.spaceclub.club.domain.Club;

public record CreateClubRequest(
String name,
String info,
String owner,
String image
) {

public Club toEntity() {
return Club.builder()
.name(name)
.info(info)
.owner(owner)
.image(image)
.build();
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/spaceclub/club/domain/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.Lob;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

Expand All @@ -17,6 +18,7 @@ public class Club extends BaseTimeEntity {

@Id
@Column(name = "club_id")
@Getter
@GeneratedValue
private Long id;

Expand All @@ -39,7 +41,7 @@ private boolean validateNameLength(String name) {
public Club(String name, String image, String info, String owner) {
Assert.notNull(name, "이름에 null 값이 올 수 없습니다");
Assert.hasText(name, "이름이 빈 값일 수 없습니다");
Assert.isTrue(validateNameLength(name), "이름의 길이는 12자를 넘을 수 없습니다");
Assert.isTrue(validateNameLength(name), "이름의 길이는 12글자를 넘을 수 없습니다");
Assert.isTrue(name.equals(name.trim()), "이름의 맨앞과 맨뒤에는 공백이 추가될 수 없습니다");

this.name = name;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/spaceclub/club/service/ClubService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.spaceclub.club.service;

import com.spaceclub.club.domain.Club;
import com.spaceclub.club.repository.ClubRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -10,4 +11,8 @@ public class ClubService {

private final ClubRepository repository;

public Club createClub(Club club) {
return repository.save(club);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spaceclub.club.controller.dto.CreateClubRequest;
import com.spaceclub.club.domain.Club;
import com.spaceclub.club.service.ClubService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -15,6 +16,8 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
Expand Down Expand Up @@ -46,7 +49,17 @@ class ClubControllerTest {
@WithMockUser
void createClubTest() throws Exception {
// given
CreateClubRequest request = new CreateClubRequest("requiredInfo");
given(clubService.createClub(any(Club.class))).willReturn(
Club.builder()
.name("연사모")
.info("연어를 사랑하는 모임")
.owner("연어대장")
.image("연어.png")
.build());
CreateClubRequest request = new CreateClubRequest("연사모",
"연어를 사랑하는 모임",
"연어대장",
"연어.png");

// when
ResultActions result = this.mockMvc.perform(post("/api/v1/clubs")
Expand All @@ -61,7 +74,10 @@ void createClubTest() throws Exception {
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
requestFields(
fieldWithPath("requiredInfo").type(JsonFieldType.STRING).description("클럽 생성시 필요한 임시 정보")
fieldWithPath("name").type(JsonFieldType.STRING).description("클럽 이름"),
fieldWithPath("info").type(JsonFieldType.STRING).description("클럽 소개"),
fieldWithPath("owner").type(JsonFieldType.STRING).description("클럽 생성자"),
fieldWithPath("image").type(JsonFieldType.STRING).description("클럽 썸네일 이미지")
)));
}

Expand Down

0 comments on commit 4e72fca

Please sign in to comment.