Implement administration and feedback services with request validation, rejection, and feedback management

This commit is contained in:
Lemonochrme 2024-12-16 17:33:31 +01:00
parent 5c2739cb98
commit 49c69016cc
6 changed files with 203 additions and 127 deletions

View file

@ -101,3 +101,85 @@ curl -X POST -H "Content-Type: application/json" -d '{"description":"Available f
- **Scalabilité** : Les microservices peuvent être déployés et mis à l'échelle individuellement.
- **Flexibilité** : Possibilité dutiliser différentes technologies pour chaque service.
- **Maintenance** : Une meilleure isolation des fonctionnalités simplifie le débogage et les mises à jour.
#### Tester les services
##### 1. **Créer un utilisateur avec UserService**
```bash
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Jane Doe", "email":"jane@example.com", "password":"1234", "role":"REQUESTER"}' \
http://localhost:8083/users
```
##### 2. **Créer une demande avec RequestService**
```bash
curl -X POST -H "Content-Type: application/json" \
-d '{"userId":1, "details":"I need help with my groceries"}' \
http://localhost:8082/requests
```
##### 3. **Valider une demande avec AdministrationService**
```bash
curl -X PUT http://localhost:8080/admin/requests/1/validate
```
##### 4. **Ajouter un feedback avec FeedbackService**
```bash
curl -X POST -H "Content-Type: application/json" \
-d '{"requestId":1, "comment":"Great service!", "rating":5}' \
http://localhost:8081/feedbacks
```
##### 5. **Un volontaire répond à une demande avec VolunteerService**
```bash
curl -X POST http://localhost:8084/volunteers/1/help?requestId=1
```
- **Lister les utilisateurs (UserService)** :
```bash
curl -X GET http://localhost:8083/users/1
```
- **Lister les demandes associées à un utilisateur (RequestService)** :
```bash
curl -X GET http://localhost:8082/requests/user/1
```
- **Lister les feedbacks pour une demande (FeedbackService)** :
```bash
curl -X GET http://localhost:8081/feedbacks/request/1
```
- **Lister les actions des volontaires (VolunteerService)** :
```bash
curl -X GET http://localhost:8084/volunteers/actions
```

View file

