In search of a solution to properly send data to the server in a format that it can accept. Currently, the title and descriptions are being successfully transmitted but the ratings are not coming through. It should be noted that there will be more than two types of ratings involved.
Here is an example of the POST request body format required by the server:
{
"title": "Test title",
"description": "Test description",
"ratings": [
{
"value": 2,
"category": {
"name": "difficulty"
}
},
{
"value": 5,
"category": {
"name": "story"
}
}
]
}
The Angular component.ts file for the review form utilizes the FormBuilder. Users input scores ranging from 1-10 for different categories.
ngOnInit() {
this.reviewForm = this.fb.group({
gameTitle: ['', []],
gameDescription: ['', []],
scores: this.fb.group({
difficulty: [, []],
story: [, []]
})
})
}
Upon submitting the form in the Angular component.ts file, the data is sent to the server using a service. However, there are concerns about the structure of the ratings data and how to extract the score values and names for the ratings object.
onSubmit() {
this.gameService.postReview(this.reviewForm.value)
.subscribe(() => {
this.success = true;
});
}
Within the Angular API service.ts file, the postReview function handles the process of posting data to the server. How can the ratings data be formatted correctly for the server to accept it? And how can the score values and names be extracted for inclusion in the ratings object?
postReview(reviewData: any) {
const gameReview = {
title: reviewData.gameTitle,
description: reviewData.gameDescription,
ratings: [{
value: reviewData.scores.value,
category: {
name: reviewData.scores.key
}
}]
};
return this.http.post(this.API_URL, gameReview, httpOptions);
}