Skip to content

Commit 587f815

Browse files
committed
Make the Spring Boot app a CRUD application
1 parent cef8ecf commit 587f815

File tree

13 files changed

+145
-26
lines changed

13 files changed

+145
-26
lines changed

Diff for: .dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
*.log

Diff for: build.gradle

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
plugins {
22
id 'java'
3-
id 'org.springframework.boot' version '3.3.4'
3+
id 'org.springframework.boot' version '3.3.5'
44
id 'io.spring.dependency-management' version '1.1.6'
55
}
66

7-
group = 'com.example'
7+
group = 'com.webenius'
88
version = '0.0.1-SNAPSHOT'
99

1010
java {
@@ -18,8 +18,10 @@ repositories {
1818
}
1919

2020
dependencies {
21+
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
2122
implementation 'org.springframework.boot:spring-boot-starter-web'
22-
implementation 'org.springframework.boot:spring-boot-starter-actuator'
23+
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
24+
runtimeOnly 'org.postgresql:postgresql'
2325
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2426
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
2527
}

Diff for: compose.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
postgres:
3+
image: 'postgres:latest'
4+
environment:
5+
- 'POSTGRES_DB=mydatabase'
6+
- 'POSTGRES_PASSWORD=password'
7+
- 'POSTGRES_USER=user'
8+
ports:
9+
- '5432:5432'

Diff for: src/main/java/com/example/springbootapp/HelloController.java

-13
This file was deleted.

Diff for: src/main/java/com/example/springbootapp/Application.java renamed to src/main/java/com/webenius/springbootapp/CrudAppApplication.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
package com.example.springboot;
2-
1+
package com.webenius.springbootapp;
32

43
import java.util.Arrays;
54

@@ -10,13 +9,13 @@
109
import org.springframework.context.annotation.Bean;
1110

1211
@SpringBootApplication
13-
public class Application {
12+
public class CrudAppApplication {
1413

1514
public static void main(String[] args) {
16-
SpringApplication.run(Application.class, args);
15+
SpringApplication.run(CrudAppApplication.class, args);
1716
}
1817

19-
@Bean
18+
/*@Bean
2019
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
2120
return args -> {
2221
@@ -29,6 +28,5 @@ public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
2928
}
3029
3130
};
32-
}
33-
31+
}*/
3432
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.webenius.springbootapp.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
import com.webenius.springbootapp.model.User;
6+
import com.webenius.springbootapp.repository.UserRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping("/api/users")
14+
public class UserController {
15+
16+
@Autowired
17+
private UserRepository userRepository;
18+
19+
@GetMapping
20+
public List<User> getAllUsers() {
21+
return userRepository.findAll();
22+
}
23+
24+
@PostMapping
25+
public User createUser(@RequestBody User user) {
26+
return userRepository.save(user);
27+
}
28+
29+
@GetMapping("/{id}")
30+
public User getUserById(@PathVariable Long id) {
31+
return userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found with id " + id));
32+
}
33+
34+
@PutMapping("/{id}")
35+
public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
36+
User user = userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found with id " + id));
37+
user.setName(userDetails.getName());
38+
user.setEmail(userDetails.getEmail());
39+
return userRepository.save(user);
40+
}
41+
42+
@DeleteMapping("/{id}")
43+
public String deleteUser(@PathVariable Long id) {
44+
userRepository.deleteById(id);
45+
return "User deleted with id " + id;
46+
}
47+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.webenius.springbootapp.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
8+
@Entity
9+
public class User {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
13+
private Long id;
14+
private String name;
15+
private String email;
16+
17+
public User() {
18+
}
19+
20+
public User(Long id, String name, String email) {
21+
this.id = id;
22+
this.name = name;
23+
this.email = email;
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getEmail() {
43+
return email;
44+
}
45+
46+
public void setEmail(String email) {
47+
this.email = email;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.webenius.springbootapp.repository;
2+
3+
import com.webenius.springbootapp.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface UserRepository extends JpaRepository<User, Long> {
7+
}

Diff for: src/main/resources/application.properties

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
spring.application.name=spring-boot-app
2+
spring.sql.init.mode=always
3+
spring.jpa.hibernate.ddl-auto=create

Diff for: src/main/resources/schema.sql

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DROP TABLE IF EXISTS User; -- don't do this on a production environment
2+
3+
CREATE TABLE User (
4+
id varchar(255) NOT NULL,
5+
name varchar(255) NOT NULL,
6+
email varchar(255) NOT NULL,
7+
PRIMARY KEY (id)
8+
);
9+
10+
INSERT INTO User
11+
(id, name, email)
12+
VALUES (1,'John Doe','[email protected]');
13+
INSERT INTO User
14+
(id, name, email)
15+
VALUES (2,'Jane Smith','[email protected]');

Diff for: src/test/java/com/example/springbootapp/HelloControllerITest.java renamed to src/test/java/com/webenius/springbootapp/HelloControllerITest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.springboot;
1+
package com.webenius.springbootapp;
22

33
import org.junit.jupiter.api.Test;
44

Diff for: src/test/java/com/example/springbootapp/HelloControllerTest.java renamed to src/test/java/com/webenius/springbootapp/HelloControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.springboot;
1+
package com.webenius.springbootapp;
22

33
import static org.hamcrest.Matchers.equalTo;
44
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

Diff for: src/test/java/com/example/springbootapp/SpringBootAppApplicationTests.java renamed to src/test/java/com/webenius/springbootapp/SpringBootAppApplicationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.springbootapp;
1+
package com.webenius.springbootapp;
22

33
import org.junit.jupiter.api.Test;
44
import org.springframework.boot.test.context.SpringBootTest;

0 commit comments

Comments
 (0)