Skip to content

Commit

Permalink
feat: create controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Edith Soares committed May 15, 2021
1 parent b58d6e9 commit 1d930ec
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/main/java/spg/finalchallenge/controller/ClientController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package spg.finalchallenge.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import spg.finalchallenge.entity.Client;
import spg.finalchallenge.service.ClientService;

import java.util.Optional;

@RestController
@RequestMapping( "/api")
public class ClientController {

@Autowired
private ClientService clientService;


@PostMapping("/client")
public ResponseEntity<Client> addClient(@RequestBody Client client){
try{
return new ResponseEntity<>(clientService.add(client), HttpStatus.CREATED);
}catch (Exception e){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/client/{id}")
public ResponseEntity<Client> getId(@PathVariable(value = "id") long id) {
try{
return new ResponseEntity<>(clientService.getId(id), HttpStatus.OK);
}catch (Exception e){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@PutMapping("/client/{id}")
public ResponseEntity<Client> update(@RequestBody Client client, @PathVariable(value = "id") long id) {
try{
Optional<Client> clientResult = clientService.findById(id);
if(clientResult.isPresent()){
client.setId(clientResult.get().getId());
return new ResponseEntity<>(clientService.update(client), HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}catch (Exception e){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

@GetMapping("/client/cout")
public long qtdeClient() {
return clientService.count();
}
}
37 changes: 37 additions & 0 deletions src/main/java/spg/finalchallenge/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spg.finalchallenge.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spg.finalchallenge.entity.Product;
import spg.finalchallenge.service.ProductService;

import java.util.Optional;

@RestController
@RequestMapping("/api")
public class ProductController {

@Autowired
private ProductService productService;

@PostMapping("/product")
public Product addProduto(@RequestBody Product product) {
return productService.add(product);
}

@GetMapping("/product/{id}")
public Product getId(@PathVariable(value = "id") long id) {
return productService.getId(id);
}

@PutMapping("/produto/{id}")
public Product updateProduto(@RequestBody Product product, @PathVariable(value = "id") long id) {
Optional<Product> productResult = productService.findById(id);
if(productResult.isPresent()){
product.setId(productResult.get().getId());
return productService.update(product);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spg.finalchallenge.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spg.finalchallenge.entity.Wishlist;
import spg.finalchallenge.service.WishlistService;

import java.util.Optional;

@RestController
@RequestMapping("/api")
public class WishlistController {

@Autowired
private WishlistService wishlistService;

@PostMapping("/wishlist")
public Wishlist addWish(@RequestBody Wishlist wishlist) {
return wishlistService.add(wishlist);
}

@GetMapping("/wishlist/{id}")
public Wishlist getId(@PathVariable(value = "id") long id) {
return wishlistService.getId(id);
}

@PutMapping("/wishlist/{id}")
public Wishlist update(@RequestBody Wishlist wishlist, @PathVariable(value = "id") long id) {
Optional<Wishlist> wishlistResult = wishlistService.findById(id);
if(wishlistResult.isPresent()){
wishlist.setId(wishlistResult.get().getId());
return wishlistService.update(wishlist);
}
return null;
}
}

0 comments on commit 1d930ec

Please sign in to comment.