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
64ab8675
Commit
64ab8675
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe wsparcie XML
parent
5d0ab57d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
19 deletions
+78
-19
RProductXML.java
PC35-RestSerwer/src/main/java/rest/RProductXML.java
+8
-19
Price.java
PC35-RestSerwer/src/main/java/sklep/model/Price.java
+33
-0
ProductList.java
PC35-RestSerwer/src/main/java/sklep/model/ProductList.java
+37
-0
No files found.
PC35-RestSerwer/src/main/java/rest/RProductXML.java
View file @
64ab8675
package
rest
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.DELETE
;
import
javax.ws.rs.GET
;
...
...
@@ -16,7 +13,9 @@ import sklep.db.DBConnection;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Price
;
import
sklep.model.Product
;
import
sklep.model.ProductList
;
import
sklep.photo.PhotoUtil
;
@Path
(
"/products.xml"
)
...
...
@@ -25,10 +24,10 @@ import sklep.photo.PhotoUtil;
public
class
RProductXML
{
@GET
public
List
<
Product
>
readAllProducts
()
throws
DBException
{
public
ProductList
readAllProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
(
);
return
new
ProductList
(
productDAO
.
readAll
()
);
}
}
...
...
@@ -50,25 +49,21 @@ public class RProductXML {
@Path
(
"/{id}/price"
)
@GET
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
public
Price
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
return
product
.
getPrice
(
);
return
new
Price
(
product
.
getPrice
()
);
}
}
// W metodach HTTP POST i PUT klient przysyła dane na serwer (tzw. body / entity / content).
// Adnotacja @Consumes mówi w jakim formacie powinny być te dane.
// Metoda w JAX-RS może posiadać co najwyżej jeden parametr nieoznaczony żadną adnotacją.
// Właśnie poprzez ten parametr przekazywane są dane przysłane w treści zapytania (w praktyce: POST i PUT).
@Path
(
"/{id}/price"
)
@PUT
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
Price
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
product
.
setPrice
(
newPrice
);
product
.
setPrice
(
newPrice
.
getValue
()
);
productDAO
.
save
(
product
);
db
.
commit
();
}
...
...
@@ -80,11 +75,8 @@ public class RProductXML {
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
if
(
product
.
getProductId
()
==
null
)
{
// jeśli przysłany rekord nie posiada ID, to utawiam ID z adresu
// w szczególności pozwalam na zapisanie nowego produktu, ale pod określonym adresem
product
.
setProductId
(
productId
);
}
else
if
(
product
.
getProductId
()
!=
productId
)
{
// ale jeśli ID było ustawione, ale było inne niż w adresie, to jest to podejrzana sytuacja i takie zapytania nie wykonamy
throw
new
IllegalArgumentException
(
"ID produktu nie zgodne z adresem URL"
);
}
productDAO
.
save
(
product
);
...
...
@@ -92,9 +84,6 @@ public class RProductXML {
}
}
// za pomocą POST będzie można przysłać wyłącznie nowe produkty be określonego ID,
// a aplikacja sama wybierze nowe ID z sekwencjii
// w wyniku zostanie odesłany uzupełniony produkt
@POST
public
Product
addProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
PC35-RestSerwer/src/main/java/sklep/model/Price.java
0 → 100644
View file @
64ab8675
package
sklep
.
model
;
import
java.math.BigDecimal
;
import
javax.xml.bind.annotation.XmlRootElement
;
import
javax.xml.bind.annotation.XmlValue
;
@XmlRootElement
public
class
Price
{
@XmlValue
private
BigDecimal
value
;
public
Price
()
{
this
.
value
=
BigDecimal
.
ZERO
;
}
public
Price
(
BigDecimal
value
)
{
this
.
value
=
value
;
}
public
BigDecimal
getValue
()
{
return
value
;
}
public
void
setValue
(
BigDecimal
value
)
{
this
.
value
=
value
;
}
@Override
public
String
toString
()
{
return
value
.
toString
();
}
}
PC35-RestSerwer/src/main/java/sklep/model/ProductList.java
0 → 100644
View file @
64ab8675
package
sklep
.
model
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
javax.xml.bind.annotation.XmlElement
;
import
javax.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
(
name
=
"products"
)
public
class
ProductList
{
@XmlElement
(
name
=
"product"
)
private
final
List
<
Product
>
products
=
new
ArrayList
<>();
public
ProductList
()
{
// zostawia pustą listę
}
public
ProductList
(
Collection
<
Product
>
products
)
{
this
.
products
.
addAll
(
products
);
}
public
List
<
Product
>
getProducts
()
{
return
Collections
.
unmodifiableList
(
this
.
products
);
}
public
void
setProducts
(
Collection
<
Product
>
products
)
{
this
.
products
.
clear
();
this
.
products
.
addAll
(
products
);
}
@Override
public
String
toString
()
{
return
this
.
products
.
toString
();
}
}
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