본문 바로가기
프로젝트/네이버 api를 이용한 검색사이트

네이버 api를 이용한 검색 사이트 (2)

by lroot 2022. 10. 6.
728x90
반응형

1) Db 구축

 

- MemoryDbRepositoryIfs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.search.db;
 
import java.util.List;
import java.util.Optional;
 
public interface MemoryDbRepositoryIfs<T> {
 
    Optional<T> findById(int index);
    T save(T entity);
    void deleteById(int index);
    List<T> listAll();
}
 
cs

 

- MemoryDbEntity

 

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.search.db;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
@NoArgsConstructor
@AllArgsConstructor
@Data
public class MemoryDbEntity {
    protected int index;
}
 
cs

 

- MemoryDbRepositoryAbstract

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.search.db;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
public class MemoryDbRepositoryAbstract<extends MemoryDbEntity> implements MemoryDbRepositoryIfs<T>{
 
    private final List<T> db = new ArrayList<>();
    private int index = 0;
 
    @Override
    public Optional<T> findById(int index){
        return db.stream().filter(it->it.getIndex() == index).findFirst();
    }
 
    @Override
    public T save(T entity){
        var optionalEntity = db.stream().filter(it->it.getIndex() == entity.getIndex()).findFirst();
 
        if(optionalEntity.isEmpty()){
            index++;
            entity.setIndex(index);
        }else{
            var preIndex = optionalEntity.get().getIndex();
            entity.setIndex(preIndex);
 
            deleteById(preIndex);
        }
        db.add(entity);
        return entity;
    }
 
    @Override
    public void deleteById(int index){
        var optionalEntity = db.stream().filter(it -> it.getIndex() == index).findFirst();
 
        if(optionalEntity.isPresent()){
            db.remove(optionalEntity.get());
        }
    }
 
    @Override
    public List<T> listAll(){
        return db;
    }
 
}
 
cs

- WishListEntity

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.search.wishlist.entity;
 
import com.example.search.db.MemoryDbEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.time.LocalDateTime;
 
@NoArgsConstructor
@AllArgsConstructor
@Data
public class WishListEntity extends MemoryDbEntity {
    private String title;               // 장소명
    private String category;            // 카테고리
    private String address;             // 주소
    private String readAddress;         // 도로명
    private String homePageLink;        // 홈페이지 주소
    private String imageLink;           // 이미지 주소
    private boolean isVisit;            // 방문 여부
    private int visitCount;             // 방문 횟수
    private LocalDateTime lastVisitDate;// 마지막 방문 일자
}
 
cs

 

- WishListRepository

 

1
2
3
4
5
6
7
8
9
10
package com.example.search.wishlist.repository;
 
import com.example.search.db.MemoryDbRepositoryAbstract;
import com.example.search.wishlist.entity.WishListEntity;
import org.springframework.stereotype.Repository;
 
@Repository // Db를 저장하는 곳
public class WishListRepository extends MemoryDbRepositoryAbstract<WishListEntity> {
}
 
cs

 

2) Test

 

- WishListRepositoryTest

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.example.search.wishlist.repository;
 
import com.example.search.wishlist.entity.WishListEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest
public class WishListRepositoryTest {
 
    @Autowired
    private WishListRepository wishListRepository;
 
    private WishListEntity create() {
        var wishList = new WishListEntity();
 
        wishList.setTitle("title");
        wishList.setCategory("category");
        wishList.setAddress("address");
        wishList.setReadAddress("readAddress");
        wishList.setHomePageLink("");
        wishList.setImageLink("");
        wishList.setVisit(false);
        wishList.setVisitCount(0);
        wishList.setLastVisitDate(null);
 
        return wishList;
    }
 
    @Test
    public void saveTest(){
        var wishListEntity = create();
        var expected = wishListRepository.save(wishListEntity);
 
        Assertions.assertNotNull(expected);
        Assertions.assertEquals(1, expected.getIndex());
    }
 
    @Test
    public void updateTest() {
        var wishListEntity = create();
        var expected = wishListRepository.save(wishListEntity);
 
        expected.setTitle("update test");
        var saveEntity = wishListRepository.save(expected);
 
        Assertions.assertEquals("update test", saveEntity.getTitle());
        Assertions.assertEquals(1, wishListRepository.listAll().size());
    }
 
    @Test
    public void findByIdTest(){
        var wishListEntity = create();
        wishListRepository.save(wishListEntity);
 
        wishListRepository.deleteById(1);
 
        int count = wishListRepository.listAll().size();
 
        Assertions.assertEquals(0,count);
    }
 
    @Test
    public void listAllTest(){
        var wishListEntity1 = create();
        wishListRepository.save(wishListEntity1);
 
        var wishListEntity2 = create();
        wishListRepository.save(wishListEntity2);
 
        int count = wishListRepository.listAll().size();
        Assertions.assertEquals(2, count);
    }
}
 
cs

 

댓글