package com.hc.web.controller.browser.back;

import com.hc.business.model.NewsAttachment;
import com.hc.business.service.NewsAttachmentService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 新闻附件表(NewsAttachment)表控制层
 *
 * @author makejava
 * @since 2024-01-20 11:48:47
 */

@RestController
@RequestMapping("/new/api/back/newsAttachment")
public class BackNewsAttachmentController {

    /**
     * 服务对象
     */
    private final NewsAttachmentService newsAttachmentService;

    public BackNewsAttachmentController(NewsAttachmentService newsAttachmentService) {
        this.newsAttachmentService = newsAttachmentService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 NewsAttachment", response = Result.class)
    public Result selectDetail(@PathVariable("id") Integer id) {
        return Result.success(this.newsAttachmentService.queryById(id));
    }


    @PostMapping("/insert")
    @ApiOperation(value = "新增附件", response = Result.class)
    public Result addAttachment(@Valid @RequestBody List<NewsAttachment> newsAttachmentList) {
        newsAttachmentList.forEach(newsAttachmentService::insert);
        return Result.success(newsAttachmentList);
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改 NewsAttachment", response = Result.class)
    public Result updateNewsAttachment(@RequestBody NewsAttachment newsAttachment) {
        return Result.success(newsAttachmentService.update(newsAttachment));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 NewsAttachment", response = Result.class)
    public Result deleteNewsAttachment(@PathVariable("id") Integer id) {
        return Result.success(newsAttachmentService.deleteById(id));
    }

    @GetMapping("/queryByNewsId")
    @ApiOperation(value = "根据newID获取他的附件", response = Result.class)
    public Result queryByNewsId(@RequestParam(defaultValue = "0") Integer newsId) {
        return Result.success(newsAttachmentService.queryByNewsId(newsId));
    }


}
