package com.ctsb.my_product_management_svc.category;

import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/categories")
@AllArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping("/find")
    public ResponseEntity<List<Category>> findAll(){
        List<Category> categories = categoryService.findAll();
        return ResponseEntity.ok(categories);
    }

    @GetMapping("/find/{id}")
    public ResponseEntity<Category>findById(@PathVariable Long id){
        Category category = categoryService.findById(id);
        return ResponseEntity.ok(category);
    }

    @PostMapping("/add")
    public ResponseEntity<Category> create(@RequestBody CategoryDto categoryDto){
        Category category = categoryService.create(categoryDto);
        return ResponseEntity.ok(category);
    }

    @PutMapping("/update/{id}")
    public ResponseEntity<Category> update(@PathVariable Long id, @RequestBody CategoryDto categoryDto){
        Category category = categoryService.update(id, categoryDto);
        return ResponseEntity.ok(category);
    }

    @DeleteMapping("/delete/{id}")
   public ResponseEntity<String> delete(@PathVariable Long id){
        categoryService.delete(id);
        return ResponseEntity.ok("Category deleted!");
    }
}
