From 8f6d03291b7130877ced850ae1d8e5ea9cbd3b46 Mon Sep 17 00:00:00 2001 From: Lemonochrme Date: Sat, 14 Dec 2024 17:24:40 +0100 Subject: [PATCH] Added RequestServiceApplication enpoints --- .../rest/RequestServiceApplication.java | 100 +++++++++++++++++- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/helpapp/request-service/src/main/java/insa/application/helpapp/rest/RequestServiceApplication.java b/helpapp/request-service/src/main/java/insa/application/helpapp/rest/RequestServiceApplication.java index c88afd2..f0e8f87 100644 --- a/helpapp/request-service/src/main/java/insa/application/helpapp/rest/RequestServiceApplication.java +++ b/helpapp/request-service/src/main/java/insa/application/helpapp/rest/RequestServiceApplication.java @@ -2,19 +2,109 @@ package insa.application.helpapp.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.*; +import java.util.concurrent.atomic.AtomicLong; @SpringBootApplication @RestController +@RequestMapping("/requests") public class RequestServiceApplication { + private final Map requestDatabase = new HashMap<>(); + private final AtomicLong idGenerator = new AtomicLong(1); + public static void main(String[] args) { SpringApplication.run(RequestServiceApplication.class, args); } - @GetMapping("/hello") - public String hello() { - return "Hello from Request Service!"; + // Create a new help request + @PostMapping + public HelpRequest createRequest(@RequestBody HelpRequest request) { + long id = idGenerator.getAndIncrement(); + request.setId(id); + request.setStatus("Pending"); + requestDatabase.put(id, request); + return request; + } + + // Get all requests (with optional status filter) + @GetMapping + public List getAllRequests(@RequestParam(required = false) String status) { + if (status == null) { + return new ArrayList<>(requestDatabase.values()); + } + List filteredRequests = new ArrayList<>(); + for (HelpRequest request : requestDatabase.values()) { + if (request.getStatus().equalsIgnoreCase(status)) { + filteredRequests.add(request); + } + } + return filteredRequests; + } + + // Get a specific request by ID + @GetMapping("/{id}") + public HelpRequest getRequest(@PathVariable Long id) { + return Optional.ofNullable(requestDatabase.get(id)) + .orElseThrow(() -> new RuntimeException("Request not found")); + } + + // Update a request (e.g., status or details) + @PutMapping("/{id}") + public HelpRequest updateRequest(@PathVariable Long id, @RequestBody HelpRequest updatedRequest) { + if (!requestDatabase.containsKey(id)) { + throw new RuntimeException("Request not found"); + } + HelpRequest existingRequest = requestDatabase.get(id); + if (updatedRequest.getDetails() != null) { + existingRequest.setDetails(updatedRequest.getDetails()); + } + if (updatedRequest.getStatus() != null) { + existingRequest.setStatus(updatedRequest.getStatus()); + } + return existingRequest; + } + + // Delete a request + @DeleteMapping("/{id}") + public String deleteRequest(@PathVariable Long id) { + if (requestDatabase.remove(id) == null) { + throw new RuntimeException("Request not found"); + } + return "Request deleted successfully"; + } + + // HelpRequest entity + static class HelpRequest { + private Long id; + private String details; + private String status; // e.g., Pending, Validated, Rejected, Completed + + // Getters and setters + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDetails() { + return details; + } + + public void setDetails(String details) { + this.details = details; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } } }