Package : db
Interface : MemoryDbRepositoryIfs
Abstract : MemoryDbRepositoryAbstract
Class : MemoryDbEntity
db / MemoryDbRepositoryIfs.interface
package com.example.restaurant.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();
}
package com.example.restaurant.db;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class MemoryDbEntity {
protected int index;
}
package com.example.restaurant.db;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public abstract class MemoryDbRepositoryAbstract<T extends MemoryDbEntity> implements MemoryDbRepositoryIfs<T>{
private final List<T> db = new ArrayList<>();
private int index = 0;
@Override
public Optional<T> findById(int index) {
// filter는 db가 갖고 있는 T에 대한 부분으로
// 여기서 getIndex는 T가 상속받은 MemoryDbEntity의 get 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()) { // db에 데이터가 없는 경우
index++;
entity.setIndex(index);
}else {// db에 이미 데이터가 있는 경우
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;
}
}
package com.example.restaurant.wishlist.entity;
import com.example.restaurant.db.MemoryDbEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class WishListEntity extends MemoryDbEntity { // DB에 저장할 내용
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; // 마지막 방문 일자
}
package com.example.restaurant.wishlist.repository;
import com.example.restaurant.db.MemoryDbRepositoryAbstract;
import com.example.restaurant.wishlist.entity.WishListEntity;
import org.springframework.stereotype.Repository;
@Repository // DB를 저장하는 곳이다라고 지정
public class WishListRepository extends MemoryDbRepositoryAbstract<WishListEntity> {
}
package com.example.restaurant.wishlist.repository;
import com.example.restaurant.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);
var expected = wishListRepository.findById(1);
Assertions.assertEquals(true, expected.isPresent());
Assertions.assertEquals(1, expected.get().getIndex());
}
@Test
public void deleteTest() {
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);
}
}