package com.ruoyi.novel.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.novel.domain.ChapterDTO;
import com.ruoyi.novel.domain.NovelChapter;
import com.ruoyi.novel.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// ChapterController.java
@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;
    @GetMapping("/list/{novelId}")
    public AjaxResult listChapters(@PathVariable Long novelId) {
        List<NovelChapter> chapters = chapterService.selectChapterListByNovelId(novelId);
        return AjaxResult.success(chapters);
    }
    @GetMapping("/{novelId}")
    public AjaxResult getChapters(@PathVariable Long novelId) {
        List<NovelChapter> chapters = chapterService.selectChapterListByNovelId(novelId);
        return AjaxResult.success(chapters);
    }

    @PostMapping("/save")
    public AjaxResult saveChapter(@RequestBody ChapterDTO dto) {
        return toAjax(chapterService.saveChapter(dto.getChapter(), dto.getContent()));
    }
    private AjaxResult toAjax(int result) {
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}