Skip to content

Commit 1a5a11d

Browse files
authored
[FEAT] 태그 모음 보기 구현
2 parents 16d7242 + 8ba11c3 commit 1a5a11d

8 files changed

Lines changed: 106 additions & 6 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.FixLog.controller;
2+
3+
import com.example.FixLog.dto.Response;
4+
import com.example.FixLog.dto.tag.TagResponseDto;
5+
import com.example.FixLog.service.TagService;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@RestController
9+
@RequestMapping("/tags")
10+
public class TagController {
11+
private final TagService tagService;
12+
13+
public TagController(TagService tagService){
14+
this.tagService = tagService;
15+
}
16+
17+
@GetMapping
18+
public Response<Object> viewTags(@RequestParam("page") int page){
19+
TagResponseDto tags = tagService.viewTags(page);
20+
return Response.success("태그 모아보기 성공", tags);
21+
}
22+
}

src/main/java/com/example/FixLog/domain/tag/Tag.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ public class Tag {
2121
@Column(length = 20, nullable = false)
2222
private String tagName;
2323

24-
public static Tag of(TagCategory tagCategory, String tagName) {
24+
@Column(nullable = false)
25+
private String tagInfo;
26+
27+
public static Tag of(TagCategory tagCategory, String tagName, String tagInfo) {
2528
Tag tag = new Tag();
2629
tag.tagCategory = tagCategory;
2730
tag.tagName = tagName;
31+
tag.tagInfo = tagInfo;
2832
return tag;
2933
}
3034
}

src/main/java/com/example/FixLog/domain/tag/TagCategory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
@Getter
66
public enum TagCategory {
7+
BIG_CATEGORY("분류"),
78
MAJOR_CATEGORY("대분류"),
89
MIDDLE_CATEGORY("중분류"),
910
MINOR_CATEGORY("소분류");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.FixLog.dto.tag;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public class TagDto {
9+
private String tagName;
10+
private String tagInfo;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.FixLog.dto.tag;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
import java.util.List;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
public class TagResponseDto {
11+
private List<TagDto> tags;
12+
private int totalPages;
13+
}

src/main/java/com/example/FixLog/mock/TagTestDataInitializer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public class TagTestDataInitializer implements CommandLineRunner {
2121
@Override
2222
public void run(String... args) {
2323
if (tagRepository.count() == 0) {
24-
Tag tag1 = Tag.of(MAJOR_CATEGORY, "101");
25-
Tag tag2 = Tag.of(MAJOR_CATEGORY, "101");
26-
Tag tag3 = Tag.of(MIDDLE_CATEGORY, "201");
27-
Tag tag4 = Tag.of(MINOR_CATEGORY, "301");
28-
tagRepository.saveAll(List.of(tag1, tag2, tag3, tag4));
24+
Tag tag1 = Tag.of(BIG_CATEGORY, "backend", "백엔드 설명");
25+
Tag tag2 = Tag.of(MAJOR_CATEGORY, "springboot", "스프링부트 설명");
26+
Tag tag3 = Tag.of(MAJOR_CATEGORY, "django", "장고 설명");
27+
Tag tag4 = Tag.of(MIDDLE_CATEGORY, "java", "자바 설명");
28+
Tag tag5 = Tag.of(MINOR_CATEGORY, "404 not found", "404 에러 설명");
29+
tagRepository.saveAll(List.of(tag1, tag2, tag3, tag4, tag5));
2930
System.out.println("임시 태그 4개 삽입 완료");
3031
}
3132
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.example.FixLog.repository.tag;
22

33
import com.example.FixLog.domain.tag.Tag;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
46
import org.springframework.data.jpa.repository.JpaRepository;
57

68
public interface TagRepository extends JpaRepository<Tag, Long> {
9+
Page<Tag> findAll(Pageable pageable);
710
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.FixLog.service;
2+
3+
import com.example.FixLog.domain.member.Member;
4+
import com.example.FixLog.domain.tag.Tag;
5+
import com.example.FixLog.dto.UserIdDto;
6+
import com.example.FixLog.dto.tag.TagDto;
7+
import com.example.FixLog.dto.tag.TagResponseDto;
8+
import com.example.FixLog.exception.CustomException;
9+
import com.example.FixLog.exception.ErrorCode;
10+
import com.example.FixLog.repository.MemberRepository;
11+
import com.example.FixLog.repository.tag.TagRepository;
12+
import org.springframework.data.domain.Page;
13+
import org.springframework.data.domain.PageRequest;
14+
import org.springframework.data.domain.Pageable;
15+
import org.springframework.stereotype.Service;
16+
17+
import java.util.List;
18+
import java.util.stream.Collectors;
19+
20+
@Service
21+
public class TagService {
22+
private final TagRepository tagRepository;
23+
24+
public TagService(TagRepository tagRepository){
25+
this.tagRepository = tagRepository;
26+
}
27+
28+
// 태그 모음 보기
29+
public TagResponseDto viewTags(int page){
30+
Pageable pageable = PageRequest.of(page - 1, 12);
31+
32+
Page<Tag> tags = tagRepository.findAll(pageable);
33+
34+
List<TagDto> tagList = tags.stream()
35+
.map(tag -> new TagDto(
36+
tag.getTagName(),
37+
tag.getTagInfo()
38+
))
39+
.collect(Collectors.toList());
40+
41+
int totalPages = tags.getTotalPages();
42+
43+
return new TagResponseDto(tagList, totalPages);
44+
}
45+
}

0 commit comments

Comments
 (0)