package com.itsol.quantrivanphong.access.homepage.controller;

import com.itsol.quantrivanphong.access.homepage.business.CatalogiBusiness;
import com.itsol.quantrivanphong.exception.ResourceNotFoundException;
import com.itsol.quantrivanphong.model.Catalogi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CatalogiController {
    @Autowired
    private CatalogiBusiness catalogiBusiness;

    @GetMapping("/catalogies")
    public ResponseEntity<List<Catalogi>> getAllNews(){
        return ResponseEntity.ok( catalogiBusiness.findAllCatalogi());
    }

    @GetMapping("/catalogies/{catalogiId}")
    public Catalogi getCatalogiById(@PathVariable(value = "catalogiId") int catalogiId){
        Catalogi catalogi =  catalogiBusiness.findCatalogiById(catalogiId);
        if(catalogi == null) {
          throw new ResourceNotFoundException("Catalogi", "catalogiId", catalogiId);
        }
        return catalogi;
    }

    @PostMapping("/catalogi")
    public Catalogi createCatalogi(@Valid@RequestBody Catalogi catalogi) {
        return catalogiBusiness.save(catalogi);
    }

}
