-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProductController.java.html
More file actions
56 lines (45 loc) · 3.55 KB
/
ProductController.java.html
File metadata and controls
56 lines (45 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ProductController.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">products</a> > <a href="index.source.html" class="el_package">com.bootexample4.products.controller</a> > <span class="el_source">ProductController.java</span></div><h1>ProductController.java</h1><pre class="source lang-java linenums">package com.bootexample4.products.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.bootexample4.products.model.Product;
import com.bootexample4.products.repository.ProductRepository;
@RestController
@RequestMapping("/api/products")
<span class="fc" id="L14">public class ProductController {</span>
@Autowired
private ProductRepository productRepository;
@GetMapping
public List<Product> getAllProducts() {
<span class="fc" id="L21"> return productRepository.findAll();</span>
}
@PostMapping
public Product createProduct(@RequestBody Product product) {
<span class="fc" id="L26"> return productRepository.save(product);</span>
}
@GetMapping("/{id}")
public ResponseEntity<Product> getProductById(@PathVariable Long id) {
<span class="fc" id="L31"> return productRepository.findById(id)</span>
<span class="fc" id="L32"> .map(product -> ResponseEntity.ok().body(product))</span>
<span class="fc" id="L33"> .orElse(ResponseEntity.notFound().build());</span>
}
@PutMapping("/{id}")
public ResponseEntity<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
<span class="fc" id="L38"> return productRepository.findById(id).map(existingProduct -> {</span>
<span class="fc" id="L39"> existingProduct.setName(product.getName());</span>
<span class="fc" id="L40"> existingProduct.setDescription(product.getDescription());</span>
<span class="fc" id="L41"> existingProduct.setPrice(product.getPrice());</span>
<span class="fc" id="L42"> Product updatedProduct = productRepository.save(existingProduct);</span>
<span class="fc" id="L43"> return ResponseEntity.ok().body(updatedProduct);</span>
<span class="fc" id="L44"> }).orElse(ResponseEntity.notFound().build());</span>
}
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteProduct(@PathVariable Long id) {
<span class="fc" id="L49"> return productRepository.findById(id).map(product -> {</span>
<span class="fc" id="L50"> productRepository.delete(product);</span>
<span class="fc" id="L51"> return ResponseEntity.ok().build();</span>
<span class="fc" id="L52"> }).orElse(ResponseEntity.notFound().build());</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>