BAEL-4842 - Use React and Spring Boot to Build a Simple CRUD App

Initial source module for the article
This commit is contained in:
Sallo Szrajbman
2021-03-28 17:00:33 +01:00
parent d1d6cafac1
commit 6a82efdd39
30 changed files with 17577 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package com.baeldung.springbootreact;
import com.baeldung.springbootreact.domain.Client;
import com.baeldung.springbootreact.repository.ClientRepository;
import com.github.javafaker.Faker;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Locale;
@Component
public class BoostrapInitialData implements CommandLineRunner {
private final ClientRepository clientRepository;
private final Faker faker = new Faker(Locale.getDefault());
public BoostrapInitialData(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@Override
public void run(String... args) {
for (int i = 0; i < 10; i++) {
clientRepository.save(new Client(faker.name().fullName(), faker.internet().emailAddress()));
}
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.springbootreact;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootReactApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootReactApplication.class, args);
}
}

View File

@@ -0,0 +1,54 @@
package com.baeldung.springbootreact.controller;
import com.baeldung.springbootreact.domain.Client;
import com.baeldung.springbootreact.repository.ClientRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
@RestController
@RequestMapping("/clients")
public class ClientsController {
private final ClientRepository clientRepository;
public ClientsController(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@GetMapping
public List getClients() {
return clientRepository.findAll();
}
@GetMapping("/{id}")
public Client getClient(@PathVariable Long id) {
return clientRepository.findById(id).orElseThrow(RuntimeException::new);
}
@PostMapping
public ResponseEntity createClient(@RequestBody Client client) throws URISyntaxException {
Client savedClient = clientRepository.save(client);
return ResponseEntity.created(new URI("/clients/" + savedClient.getId())).body(savedClient);
}
@PutMapping("{id}")
public ResponseEntity updateClient(@PathVariable Long id, @RequestBody Client client) {
Client currentClient = clientRepository.findById(id).orElseThrow(RuntimeException::new);
currentClient.setName(client.getName());
currentClient.setEmail(client.getEmail());
currentClient = clientRepository.save(client);
return ResponseEntity.ok(currentClient);
}
@DeleteMapping("/{id}")
public ResponseEntity deleteClient(@PathVariable Long id) {
clientRepository.deleteById(id);
return ResponseEntity.ok().build();
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.springbootreact.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
public Client() {
}
public Client(String name, String email) {
this.name = name;
this.email = email;
}
public Client(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.springbootreact.repository;
import com.baeldung.springbootreact.domain.Client;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ClientRepository extends JpaRepository<Client, Long> {
}