package com.iwe3.langchain4j;

import com.iwe3.langchain4j.entity.ChatMessages;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@SpringBootTest
public class MongoCrudTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void delete(){
        var criteria = Criteria.where("_id").is("69420db67db051baab1b4873");
        var query = new Query(criteria);
        mongoTemplate.remove(query,ChatMessages.class);
    }

    @Test
    public void update(){
        //id不存在，则会自动变成：新增
        var criteria = Criteria.where("_id").is("69420db67db051baab1b4873");
        var query = new Query(criteria);
        //创建一个修改对象
        var update = new Update();
        update.set("content","奥特之母");
        //将查询条件， 修改的内容，文档的信息 -> mongodb
        mongoTemplate.upsert(query,update,ChatMessages.class);
    }


    @Test
    public void insert(){
//        mongoTemplate.insert(new ChatMessages(1L,"迪迦")); //程序员自己管理ID就用Long

        //利用数据库生成ID就用ObjectId
        var msg = new ChatMessages();
        msg.setContent("贝利亚");
        mongoTemplate.insert(msg);
    }

    @Test
    public void findById(){
        var msg = mongoTemplate.
                findById("69420db67db051baab1b4873",
                        ChatMessages.class);
        System.out.println(msg);
    }


}
