Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_weekendowa_20221008
Commits
48a00e96
Commit
48a00e96
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
/order/{id}/customer - za pomocą przekierowania
parent
01d911b3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
2 deletions
+22
-2
ROrder.java
PC35-RestSerwer/src/main/java/sklep/rest/ROrder.java
+22
-2
No files found.
PC35-RestSerwer/src/main/java/sklep/rest/ROrder.java
View file @
48a00e96
package
sklep
.
rest
;
import
java.net.URI
;
import
java.util.List
;
import
javax.enterprise.context.RequestScoped
;
...
...
@@ -10,13 +11,12 @@ import javax.ws.rs.PathParam;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response.Status
;
import
javax.ws.rs.core.UriBuilder
;
import
sklep.db.CustomerDAO
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.OrderDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Customer
;
import
sklep.model.Order
;
@RequestScoped
...
...
@@ -50,9 +50,11 @@ public class ROrder {
}
}
/*
// Metoda, która ma obsłużyć pobranie info o właścicielu zamówienia:
// /orders/1/customer
// W tej wersji metoda zwraca bezpośrednio dane klienta.
// Wada tego podejścia: ten sam rekord (konkretny klient) jest widoczny pod różnymi adresami URL.
@GET
@Path("/{id:[0-9][0-9]*}/customer")
public Customer getCustomer(@PathParam("id") Integer orderId) throws DBException, RecordNotFound {
...
...
@@ -64,5 +66,23 @@ public class ROrder {
return customer;
}
}
*/
// W tej wersji w odpowiedzi na zapytanie o dane klienta, który złożył zamówienie,
// wyślemy przekierowanie pod adres tego klienta.
// To jest lepsze z punktu widzenia "dobrych praktyk REST".
@GET
@Path
(
"/{id:[0-9][0-9]*}/customer"
)
public
Response
getCustomer
(
@PathParam
(
"id"
)
Integer
orderId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
Order
order
=
orderDAO
.
findById
(
orderId
);
URI
customerURI
=
UriBuilder
.
fromResource
(
RCustomer
.
class
)
.
path
(
"/{email}"
)
.
build
(
order
.
getCustomerEmail
());
return
Response
.
seeOther
(
customerURI
).
build
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment