Refactor: Update FormOfAddressService and implement FormOfAdressController
parent
17ce97390a
commit
ad0e18bc56
|
@ -0,0 +1,107 @@
|
|||
package com.maradona.backend.controllers.api;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.maradona.backend.entities.FormOfAddress;
|
||||
import com.maradona.backend.services.FormOfAddressService;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
|
||||
/**
|
||||
* Controller for routing to the form of address related API endpoints.
|
||||
*
|
||||
* List of endpoints:
|
||||
* - GET /api/form-of-adress
|
||||
* - GET /api/form-of-adress/all
|
||||
* - POST /api/form-of-adress
|
||||
* - PUT /api/form-of-adress
|
||||
* - DELETE /api/form-of-adress
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/form-of-adress")
|
||||
class FormOfAdressController {
|
||||
|
||||
@Autowired
|
||||
private FormOfAddressService formOfAdressService;
|
||||
|
||||
/**
|
||||
* Returns the form of address with the given ID.
|
||||
*
|
||||
* @param id The ID of the form of address to return.
|
||||
* @return The description of the form of address with the given ID.
|
||||
*/
|
||||
@GetMapping({ "/", "" })
|
||||
public ResponseEntity<FormOfAddress> getFormOfAdress(@RequestParam Long id) {
|
||||
return formOfAdressService.getFormOfAddressById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all form of addresses.
|
||||
*
|
||||
* @return A list of all form of addresses.
|
||||
*/
|
||||
@GetMapping("/all")
|
||||
public ResponseEntity<Iterable<FormOfAddress>> getAllFormOfAdresses() {
|
||||
Iterable<FormOfAddress> formOfAddresses = formOfAdressService.getAllFormOfAddresses();
|
||||
return ResponseEntity.ok(formOfAddresses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new form of address.
|
||||
*
|
||||
* @param description The description of the new form of address.
|
||||
* @return The ID of the newly created form of address.
|
||||
*/
|
||||
@PostMapping({ "/", "" })
|
||||
public ResponseEntity<Void> createFormOfAdress(@RequestBody String description) {
|
||||
var formOfAddress = new FormOfAddress();
|
||||
formOfAddress.setDescription(description);
|
||||
formOfAdressService.saveFormOfAddress(formOfAddress);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the description of a form of address.
|
||||
*
|
||||
* @param id The ID of the form of address to update.
|
||||
* @param description The new description of the form of address.
|
||||
* @return The updated form of address.
|
||||
*/
|
||||
@PutMapping({ "/", "" })
|
||||
public ResponseEntity<Void> updateFormOfAdress(@RequestParam Long id, @RequestBody String description) {
|
||||
var formOfAddress = formOfAdressService.getFormOfAddressById(id).orElse(null);
|
||||
if (formOfAddress == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
formOfAddress.setDescription(description);
|
||||
formOfAdressService.saveFormOfAddress(formOfAddress);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a form of address.
|
||||
*
|
||||
* @param id The ID of the form of address to delete.
|
||||
* @return The deleted form of address.
|
||||
*/
|
||||
@DeleteMapping({ "/", "" })
|
||||
public ResponseEntity<Void> deleteFormOfAdress(@RequestParam Long id) {
|
||||
var formOfAddress = formOfAdressService.getFormOfAddressById(id).orElse(null);
|
||||
if (formOfAddress == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
formOfAdressService.deleteFormOfAddress(id);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
|
@ -28,4 +28,14 @@ public class FormOfAddressService {
|
|||
public Iterable<FormOfAddress> getAllFormOfAddresses() {
|
||||
return formOfAddressRepository.findAll();
|
||||
}
|
||||
|
||||
public Long updateFormOfAddress(Long id, String description) {
|
||||
var formOfAddress = formOfAddressRepository.findById(id).orElse(null);
|
||||
if (formOfAddress != null) {
|
||||
formOfAddress.setDescription(description);
|
||||
formOfAddressRepository.save(formOfAddress);
|
||||
return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue