package com.izouma.nineth.web;

import com.izouma.nineth.domain.AppVersion;
import com.izouma.nineth.domain.Asset;
import com.izouma.nineth.dto.AssetDTO;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AppVersionRepo;
import com.izouma.nineth.service.AppVersionService;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/appVersion")
@AllArgsConstructor
public class AppVersionController extends BaseController {
    private final AppVersionService appVersionService;

    private final AppVersionRepo appVersionRepo;


    /**
     * 新建/删除版本信息
     * @param appVersion 新加/删除版本信息
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public AppVersion save(@RequestBody AppVersion appVersion) {
        return appVersionService.save(appVersion);
    }

    /**
     * 查看全部版本
     * @param pageQuery 全部版本信息
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<AppVersion> all(@RequestBody PageQuery pageQuery){
        return appVersionService.all(pageQuery);
    }

    /**
     * 获取版本信息
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/get/{id}")
    public AppVersion get(@PathVariable Long id) {
        AppVersion appVersion = appVersionRepo.findById(id).orElseThrow(new BusinessException("版本无记录"));
        return appVersion;
    }

    /**
     * 删除指定版本信息
     * @param id 删除的id
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del")
    public void all(@RequestBody AppVersion appVersion){
        appVersionService.softDelete(appVersion.getId());
    }
}
