Skip to content

Commit c1ffe83

Browse files
authored
Merge pull request #5 from CarToi/feat/usecase
[Feat/usecase] 데이터 수집 전처리 부수 요소 완료
2 parents 733b150 + 0a32deb commit c1ffe83

35 files changed

+556
-178
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ dependencies {
3535
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3636
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
3737

38-
// H2 DB & JPA
38+
// MySQL, H2 DB & JPA
39+
runtimeOnly 'com.mysql:mysql-connector-j'
3940
runtimeOnly 'com.h2database:h2'
4041
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
4142

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.jun.saemangeum.global.domain;
2+
3+
/**
4+
* 구분용 합성 코드 : 지명 + 분야(카테고리) + 수집 방법(크롤링: CR, API: API)
5+
*/
6+
public enum CollectSource {
7+
GJCUAP, // 김제 문하시설 API
8+
GSCUAP, // 군산 문화시설 API
9+
SMGEVAP, // 새만금 행사 API
10+
SMGFEAP, // 새만금 축제 API
11+
ARTOCR, // 새만금 군도 관광지 크롤링
12+
GJTOCR, // 김제 관광지 크롤링
13+
GSTOCR, // 군산 관광지 크롤링
14+
BATOCR, // 부안 관광지 크롤링
15+
GSFECR, // 군산 축제 크롤링
16+
SWTOCR, // 새만금 방조제 관광지 크롤링
17+
}

src/main/java/org/jun/saemangeum/global/domain/Content.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import lombok.NoArgsConstructor;
88
import org.jun.saemangeum.process.application.dto.RefinedDataDTO;
99

10-
// H2의 엔티티로 쓰이게 될 클래스
1110
@Entity
1211
@Builder
1312
@Table(name = "contents")
@@ -36,9 +35,16 @@ public class Content {
3635
private String url;
3736

3837
@Lob // MySQL 등에서는 TEXT 등으로
39-
@Column
38+
@Column(columnDefinition = "TEXT")
4039
private String introduction;
4140

41+
@Column
42+
private CollectSource collectSource;
43+
44+
@OneToOne(mappedBy = "content", cascade = CascadeType.REMOVE, orphanRemoval = true)
45+
// @JoinColumn(name = "vector_id") // 연관관계 주인이 벡터인데 굳이 얜 필요없겠네
46+
private Vector vector;
47+
4248
public static Content create(RefinedDataDTO dto) {
4349
return Content.builder()
4450
.title(dto.title())
@@ -47,6 +53,14 @@ public static Content create(RefinedDataDTO dto) {
4753
.image(dto.image())
4854
.introduction(dto.introduction())
4955
.url(dto.url())
56+
.collectSource(dto.collectSource())
5057
.build();
5158
}
59+
60+
public void setVector(Vector vector) {
61+
this.vector = vector;
62+
if (vector.getContent() != null) {
63+
vector.setContent(this);
64+
}
65+
}
5266
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jun.saemangeum.global.domain;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
@Entity
10+
@Builder
11+
@Table(name = "data_counts")
12+
@Getter
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
public class Count {
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
20+
@Column(nullable = false, name = "collect_source")
21+
private CollectSource collectSource;
22+
23+
@Column
24+
private int count;
25+
26+
public static Count of(CollectSource collectSource, int count) {
27+
return Count.builder().collectSource(collectSource).count(count).build();
28+
}
29+
30+
public void update(int count) {
31+
this.count = count;
32+
}
33+
}

src/main/java/org/jun/saemangeum/global/domain/Vector.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package org.jun.saemangeum.global.domain;
22

33
import jakarta.persistence.*;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Builder;
6-
import lombok.Getter;
7-
import lombok.NoArgsConstructor;
4+
import lombok.*;
85

96
@Entity
107
@Builder
118
@Table(name = "vectors")
129
@Getter
10+
@Setter
1311
@NoArgsConstructor
1412
@AllArgsConstructor
1513
public class Vector {
@@ -18,7 +16,7 @@ public class Vector {
1816
private Long id;
1917

2018
@Lob
21-
@Column
19+
@Column(columnDefinition = "BLOB")
2220
private byte[] vector;
2321

2422
@OneToOne
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.jun.saemangeum.global.repository;
22

3-
import org.jun.saemangeum.global.domain.Category;
43
import org.jun.saemangeum.global.domain.Content;
4+
import org.jun.saemangeum.global.domain.CollectSource;
55
import org.springframework.data.jpa.repository.JpaRepository;
66
import org.springframework.stereotype.Repository;
77

88
import java.util.List;
99

1010
@Repository
1111
public interface ContentRepository extends JpaRepository<Content, Long> {
12+
List<Content> findByCollectSource(CollectSource collectSource);
13+
void deleteByCollectSource(CollectSource collectSource);
14+
int countByCollectSource(CollectSource collectSource);
1215
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.jun.saemangeum.global.repository;
2+
3+
import org.jun.saemangeum.global.domain.CollectSource;
4+
import org.jun.saemangeum.global.domain.Count;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.util.Optional;
9+
10+
@Repository
11+
public interface CountRepository extends JpaRepository<Count, Long> {
12+
Optional<Count> findByCollectSource(CollectSource collectSource);
13+
boolean existsByCollectSource(CollectSource collectSource);
14+
}

src/main/java/org/jun/saemangeum/global/service/ContentService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.RequiredArgsConstructor;
44
import org.jun.saemangeum.global.domain.Content;
55
import org.jun.saemangeum.global.repository.ContentRepository;
6+
import org.jun.saemangeum.global.domain.CollectSource;
67
import org.springframework.stereotype.Service;
78
import org.springframework.transaction.annotation.Transactional;
89

@@ -19,6 +20,16 @@ public void saveContents(List<Content> contents) {
1920
contentRepository.saveAll(contents);
2021
}
2122

23+
@Transactional
24+
public void deleteByCollectSource(CollectSource collectSource) {
25+
contentRepository.deleteByCollectSource(collectSource);
26+
}
27+
28+
@Transactional
29+
public void deleteAll() {
30+
contentRepository.deleteAll();
31+
}
32+
2233
@Transactional(readOnly = true)
2334
public List<Content> getContents() {
2435
return contentRepository.findAll();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jun.saemangeum.global.service;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.jun.saemangeum.global.domain.CollectSource;
5+
import org.jun.saemangeum.global.domain.Count;
6+
import org.jun.saemangeum.global.repository.CountRepository;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Service
11+
@RequiredArgsConstructor
12+
public class CountService {
13+
14+
private final CountRepository countRepository;
15+
16+
// 트랜잭션 read only 해버리면
17+
// 이 트랜잭션 내부에서 읽기만 할 거니까, 쓰기 관련 영속성 관리도 하지 말고, 성능 최적화 해줘 하는 셈
18+
@Transactional
19+
public Count findByCollectSource(CollectSource collectSource) {
20+
return countRepository
21+
.findByCollectSource(collectSource)
22+
.orElseThrow(() -> new IllegalArgumentException("잘못된 데이터 소스 식별 코드!"));
23+
}
24+
25+
@Transactional
26+
public void deleteAll() {
27+
countRepository.deleteAll();
28+
}
29+
}

src/main/java/org/jun/saemangeum/global/service/VectorService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public void saveVector(Vector vector) {
2424
vectorRepository.save(vector);
2525
}
2626

27+
@Transactional
28+
public void deleteAll() {
29+
vectorRepository.deleteAll();
30+
}
31+
2732
@Transactional(readOnly = true)
2833
public List<Vector> getVectors() {
2934
return vectorRepository.findAll();

0 commit comments

Comments
 (0)