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

import com.hc.business.model.CourseAttachment;
import com.hc.business.service.CourseAttachmentService;
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;

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

@RestController
@RequestMapping("/new/api/back/courseAttachment")
public class BackCourseAttachmentController {

    /**
     * 服务对象
     */
    private final CourseAttachmentService courseAttachmentService;

    public BackCourseAttachmentController(CourseAttachmentService courseAttachmentService) {
        this.courseAttachmentService = courseAttachmentService;
    }

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

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

    @PutMapping("/update")
    @ApiOperation(value = "修改 CourseAttachment", response = Result.class)
    public Result updateCourseAttachment(@RequestBody CourseAttachment courseAttachment) {
        return Result.success(courseAttachmentService.update(courseAttachment));
    }

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

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


}
