package cn.reghao.tnb.search.app.blog.service;

import cn.reghao.tnb.search.app.blog.db.repository.ArticleRepository;
import cn.reghao.tnb.search.app.blog.model.dto.QuestionUpdateDto;
import cn.reghao.tnb.search.app.blog.model.po.Article;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-06-12 21:15:15
 */
@Service
public class QuestionService {
    private final ArticleRepository articleRepository;

    public QuestionService(ArticleRepository articleRepository) {
        this.articleRepository = articleRepository;
    }

    public void updateQuestion(QuestionUpdateDto questionUpdateDto) {
        String questionId = questionUpdateDto.getQuestionId();
        Article question = articleRepository.findByArticleId(questionId);
        if (question != null) {
            question.setTitle(questionUpdateDto.getTitle());
            question.setContent(questionUpdateDto.getContent());
            articleRepository.save(question);
        }
    }

    public void addWeight(String questionId) {
        Article question = articleRepository.findByArticleId(questionId);
        if (question != null) {
            int weight = question.getWeight();
            question.setWeight(weight+1);
            articleRepository.save(question);
        }
    }

    public void minusWeight(String questionId) {
        Article question = articleRepository.findByArticleId(questionId);
        if (question != null) {
            int weight = question.getWeight();
            question.setWeight(weight-1);
            articleRepository.save(question);
        }
    }

    public void deleteQuestions(List<String> questionIds) {
        questionIds.forEach(questionId -> {
            Article question = articleRepository.findByArticleId(questionId);
            if (question != null) {
                articleRepository.delete(question);
            }
        });
    }
}
