package com.izouma.jmrh.web;

import com.izouma.jmrh.repo.CollectRepo;
import com.izouma.jmrh.service.CollectService;
import com.izouma.jmrh.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/collect")
@AllArgsConstructor
public class CollectController extends BaseController {
    private CollectService collectService;
    private CollectRepo collectRepo;

    @GetMapping("/get/{id}")
    public boolean get(@PathVariable Long id) {
        return collectRepo.findByUserIdAndSndId(SecurityUtils.getAuthenticatedUser().getId(), id) != null;
    }

    @GetMapping("/{id}")
    public void collectSnd(@PathVariable("id") Long sndId) {
        collectService.collectSnd(SecurityUtils.getAuthenticatedUser().getId(), sndId);
    }

    @PostMapping("/del")
    public void del(@RequestBody List<Long> ids) {
        if (!ids.isEmpty()) {
            collectRepo.deleteAllByIdIn(ids);
        }
    }

    @PostMapping("/remove")
    public void remove(@RequestParam List<Long> sndIds) {
        if (!sndIds.isEmpty()) {
            collectRepo.deleteAllByUserIdAndSndIdIn(SecurityUtils.getAuthenticatedUser().getId(), sndIds);
        }
    }
}

