package com.example.crm.controller;
import com.example.crm.model.Customer;
import com.example.crm.service.CustomerService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/customers")
public class CustomerRestController {
private final CustomerService customerService;
public CustomerRestController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping
public List<Customer> getAll() {
return customerService.findAll();
}
@GetMapping("/{id}")
public Customer getById(@PathVariable Long id) {
return customerService.findById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Customer create(@Valid @RequestBody Customer customer) {
return customerService.create(customer);
}
@PutMapping("/{id}")
public Customer update(@PathVariable Long id, @Valid @RequestBody Customer customer) {
return customerService.update(id, customer);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
customerService.delete(id);
}
}
package com.example.crm.controller;
import com.example.crm.model.Customer;
import com.example.crm.service.CustomerService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/customers")
public class CustomerRestController {
private final CustomerService customerService;
public CustomerRestController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping
public List<Customer> getAll() {
return customerService.findAll();
}
@GetMapping("/{id}")
public Customer getById(@PathVariable Long id) {
return customerService.findById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Customer create(@Valid @RequestBody Customer customer) {
return customerService.create(customer);
}
@PutMapping("/{id}")
public Customer update(@PathVariable Long id, @Valid @RequestBody Customer customer) {
return customerService.update(id, customer);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
customerService.delete(id);
}
}