@ -2,19 +2,38 @@ 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.*;
@SpringBootApplication
@RestController
@RequestMapping("/admin/requests")
public class AdministrationServiceApplication {
private final Map<Long, String> requestStatusDatabase = new HashMap<>();
public static void main(String[] args) {
SpringApplication.run(AdministrationServiceApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from Administration Service!";
// Validate a request
@PutMapping("/{id}/validate")
public String validateRequest(@PathVariable Long id) {
requestStatusDatabase.put(id, "Validated");
return "Request " + id + " validated.";
}
// Reject a request with justification
@PutMapping("/{id}/reject")
public String rejectRequest(@PathVariable Long id, @RequestParam String reason) {
requestStatusDatabase.put(id, "Rejected: " + reason);
return "Request " + id + " rejected for reason: " + reason;
}
// View request statuses
@GetMapping
public Map<Long, String> viewAllStatuses() {
return requestStatusDatabase;
}
}

View file

@ -2,19 +2,82 @@ 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("/feedbacks")
public class FeedbackServiceApplication {
private final Map<Long, Feedback> feedbackDatabase = new HashMap<>();
private final AtomicLong idGenerator = new AtomicLong(1);
public static void main(String[] args) {
SpringApplication.run(FeedbackServiceApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from FeedBack Service!";
// Add feedback
@PostMapping
public Feedback addFeedback(@RequestBody Feedback feedback) {
long id = idGenerator.getAndIncrement();
feedback.setId(id);
feedbackDatabase.put(id, feedback);
return feedback;
}
// Get feedback by request ID
@GetMapping("/request/{requestId}")
public List<Feedback> getFeedbackByRequest(@PathVariable Long requestId) {
List<Feedback> feedbackList = new ArrayList<>();
for (Feedback feedback : feedbackDatabase.values()) {
if (feedback.getRequestId().equals(requestId)) {
feedbackList.add(feedback);
}
}
return feedbackList;
}
// Feedback entity
static class Feedback {
private Long id;
private Long requestId;
private String comment;
private Integer rating; // 1 to 5
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getRequestId() {
return requestId;
}
public void setRequestId(Long requestId) {
this.requestId = requestId;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
}
}

View file

@ -57,6 +57,12 @@ public class RequestServiceApplication {
return userRequests;
}
// Get all help requests
@GetMapping
public List<HelpRequest> getAllRequests() {
return new ArrayList<>(requestDatabase.values());
}
// Simulate user validation (integration with UserService)
private boolean isUserValid(Long userId) {
try {

View file

@ -2,19 +2,37 @@ 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.*;
@SpringBootApplication
@RestController
@RequestMapping("/volunteers")
public class VolunteerServiceApplication {
private final Map<Long, String> volunteerActions = new HashMap<>();
public static void main(String[] args) {
SpringApplication.run(VolunteerServiceApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello from Volunteer Service!";
// View available requests (simulating)
@GetMapping("/requests")
public List<String> viewAvailableRequests() {
return List.of("Request 1: Grocery delivery", "Request 2: Medical help");
}
// Respond to a specific request
@PostMapping("/{volunteerId}/help")
public String respondToRequest(@PathVariable Long volunteerId, @RequestParam Long requestId) {
volunteerActions.put(requestId, "Volunteer " + volunteerId + " responded.");
return "Volunteer " + volunteerId + " responded to request " + requestId;
}
// View all volunteer actions
@GetMapping("/actions")
public Map<Long, String> viewActions() {
return volunteerActions;
}
}

View file

@ -1,112 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HelpApp - Simple Frontend</title>
</head>
<body>
<h1>HelpApp - Test Frontend</h1>
<!-- User Service -->
<section>
<h2>User Service</h2>
<form id="create-user-form">
<h3>Create User</h3>
<input type="text" id="user-name" placeholder="Name" required>
<input type="email" id="user-email" placeholder="Email" required>
<input type="password" id="user-password" placeholder="Password" required>
<select id="user-role" required>
<option value="REQUESTER">Requester</option>
<option value="VOLUNTEER">Volunteer</option>
<option value="ADMIN">Admin</option>
</select>
<button type="submit">Create User</button>
</form>
<form id="get-user-form">
<h3>Get User by ID</h3>
<input type="number" id="get-user-id" placeholder="User ID" required>
<button type="submit">Get User</button>
</form>
<div id="user-response"></div>
</section>
<!-- Request Service -->
<section>
<h2>Request Service</h2>
<form id="create-request-form">
<h3>Create Help Request</h3>
<input type="number" id="request-user-id" placeholder="User ID" required>
<input type="text" id="request-details" placeholder="Request Details" required>
<button type="submit">Create Request</button>
</form>
<form id="get-requests-form">
<h3>Get Requests by User ID</h3>
<input type="number" id="get-requests-user-id" placeholder="User ID" required>
<button type="submit">Get Requests</button>
</form>
<div id="request-response"></div>
</section>
<script>
const userServiceBaseUrl = 'http://localhost:8083/users';
const requestServiceBaseUrl = 'http://localhost:8082/requests';
// Handle User Creation
document.getElementById('create-user-form').addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('user-name').value;
const email = document.getElementById('user-email').value;
const password = document.getElementById('user-password').value;
const role = document.getElementById('user-role').value;
const response = await fetch(userServiceBaseUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email, password, role }),
});
const data = await response.json();
document.getElementById('user-response').innerText = JSON.stringify(data, null, 2);
});
// Handle Get User by ID
document.getElementById('get-user-form').addEventListener('submit', async (e) => {
e.preventDefault();
const userId = document.getElementById('get-user-id').value;
const response = await fetch(`${userServiceBaseUrl}/${userId}`);
const data = await response.json();
document.getElementById('user-response').innerText = JSON.stringify(data, null, 2);
});
// Handle Request Creation
document.getElementById('create-request-form').addEventListener('submit', async (e) => {
e.preventDefault();
const userId = document.getElementById('request-user-id').value;
const details = document.getElementById('request-details').value;
const response = await fetch(requestServiceBaseUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ userId, details }),
});
const data = await response.json();
document.getElementById('request-response').innerText = JSON.stringify(data, null, 2);
});
// Handle Get Requests by User ID
document.getElementById('get-requests-form').addEventListener('submit', async (e) => {
e.preventDefault();
const userId = document.getElementById('get-requests-user-id').value;
const response = await fetch(`${requestServiceBaseUrl}/user/${userId}`);
const data = await response.json();
document.getElementById('request-response').innerText = JSON.stringify(data, null, 2);
});
</script>
</body>
</html>