-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_5012023.java
More file actions
101 lines (81 loc) · 2.85 KB
/
Copy pathcode_5012023.java
File metadata and controls
101 lines (81 loc) · 2.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.jar.Attributes.Name;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
// @SpringBootAppliaction
// @EnableMongoRepositories
// public class Application {
// public static void main(String[] args) {
// SpringApplication.run(Application.class, args);
// }
// }
public class Item {
@Id
private String id;
private String name;
private String description;
public Item(){}
public Item(String name, String description) {
this.name=name;
this.description= description;
}
public String getID(){ return id; }
public void setId(String id) {this.id=id;}
public String getName(){ return name; }
public void setName(String name) {this.name=name;}
public String getDescription(){ return description; }
public void setDescription(String description) {this.description=description;}
}
public interface ItemRepository extends MongoRepository<Item,String>{}
/**
* RestController
* RequestMapping
*/
public class ItemController {
private final ItemRepository repository;
public ItemController(ItemRepository repository){
this.repository=repository;
}
@GetMapping
public List<Item> getAllItems(){
return repository.findAll();
}
@PostMapping
public Item addItem(@RequestBody Item item){
return repository.save(item);
}
@GetMapping("/{id}")
public Item getItem(@PathVariable String id){
return repository.findById(id).orElse(null);
}
@PutMapping("/{id}")
public Item updateItem(@PathVariable String id, @RequestBody Item item){
Item existingItem= repository.findById(id).orElse(null);
if(existingItem==null){
return null;
}
existingItem.setName((item.getName()));
existingItem.setDescription(item.getDescription());
return repository.save(existingItem);
}
}
/**
* Model: the `Item` class represents the model in this example.
* POJO(plain old java object) that represents a document in the `items`
* collection in the MongoDB database.
*
* View: using a REST API as the view, the view is the HTTP response that is
* returned to the client.
*
*
* Controller: The `ItemController` class is the controller in this example.
* It handles HTTP requests and performs CRUD operations on the `Items`
* collection using the `ItemRepository`
*
* Here is how the MVC components interact with each other:
* 1.The client sends an HTTP request to the server.
* 2.The controller receives the request and processes it.
* 3.The controller uses the repository to perform the necessary CRUD operation on the model
* 4.The controller returns an HTTP response to the client, which represents the view.
*
* **/