Skip to content

Commit f589310

Browse files
committed
Add Update
1 parent 76d5c46 commit f589310

14 files changed

Lines changed: 579 additions & 0 deletions

File tree

spring-boot-update-example/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Krzysztof Chruściel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# spring-boot-update-example
2+
Spring Boot example of POST, PUT and PATCH methods.

spring-boot-update-example/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>pl.codecouple.update</groupId>
7+
<artifactId>update-example</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>update-example</name>
12+
<description>Example of POST, PUT and PATCH method</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
<version>1.4.192</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.projectlombok</groupId>
54+
<artifactId>lombok</artifactId>
55+
<version>1.16.12</version>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
69+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pl.codecouple.update;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UpdateExampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(UpdateExampleApplication.class, args);
11+
}
12+
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package pl.codecouple.update.todo;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
import org.springframework.util.Assert;
6+
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
11+
/**
12+
* Created by Krzysztof Chruściel.
13+
*/
14+
@Data
15+
@NoArgsConstructor
16+
@Entity
17+
public class Todo {
18+
19+
@Id
20+
@GeneratedValue
21+
private Long id;
22+
23+
private String title;
24+
25+
private String description;
26+
27+
public Todo(String title, String description) {
28+
Assert.notNull(title, "Title cannot be null!");
29+
Assert.hasLength(title, "Title cannot be empty!");
30+
Assert.notNull(description, "Description cannot be null!");
31+
Assert.hasLength(description, "Description cannot be empty!");
32+
this.title = title;
33+
this.description = description;
34+
}
35+
36+
37+
public Todo(Long id, String title, String description) {
38+
Assert.notNull(title, "Title cannot be null!");
39+
Assert.hasLength(title, "Title cannot be empty!");
40+
Assert.notNull(description, "Description cannot be null!");
41+
Assert.hasLength(description, "Description cannot be empty!");
42+
Assert.notNull(id, "ID cannot be null!");
43+
this.id = id;
44+
this.title = title;
45+
this.description = description;
46+
}
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package pl.codecouple.update.todo;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
import java.net.URI;
8+
import java.util.Map;
9+
10+
/**
11+
* Created by Krzysztof Chruściel.
12+
*/
13+
@RestController
14+
@RequestMapping("/todos")
15+
public class TodoController {
16+
17+
private final TodoService todoService;
18+
19+
public TodoController(TodoService todoService) {
20+
this.todoService = todoService;
21+
}
22+
23+
@PostMapping
24+
@ResponseStatus(value = HttpStatus.CREATED, reason = "Todo created!")
25+
public void addNewTodo(@RequestBody Todo todo){
26+
todoService.addNewTodo(todo);
27+
}
28+
29+
@PutMapping("/{id}")
30+
public ResponseEntity<Void> updateTodo(@RequestBody Todo todo, @PathVariable Long id) throws TodoNotFound {
31+
Todo todoById = todoService.getTodoById(id);
32+
if(todoById == null){
33+
todoService.addNewTodo(todo);
34+
return ResponseEntity.created(URI.create(String.format("/todos/%d", id))).build();
35+
}
36+
todoService.update(todo);
37+
return ResponseEntity.noContent().build();
38+
}
39+
40+
@PatchMapping("/{id}")
41+
@ResponseStatus(value = HttpStatus.NO_CONTENT, reason = "Todo partial updated!")
42+
public void updateTodo(@RequestBody Map<String, Object> updates, @PathVariable Long id) throws TodoNotFound {
43+
Todo todo = todoService.getTodoById(id);
44+
if (todo == null) {
45+
throw new TodoNotFound(String.format("Todo with ID %d not exists!", id));
46+
}
47+
partialUpdate(todo, updates);
48+
}
49+
50+
private void partialUpdate(Todo todo, Map<String, Object> updates) {
51+
if (updates.containsKey("title")) {
52+
todo.setTitle((String) updates.get("title"));
53+
}
54+
if (updates.containsKey("description")) {
55+
todo.setDescription((String) updates.get("description"));
56+
}
57+
todoService.partialUpdated(todo);
58+
}
59+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.codecouple.update.todo;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
/**
7+
* Created by Krzysztof Chruściel.
8+
*/
9+
@ResponseStatus(HttpStatus.NOT_FOUND)
10+
public class TodoNotFound extends Exception {
11+
12+
public TodoNotFound() {
13+
super("Todo not found!");
14+
}
15+
16+
public TodoNotFound(String message) {
17+
super(message);
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pl.codecouple.update.todo;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
/**
7+
* Created by Krzysztof Chruściel.
8+
*/
9+
@Repository
10+
public interface TodoRepository extends JpaRepository<Todo, Long>{
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pl.codecouple.update.todo;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
/**
6+
* Created by Krzysztof Chruściel.
7+
*/
8+
@Service
9+
public class TodoService {
10+
11+
12+
private final TodoRepository todoRepository;
13+
14+
public TodoService(TodoRepository todoRepository) {
15+
this.todoRepository = todoRepository;
16+
}
17+
18+
19+
public void addNewTodo(Todo todo) {
20+
todoRepository.save(todo);
21+
}
22+
23+
public void update(Todo todo) {
24+
todoRepository.save(todo);
25+
}
26+
27+
public Todo getTodoById(Long id){
28+
return todoRepository.findOne(id);
29+
}
30+
31+
public void partialUpdated(Todo todo) {
32+
todoRepository.save(todo);
33+
}
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#SERVER
2+
server.port=8081

0 commit comments

Comments
 (0)