package com.ecep.contract.ds.company.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.model.Company;

@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @RequestMapping("/findById")
    public Company findById(Integer id) {
        return companyService.findById(id);
    }

    @RequestMapping("/list")
    public Page<Company> list(
            Map<String, Object> params,
            @RequestParam(defaultValue = "0", name = "page") int pageNumber,
            @RequestParam(defaultValue = "10", name = "size") int pageSize) {
        Specification<Company> spec = null;
        Sort sort = Sort.by(Sort.Order.desc("id"));
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);
        return companyService.findAll(spec, pageable);
    }

    @RequestMapping("/save")
    public Company save(Company company) {
        return companyService.save(company);
    }

    @RequestMapping("/delete")
    public void delete(Integer id) {
        Company company = companyService.findById(id);
        companyService.delete(company);
    }
}