-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNotes.txt
More file actions
104 lines (97 loc) · 6.97 KB
/
Notes.txt
File metadata and controls
104 lines (97 loc) · 6.97 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
@RestController: This annotation indicates that the class defines a
RESTful web service. It's a convenience annotation that combines @Controller
and @ResponseBody, meaning that methods in this class return data that is
serialized directly into the HTTP response body.
BakerController: This is the name of the controller class.
private BakerService service;: This declares a private field service of type
BakerService. This field will be autowired by Spring with an instance of BakerService.
@Autowired: This annotation is used to automatically wire the BakerService bean
into the constructor of BakerController.
public BakerController(BakerService service): This is the constructor of
BakerController, which takes an instance of BakerService as a parameter.
When Spring initializes the controller, it injects an instance of BakerService
into this constructor.
@GetMapping("/bakers"): This annotation maps HTTP GET requests for the /bakers
endpoint to the index() method.
public ResponseEntity<Iterable<Baker>> index(): This method handles HTTP GET
requests for the /bakers endpoint. It returns a ResponseEntity containing an
Iterable of Baker objects, which represents a collection of bakers.
new ResponseEntity<>(service.index(), HttpStatus.OK): This creates a new
ResponseEntity with the data returned by service.index() as the body and the
HTTP status code OK (200).
@GetMapping("/baker/{id}"): This annotation maps HTTP GET requests for the
/baker/{id} endpoint to the show() method. The {id} part of the path is a placeholder
for the ID of the specific baker.
public ResponseEntity<Baker> show(@PathVariable Long id): This method handles HTTP
GET requests for the /baker/{id} endpoint. It takes the id path variable as a
parameter and returns a ResponseEntity containing a Baker object with the specified ID.
@PostMapping("/bakers"): This annotation maps HTTP POST requests for the /bakers
endpoint to the create() method.
public ResponseEntity<Baker> create(@RequestBody Baker baker): This method
handles HTTP POST requests for the /bakers endpoint. It takes a Baker object as
a request body, creates a new baker using the service.create() method, and returns
a ResponseEntity containing the created Baker object with the HTTP status code
CREATED (201).
@PutMapping("/baker/{id}"): This annotation maps HTTP PUT requests for the /baker/{id}
endpoint to the update() method.
public ResponseEntity<Baker> update(@PathVariable Long id, @RequestBody Baker baker):
This method handles HTTP PUT requests for the /baker/{id} endpoint. It takes the id
path variable and a Baker object as parameters, updates the baker with the specified
ID using the service.update() method, and returns a ResponseEntity containing the
updated Baker object with the HTTP status code OK (200).
@DeleteMapping("/baker/{id}"): This annotation maps HTTP DELETE requests for the
/baker/{id} endpoint to the destroy() method.
public ResponseEntity<Boolean> destroy(@PathVariable Long id): This method handles
HTTP DELETE requests for the /baker/{id} endpoint. It takes the id path variable as
a parameter, deletes the baker with the specified ID using the service.delete() method,
and returns a ResponseEntity containing a Boolean indicating whether the deletion was
successful, along with the HTTP status code OK (200).
@Service: This annotation marks the class as a Spring-managed service component.
It's typically used to indicate that the class contains business logic and should
be managed by the Spring container.
private MuffinRepository repository;: This declares a private field repository of
type MuffinRepository. This field is autowired by Spring with an instance of MuffinRepository.
@Autowired: This annotation is used to automatically wire the MuffinRepository bean
into the constructor of MuffinService.
public MuffinService(MuffinRepository repository): This is the constructor of
MuffinService, which takes an instance of MuffinRepository as a parameter. When Spring
initializes the service, it injects an instance of MuffinRepository into this constructor.
public Iterable<Muffin> index(): This method returns an Iterable of Muffin objects,
representing a collection of muffins. It delegates the call to the findAll() method of
the MuffinRepository, which retrieves all muffins from the database.
public Muffin show(Long id): This method takes a Long ID as a parameter and returns the
Muffin object with the specified ID. It delegates the call to the findById() method of
the MuffinRepository, which retrieves the muffin from the database by its ID.
public Muffin create(Muffin muffin): This method takes a Muffin object as a parameter
and creates a new muffin in the database. It delegates the call to the save() method of
the MuffinRepository, which saves the muffin to the database.
public Muffin update(Long id, Muffin newMuffinData): This method takes a Long ID and a
Muffin object containing updated muffin data as parameters. It retrieves the original
muffin from the database by its ID using the findById() method of the MuffinRepository,
updates its flavor with the new data, and then saves the updated muffin back to the
database using the save() method of the MuffinRepository.
public Boolean delete(Long id): This method takes a Long ID as a parameter and deletes
the muffin with the specified ID from the database. It delegates the call to the
deleteById() method of the MuffinRepository, which deletes the muffin from the database
by its ID.
Overall, the MuffinService class encapsulates the business logic for managing muffins,
such as retrieving, creating, updating, and deleting muffins in the database. It interacts
with the data access layer (in this case, the MuffinRepository) to perform database
operations.
@Repository: This annotation is used to indicate that the interface is a Spring
Data repository. It tells Spring to create a bean for this interface during component
scanning and enables exception translation for JPA-related exceptions.
public interface BakerRepository: This declares an interface named BakerRepository.
Spring Data repositories are interfaces that define methods for accessing and manipulating
data in the underlying database.
extends CrudRepository<Baker, Long>: This interface extends the CrudRepository interface,
which is a part of the Spring Data JPA framework. CrudRepository provides CRUD (Create,
Read, Update, Delete) operations for the entity type Baker. The first generic parameter
<Baker> specifies the entity type managed by the repository, and the second generic
parameter <Long> specifies the type of the entity's primary key.
In summary, the BakerRepository interface serves as a contract defining methods for
performing CRUD operations on Baker entities. It extends the CrudRepository interface,
which provides basic database operations out of the box, without the need for explicit
implementation. Spring Data JPA generates the implementation of these methods at runtime
based on naming conventions and method signatures. This approach reduces boilerplate code
and simplifies database interaction in Spring applications.