Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
d6eb5a75
Commit
d6eb5a75
authored
Nov 08, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uzupełnienia w wersjach JSON
parent
223eb2cc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
117 additions
and
0 deletions
+117
-0
RCustomers.java
PC35-RestSerwer/src/main/java/rest/RCustomers.java
+39
-0
ROrders.java
PC35-RestSerwer/src/main/java/rest/ROrders.java
+36
-0
RProducts.java
PC35-RestSerwer/src/main/java/rest/RProducts.java
+42
-0
No files found.
PC35-RestSerwer/src/main/java/rest/RCustomers.java
View file @
d6eb5a75
...
...
@@ -2,11 +2,16 @@ package rest;
import
java.util.List
;
import
jakarta.ws.rs.DELETE
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.POST
;
import
jakarta.ws.rs.PUT
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.db.CustomerDAO
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
...
...
@@ -33,4 +38,38 @@ public class RCustomers {
return
customerDAO
.
readAll
();
}
}
@POST
public
Response
save
(
final
Customer
customer
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customerDAO
.
save
(
customer
);
db
.
commit
();
return
Response
.
created
(
UriBuilder
.
fromResource
(
RCustomers
.
class
).
path
(
String
.
valueOf
(
customer
.
getEmail
())).
build
()).
build
();
}
}
@PUT
@Path
(
"/{id}"
)
public
Response
update
(
@PathParam
(
"id"
)
String
email
,
final
Customer
customer
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customer
.
setEmail
(
email
);
customerDAO
.
save
(
customer
);
db
.
commit
();
}
return
Response
.
noContent
().
build
();
}
@DELETE
@Path
(
"/{id}"
)
public
Response
deleteById
(
@PathParam
(
"id"
)
String
email
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
customerDAO
.
delete
(
email
);
db
.
commit
();
}
return
Response
.
noContent
().
build
();
}
}
PC35-RestSerwer/src/main/java/rest/ROrders.java
View file @
d6eb5a75
package
rest
;
import
java.net.URI
;
import
java.util.List
;
import
jakarta.ws.rs.GET
;
...
...
@@ -9,6 +10,7 @@ import jakarta.ws.rs.Produces;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.Response.Status
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.OrderDAO
;
...
...
@@ -44,4 +46,38 @@ public class ROrders {
}
}
/*
// 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 {
try(DBConnection db = DBConnection.open()) {
OrderDAO orderDAO = db.orderDAO();
CustomerDAO customerDAO = db.customerDAO();
Order order = orderDAO.findById(orderId);
Customer customer = customerDAO.findByEmail(order.getCustomerEmail());
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]+}/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
(
RCustomers
.
class
)
.
path
(
"/{email}"
)
.
build
(
order
.
getCustomerEmail
());
return
Response
.
seeOther
(
customerURI
).
build
();
}
}
}
PC35-RestSerwer/src/main/java/rest/RProducts.java
View file @
d6eb5a75
package
rest
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
jakarta.ws.rs.Consumes
;
import
jakarta.ws.rs.DELETE
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.POST
;
import
jakarta.ws.rs.PUT
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
...
...
@@ -49,6 +52,45 @@ public class RProducts {
}
}
// Ta metoda zwraca wartość wybranego pola w rekordzie.
// W praktyce rzadko kiedy twozy się takie metody, ale gdybyśmy wiedzieli, że klient akurat takiej rzeczy może potrzebować,
// to można taką dodatkową meotdę stworzyć.
// Właściwą strukturą adresu będzie wtedy np. products/3/price
@GET
@Path
(
"/{id}/price"
)
@Produces
(
"application/json"
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
findById
(
productId
).
getPrice
();
}
}
// Metoda PUT służy w HTTP do zapisywania danych DOKŁADNIE POD PODANYM ADRESEM
@PUT
@Path
(
"/{id}/price"
)
@Consumes
(
"application/json"
)
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
product
.
setPrice
(
newPrice
);
productDAO
.
update
(
product
);
db
.
commit
();
}
}
@DELETE
@Path
(
"/{id}"
)
public
void
delete
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
delete
(
productId
);
db
.
commit
();
}
}
@GET
@Path
(
"/{id}/photo"
)
@Produces
(
"image/jpeg"
)
...
...
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