68 lines
2.7 KiB
Java
68 lines
2.7 KiB
Java
package zw.qantra.tm.domain.controllers;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import net.kaczmarzyk.spring.data.jpa.domain.Equal;
|
|
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import zw.qantra.tm.domain.models.Recipient;
|
|
import zw.qantra.tm.domain.services.RecipientService;
|
|
import net.kaczmarzyk.spring.data.jpa.web.annotation.And;
|
|
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/public/recipients")
|
|
@RequiredArgsConstructor
|
|
public class RecipientController {
|
|
|
|
private final RecipientService recipientService;
|
|
|
|
@GetMapping
|
|
public ResponseEntity searchRecipients(
|
|
@And({
|
|
@Spec(path = "name", spec = Equal.class),
|
|
@Spec(path = "email", spec = Equal.class),
|
|
@Spec(path = "phoneNumber", spec = Equal.class),
|
|
@Spec(path = "account", spec = Equal.class),
|
|
@Spec(path = "latestProviderLabel", spec = Equal.class),
|
|
@Spec(path = "userId", spec = Equal.class),
|
|
@Spec(path = "workspaceId", defaultVal = "null", spec = Equal.class)
|
|
}) Specification<Recipient> specification, Pageable pageable) {
|
|
return ResponseEntity.ok(recipientService.findAllRecipients(specification, pageable));
|
|
}
|
|
|
|
@GetMapping("/{account}/workspace/{workspaceId}")
|
|
public ResponseEntity<Recipient> getRecipient(@PathVariable String account, @PathVariable UUID workspaceId) {
|
|
List<Recipient> recipients = recipientService.getRecipient(account, workspaceId);
|
|
if(recipients.size() > 0){
|
|
return ResponseEntity.ok(recipients.get(0));
|
|
}
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
|
|
@PostMapping
|
|
public ResponseEntity<Recipient> createRecipient(@RequestBody Recipient recipient) {
|
|
return ResponseEntity.ok(recipientService.save(recipient));
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public ResponseEntity<Recipient> updateRecipient(@PathVariable UUID id, @RequestBody Recipient recipient) {
|
|
Recipient updatedRecipient = recipientService.updateRecipient(id, recipient);
|
|
if (updatedRecipient != null) {
|
|
return ResponseEntity.ok(updatedRecipient);
|
|
}
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public ResponseEntity<String> deleteRecipient(@PathVariable UUID id) {
|
|
if (recipientService.deleteRecipient(id)) {
|
|
return ResponseEntity.ok("Recipient deleted successfully");
|
|
}
|
|
return ResponseEntity.notFound().build();
|
|
}
|
|
} |