-
Notifications
You must be signed in to change notification settings - Fork 0
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
[FEAT] CG-01 #9 #18
base: main
Are you sure you want to change the base?
[FEAT] CG-01 #9 #18
Changes from 5 commits
a7d740f
907d63f
3671295
89410c7
b0befc2
9a13351
a2b3a46
3a3c5ab
d140257
6179d8b
847938b
7101833
aa3a062
e25a12e
be2b042
ee69994
f6c8f58
9917087
a0c2421
3475986
d836978
2bf5902
d37c0a2
68b8fe6
bc6435d
5cff246
ff00f4e
47df405
42d8f0a
bc79e88
a1e7db5
bbf29c4
aabff3e
0d528df
30c3958
2e82d5f
f735063
5bc4324
9cf413c
8e646b8
54a5196
b0f2823
f518dfe
245cc92
40758cc
f1a5e27
93fc640
17d39ce
a2bdb63
d7cbceb
2a371b9
68c7b45
1b6e417
9446504
77abefb
2fe5efd
c305369
66115ff
ba72172
60427c9
1b95887
7d43e72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.backend.category.controller; | ||
|
||
import com.backend.category.dto.CategoryDto; | ||
import com.backend.category.service.CategoryService; | ||
import com.backend.global.response.GenericResponse; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/category") | ||
@RequiredArgsConstructor | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
// ์นดํ ๊ณ ๋ฆฌ ์ ์ฒด ์กฐํ | ||
@GetMapping | ||
public GenericResponse<List<CategoryDto>> getAllCategory() { | ||
List<CategoryDto> categorieList = categoryService.categoryList(); | ||
return GenericResponse.of(true, "200", categorieList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.backend.category.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CategoryDto { | ||
private Long id; | ||
private String name; | ||
private LocalDateTime createdAt; | ||
private LocalDateTime modifiedAt; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.backend.category.entity; | ||
|
||
import com.backend.global.baseentity.BaseEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.Size; | ||
import java.util.Objects; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Category extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
@Size(max = 25) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ ํจ์ฑ ๊ฒ์ฆํ ๋ ์ฐ์ด๋๊ฑฐ๋ผ ์ด๋ถ๋ถ ์ ๊ฑฐํด์ฃผ์๊ณ @column(nullable = false, length = 25) ์ด๋ ๊ฒ ์ค์ ํด์ฃผ์๋ฉด ๋ ๊ฑฐ ๊ฐ์ต๋๋ค. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ต ์๊ฒ ์ต๋๋ค ๊ฐ์ฌํฉ๋๋ค! |
||
private String name; | ||
|
||
// category ๊ฐ์ฒด์ ๊ฐ์ด ๋์ผํ์ง ๋น๊ตํ๋ ๋ฉ์๋ | ||
// ํ ์คํธ ์ค ์๋ฌ๊ฐ ๋ฐ์ํด์ ์ถ๊ฐํ ๋ฉ์๋ | ||
// ์์ ๊ฒฐ๊ณผ์ ์ค์ ๊ฒฐ๊ณผ๋ฅผ ๋น๊ตํ ๋ ์ฌ์ฉ | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Category category = (Category) o; | ||
return Objects.equals(id, category.id) && Objects.equals(name, category.name); | ||
} | ||
|
||
// equals() ๋ฉ์๋์ ํจ๊ป ์ฌ์ฉ๋๋ hashCode() ๋ฉ์๋ | ||
// ์ด ๋ฉ์๋์์ ๋น๊ตํ๋ ํ๋๋ค์ ๊ธฐ๋ฐ์ผ๋ก hashCode ๊ฐ์ ๊ณ์ฐ, ๊ฐ์ฒด ๋น๊ต์ ์ผ๊ด์ฑ์ ์ ์ง | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.backend.category.repository; | ||
|
||
import com.backend.category.entity.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.backend.category.service; | ||
|
||
import com.backend.category.dto.CategoryDto; | ||
import com.backend.category.entity.Category; | ||
import com.backend.category.repository.CategoryRepository; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CategoryService { | ||
|
||
private final CategoryRepository categoryRepository; | ||
|
||
// ์นดํ ๊ณ ๋ฆฌ ์ ์ฒด ์กฐํ | ||
public List<CategoryDto> categoryList() { | ||
List<Category> categoryList = categoryRepository.findAll(); | ||
return mappingCategory(categoryList); | ||
} | ||
|
||
// ์นดํ ๊ณ ๋ฆฌ ๋งคํ | ||
public List<CategoryDto> mappingCategory(List<Category> categoryList) { | ||
return categoryList.stream() | ||
.map(category -> CategoryDto.builder() | ||
.id(category.getId()) | ||
.name(category.getName()) | ||
.createdAt(category.getCreatedAt().toLocalDateTime()) | ||
.modifiedAt(category.getModifiedAt().toLocalDateTime()) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.backend; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.backend.category.dto.CategoryDto; | ||
import com.backend.category.entity.Category; | ||
import com.backend.category.repository.CategoryRepository; | ||
import com.backend.category.service.CategoryService; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
public class CategoryControllerTest { | ||
|
||
@Autowired | ||
private CategoryRepository categoryRepository; // Repository ์ฃผ์ | ||
|
||
@Autowired | ||
private CategoryService categoryService; | ||
|
||
@BeforeEach | ||
void setup() { | ||
// ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ ์ ์ฝ์ | ||
categoryRepository.save(Category.builder() | ||
.name("Category 1") | ||
.build()); | ||
|
||
categoryRepository.save(Category.builder() | ||
.name("Category 2") | ||
.build()); | ||
} | ||
|
||
@Test | ||
void testCategoryList_shouldReturnCategoryDtos() { | ||
// ์นดํ ๊ณ ๋ฆฌ ์ ์ฒด ์กฐํ (DTO ๋ฐํ) | ||
List<CategoryDto> categoryDtos = categoryService.categoryList(); | ||
|
||
// ์นดํ ๊ณ ๋ฆฌ ์ ์ฒด ์กฐํ ๊ฒฐ๊ณผ๊ฐ 2๊ฐ์ธ์ง ํ์ธ | ||
assertThat(categoryDtos.size()).isEqualTo(2); | ||
|
||
// ์ฒซ ๋ฒ์งธ ์นดํ ๊ณ ๋ฆฌ DTO์ name์ด "Category 1"์ธ์ง ํ์ธ | ||
assertThat(categoryDtos.get(0).getName()).isEqualTo("Category 1"); | ||
|
||
// ๋ ๋ฒ์งธ ์นดํ ๊ณ ๋ฆฌ DTO์ name์ด "Category 2"์ธ์ง ํ์ธ | ||
assertThat(categoryDtos.get(1).getName()).isEqualTo("Category 2"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๊น ํ์ธ์ ๋ชปํ๋ ๋ถ๋ถ์ธ๋ฐ Response์ฉ ๊ฐ์ฒด๋ ๊ฐ์ฒด๋ช ์ ๋๋ฉ์ธ + Response๋ก ํต์ผ ์์ผ์ฃผ์๋ฉด ๋ฉ๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต ์์ ํด์ ์ฌ๋ฆฌ๊ฒ ์ต๋๋ค!