need to change a json on a network request from the order

angular/java want to get the user id but also the products but dunno how to do it so they don t block each other
29 Replies
JavaBot
JavaBot10mo ago
This post has been reserved for your question.
Hey @timo! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Timo
TimoOP10mo ago
No description
Suika
Suika10mo ago
java{
"orderId": 1,
"status": "placed",
"orderDate": "2024-05-24",
"customUser": {
"id": 2,
"firstname": null,
"lastname": null,
"email": "[email protected]",
"password": "$2a$10$eaWM1ngVNwOJ2eMrFQW1MOgQ0vNAdGfyXulW3SwZXb4vCbRPIKK92",
"userRole": null
}
}
java{
"orderId": 1,
"status": "placed",
"orderDate": "2024-05-24",
"customUser": {
"id": 2,
"firstname": null,
"lastname": null,
"email": "[email protected]",
"password": "$2a$10$eaWM1ngVNwOJ2eMrFQW1MOgQ0vNAdGfyXulW3SwZXb4vCbRPIKK92",
"userRole": null
}
}
json
This message has been formatted automatically. You can disable this using /preferences.
Timo
TimoOP10mo ago
<h1>test</h1>
<div *ngIf="!loading">
<div *ngIf="orders">
<div *ngFor="let order of orders; ">
<div *ngFor="let product of order.products">
<div class="card" style="width: 18rem;">
<div class="card-body">
<p class="card-text"><strong> Bestelling ID:</strong> {{ order.id }}</p>
<p class="card-text"><strong>Product naam:</strong> {{ product.name }}</p>
<p class="card-text"><strong>Prijs product(en):</strong> {{ product.price | currency }}</p>
</div>
</div>
</div>
<div class="card" style="width: 18rem;">
<p class="card-text"><strong>Totaalprijs:</strong> {{ order.userEmail }}</p>
</div>
<hr>
</div>
</div>
<div *ngIf="!orders || orders.length === 0">
<p>Geen bestellingen gevonden.</p>
</div>
</div>
<h1>test</h1>
<div *ngIf="!loading">
<div *ngIf="orders">
<div *ngFor="let order of orders; ">
<div *ngFor="let product of order.products">
<div class="card" style="width: 18rem;">
<div class="card-body">
<p class="card-text"><strong> Bestelling ID:</strong> {{ order.id }}</p>
<p class="card-text"><strong>Product naam:</strong> {{ product.name }}</p>
<p class="card-text"><strong>Prijs product(en):</strong> {{ product.price | currency }}</p>
</div>
</div>
</div>
<div class="card" style="width: 18rem;">
<p class="card-text"><strong>Totaalprijs:</strong> {{ order.userEmail }}</p>
</div>
<hr>
</div>
</div>
<div *ngIf="!orders || orders.length === 0">
<p>Geen bestellingen gevonden.</p>
</div>
</div>
so need to change the request of the controller to make sure it connects the user by the id but also has the products and displays them right in the frontend
Timo
TimoOP10mo ago
No description
Timo
TimoOP10mo ago
No description
szatkus
szatkus10mo ago
Database-wise you can use JOIN to get both with one request. But I don't know which library you use to access the database.
Timo
TimoOP10mo ago
I use postgressql and hibernate Biggest problem is that the data im sending back is wrong compare to the html wanna see the products but has to be by id
szatkus
szatkus10mo ago
I see that you don't have userEmail in that JSON.
Timo
TimoOP10mo ago
I do email [email protected]?
szatkus
szatkus10mo ago
I mean, that JSON above is a request or respone? Wait, you're returning a password there? 😱
Timo
TimoOP10mo ago
Yeah?
szatkus
szatkus10mo ago
Don't do that. Even though it's just a hash.
Timo
TimoOP10mo ago
Security fears me but the code is Just wrong Hashcat :kekw: It is school so security doesn t matter that much
szatkus
szatkus10mo ago
Okay, so what do you want to return?
Timo
TimoOP10mo ago
the userid and the products wich they have ordered
Suika
Suika10mo ago
id": 1, "status": "Placed", "products": [
{
"id": 1,
"name": "Asus Gpu xt 7900",
"price": 799.0,
"description": "Een van de beste price performace videokaarten van AMD ",
"imageUrl": "https://edgeup.asus.com/wp-content/uploads/2022/11/feature-7900-xtx.jpg",
"stock": 4
}
],
"receiveDate": "2024-05-24",
"totalPrice": 799.0
}
{
"id": 1,
"name": "Asus Gpu xt 7900",
"price": 799.0,
"description": "Een van de beste price performace videokaarten van AMD ",
"imageUrl": "https://edgeup.asus.com/wp-content/uploads/2022/11/feature-7900-xtx.jpg",
"stock": 4
}
],
"receiveDate": "2024-05-24",
"totalPrice": 799.0
}
] bassically something like this but need to make sure the id is connected to the user so each order = user
This message has been formatted automatically. You can disable this using /preferences.
szatkus
szatkus10mo ago
Both handlers that you send above only limit results to a user that is provided by principal.
Timo
TimoOP10mo ago
what do you mean by that sorry don t understand
szatkus
szatkus10mo ago
Sorry, it's late 😄
Timo
TimoOP10mo ago
for me its not im just not a native speaker
szatkus
szatkus10mo ago
I'm talking about this code. In both cases you fetch only data related to the user that is logged in currently.
Timo
TimoOP10mo ago
so i could reframe the controller of getorderbyid? because it should already pick up the user id?
@GetMapping("/history")
public ResponseEntity<List<Order>> getOrderHistory( Principal principal) {
if (principal == null) {
return ResponseEntity.badRequest().build();
}
String userEmail = principal.getName();
Optional<CustomUser> user = userRepository.findByEmail(userEmail);
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ok(orderService.findOrdersByCustomUser(user.get()));

}
@GetMapping("/history")
public ResponseEntity<List<Order>> getOrderHistory( Principal principal) {
if (principal == null) {
return ResponseEntity.badRequest().build();
}
String userEmail = principal.getName();
Optional<CustomUser> user = userRepository.findByEmail(userEmail);
if (user.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ok(orderService.findOrdersByCustomUser(user.get()));

}
so how should i change this?
szatkus
szatkus10mo ago
If you want to have a different structure than you already have then probably the best way to achieve that would be creating a new class with all fields that you need and then mapping the results. Look for streams in Java.
Timo
TimoOP10mo ago
stream =foreach() but like can t i just change this cuz prob can t do something complicated like that over text
szatkus
szatkus10mo ago
orderService.findOrdersByCustomUser(user.get()).stream().map(order -> {
... and here goes all the mambo-jambo ...
}).toList()
orderService.findOrdersByCustomUser(user.get()).stream().map(order -> {
... and here goes all the mambo-jambo ...
}).toList()
Timo
TimoOP10mo ago
but then i still have the customuser in the frontend in the network etc :/ you down for a call or im i too scary 😅 :boohoo: @szatkus
szatkus
szatkus10mo ago
I'm not afraid, but I have other things to do 😄 I'm going to sleep. I hope you'll figure this out or someone else could help you.
JavaBot
JavaBot10mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?