mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 13:40:50 +02:00
Backend: Feedback creation with the api : "/create_feedback"
This commit is contained in:
parent
8cf27b7b64
commit
3b8b095db5
5 changed files with 101 additions and 6 deletions
|
@ -23,7 +23,7 @@ Course Exercice : Application to help others
|
||||||
- [X] `Rest` Make sure admin can do everything and users don't
|
- [X] `Rest` Make sure admin can do everything and users don't
|
||||||
- [X] `Rest` Create a Help Request
|
- [X] `Rest` Create a Help Request
|
||||||
- [X] `Rest` Modify the Help Request status
|
- [X] `Rest` Modify the Help Request status
|
||||||
- [ ] `Rest` Create a User Feedback
|
- [X] `Rest` Create a User Feedback
|
||||||
- [ ] `Rest` Gather User Feedbacks
|
- [ ] `Rest` Gather User Feedbacks
|
||||||
|
|
||||||
## Check `SOAP` Requests
|
## Check `SOAP` Requests
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package insa.application.helpapp.rest;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "feedback", schema = "service-architecture")
|
||||||
|
public class Feedback {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@Column(name = "id_user", nullable = false)
|
||||||
|
private int idUser;
|
||||||
|
|
||||||
|
@Column(name = "id_request", nullable = false)
|
||||||
|
private int idRequest;
|
||||||
|
|
||||||
|
@Column(name = "message", nullable = false)
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
// Getters and Setters
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdUser() {
|
||||||
|
return idUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdUser(int idUser) {
|
||||||
|
this.idUser = idUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdRequest() {
|
||||||
|
return idRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdRequest(int idRequest) {
|
||||||
|
this.idRequest = idRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package insa.application.helpapp.rest;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface FeedbackRepository extends JpaRepository<Feedback, Integer> {
|
||||||
|
}
|
|
@ -8,6 +8,9 @@ import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@ -15,6 +18,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
public class FeedbackServiceApplication {
|
public class FeedbackServiceApplication {
|
||||||
@Autowired
|
@Autowired
|
||||||
private AdministrationService administrationService;
|
private AdministrationService administrationService;
|
||||||
|
@Autowired
|
||||||
|
private RequestRepository requestRepository;
|
||||||
|
@Autowired
|
||||||
|
private FeedbackRepository feedbackRepository;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(FeedbackServiceApplication.class, args);
|
SpringApplication.run(FeedbackServiceApplication.class, args);
|
||||||
|
@ -33,4 +40,28 @@ public class FeedbackServiceApplication {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/create_feedback")
|
||||||
|
public ResponseEntity<?> CreateFeedback(@RequestParam int idUser, @RequestParam String token,
|
||||||
|
@RequestParam int idRequest, @RequestParam String message) {
|
||||||
|
if (!administrationService.checkToken(idUser, token)) {
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
||||||
|
}
|
||||||
|
Optional<Request> requestOption = requestRepository.findById(idRequest);
|
||||||
|
if (!requestOption.isPresent()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("No request found with the following ID.");
|
||||||
|
}
|
||||||
|
Request request = requestOption.get();
|
||||||
|
int idRole = administrationService.getRole(idUser).get();
|
||||||
|
// Checks if the user id corresponds to a 'user'. if admin/volunteer can send feedback either way
|
||||||
|
if((idRole == RoleEnum.USER.getValue()) && (request.getIdUser() != idUser))
|
||||||
|
{
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Only the author can respond to its own request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Feedback feedback = new Feedback();
|
||||||
|
feedback.setIdRequest(idRequest);
|
||||||
|
feedback.setIdUser(idUser);
|
||||||
|
feedback.setMessage(message);
|
||||||
|
return ResponseEntity.ok(feedbackRepository.save(feedback));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,17 +60,17 @@ public class RequestServiceApplication {
|
||||||
return ResponseEntity.ok(requestRepository.save(request));
|
return ResponseEntity.ok(requestRepository.save(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put should be : /change_status?idUser=1&idMessage=1&status=1&token=?
|
// Put should be : /change_status?idUser=1&idRequest=1&status=1&token=?
|
||||||
// Response if success: the request with the changed status
|
// Response if success: the request with the changed status
|
||||||
@PutMapping("/change_status")
|
@PutMapping("/change_status")
|
||||||
public ResponseEntity<?> changeStatus(@RequestParam int idUser, @RequestParam String token,
|
public ResponseEntity<?> changeStatus(@RequestParam int idUser, @RequestParam String token,
|
||||||
@RequestParam int idMessage, @RequestParam int status) {
|
@RequestParam int idRequest, @RequestParam int status) {
|
||||||
if (!administrationService.checkToken(idUser, token)) {
|
if (!administrationService.checkToken(idUser, token)) {
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User or token invalid.");
|
||||||
}
|
}
|
||||||
Optional<Request> requestOption = requestRepository.findById(idMessage);
|
Optional<Request> requestOption = requestRepository.findById(idRequest);
|
||||||
if (!requestOption.isPresent()) {
|
if (!requestOption.isPresent()) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("No message found with the following ID.");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("No request found with the following ID.");
|
||||||
}
|
}
|
||||||
Request request = requestOption.get();
|
Request request = requestOption.get();
|
||||||
|
|
||||||
|
@ -116,5 +116,4 @@ public class RequestServiceApplication {
|
||||||
.body("Only Admins can perform this action.");
|
.body("Only Admins can perform this action.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue