Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
f44527c9
Commit
f44527c9
authored
Jun 14, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wersja MultiFormatowa
parent
974a6bf0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
9 deletions
+37
-9
RProducts.java
PC27-RestSerwer/src/main/java/sklep/rest/RProducts.java
+29
-3
RProductsXml.java
PC27-RestSerwer/src/main/java/sklep/rest/RProductsXml.java
+8
-6
No files found.
PC27-RestSerwer/src/main/java/sklep/rest/RProducts.java
View file @
f44527c9
...
...
@@ -11,9 +11,9 @@ import java.math.BigDecimal;
import
java.util.List
;
@Path
(
"/products"
)
@Produces
(
"application/json"
)
public
class
RProducts
{
@GET
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain;charset=UTF-8"
})
public
List
<
Product
>
allProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -21,8 +21,24 @@ public class RProducts {
}
}
// Może też być tak, że kilka metod działa pod tym samym adresem, ale służą one do tworzenia odpowiedzi w różnych formatach.
// Przykład: tworzenie HTML w oddzielnej metodzie
@GET
@Produces
(
"text/html;charset=UTF-8"
)
public
String
readAllHTML
()
throws
DBException
{
List
<
Product
>
products
=
allProducts
();
StringBuilder
txt
=
new
StringBuilder
(
"<!DOCTYPE html>\n<html><body>\n"
);
txt
.
append
(
"<h1>Lista produktów</h1>\n"
);
for
(
Product
product
:
products
)
{
txt
.
append
(
product
.
toHtml
()).
append
(
'\n'
);
}
txt
.
append
(
"</body></html>"
);
return
txt
.
toString
();
}
@GET
@Path
(
"/{id}"
)
@Produces
({
"application/json"
,
"application/xml"
,
"text/plain;charset=UTF-8"
})
// przykładowo /api/products/3
public
Product
oneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
...
...
@@ -31,8 +47,17 @@ public class RProducts {
}
}
@GET
@Produces
(
"text/html;charset=UTF-8"
)
@Path
(
"/{id}"
)
public
String
readOneHTML
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
Product
product
=
oneProduct
(
productId
);
return
"<!DOCTYPE html>\n<html><body>"
+
product
.
toHtml
()
+
"</body></html>"
;
}
@POST
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"application/xml"
}
)
public
void
saveProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
@@ -45,13 +70,14 @@ public class RProducts {
@GET
@Path
(
"/{id}/price"
)
// przykładowo /api/products/3/price
@Produces
({
"application/json"
,
"text/plain"
})
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
oneProduct
(
productId
).
getPrice
();
}
@PUT
@Path
(
"/{id}/price"
)
@Consumes
(
"application/json"
)
@Consumes
(
{
"application/json"
,
"text/plain"
}
)
public
void
setPrice
(
@PathParam
(
"id"
)
int
productId
,
BigDecimal
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
...
...
PC27-RestSerwer/src/main/java/sklep/rest/RProductsXml.java
View file @
f44527c9
...
...
@@ -5,7 +5,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
java.math.BigDecimal
;
import
java.util.List
;
...
...
@@ -15,10 +17,10 @@ import java.util.List;
@Consumes
(
"application/xml"
)
public
class
RProductsXml
{
@GET
public
List
<
Product
>
allProducts
()
throws
DBException
{
public
ProductList
allProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
(
);
return
new
ProductList
(
productDAO
.
readAll
()
);
}
}
...
...
@@ -43,17 +45,17 @@ public class RProductsXml {
@GET
@Path
(
"/{id}/price"
)
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
oneProduct
(
productId
).
getPrice
(
);
public
Price
getPrice
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
new
Price
(
oneProduct
(
productId
).
getPrice
()
);
}
@PUT
@Path
(
"/{id}/price"
)
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
();
}
...
...
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