RestController Parcial

package com.trifulcas.SpringBootAPI.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.trifulcas.SpringBootAPI.model.Category;
import com.trifulcas.SpringBootAPI.repository.CategoryRepository;

@RestController
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	CategoryRepository categoryRepository;
	
	@GetMapping("")
	public List<Category> getAll() {
		return categoryRepository.findAll();
	}

	// Poner los valores en la URL, no parámetros nombrados
	@GetMapping("/{id}")
	public Category getById(@PathVariable int id) {
		System.out.println(id);
		return categoryRepository.findById(id).orElse(null);
	}
	
	@PostMapping("")
	public Category add(@RequestBody Category cat) {
		System.out.println(cat);
		if (categoryRepository.existsById(cat.getCategoryId())) {
			return null;
		}
		return categoryRepository.save(cat);
	}

	@PutMapping("/put")
	public String put() {
		return "put";
	}

	@PatchMapping("/patch")
	public String patch() {
		return "patch";
	}

	@DeleteMapping("/delete")
	public String delete() {
		return "delete";
	}
}

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos