package com.qiaoba.module.file.controller;

import com.qiaoba.api.file.entity.File;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.module.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/19 9:14
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Tag(name = "文件管理")
public class FileController {

    private final FileService fileService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public AjaxResult upload(MultipartFile file) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", fileService.upload(file));
        ajax.put("fileName", file.getOriginalFilename());
        return ajax;
    }

    @DeleteMapping
    @Operation(summary = "删除文件")
    public AjaxResult remove(@RequestBody File file) {
        return fileService.delete(file.getUrl()) ? AjaxResult.success("文件删除成功") : AjaxResult.error("文件删除失败");
    }
}
