Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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
alx_java2b_20250412
Commits
df859ef2
Commit
df859ef2
authored
May 11, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RProductXML
parent
6a22e796
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
151 additions
and
0 deletions
+151
-0
Price.java
PC26-RestSerwer/src/main/java/sklep/model/Price.java
+33
-0
ProductList.java
PC26-RestSerwer/src/main/java/sklep/model/ProductList.java
+37
-0
RProductXML.java
PC26-RestSerwer/src/main/java/sklep/rest/RProductXML.java
+81
-0
No files found.
PC26-RestSerwer/src/main/java/sklep/model/Price.java
0 → 100644
View file @
df859ef2
package
sklep
.
model
;
import
java.math.BigDecimal
;
import
jakarta.xml.bind.annotation.XmlRootElement
;
import
jakarta.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
();
}
}
PC26-RestSerwer/src/main/java/sklep/model/ProductList.java
0 → 100644
View file @
df859ef2
package
sklep
.
model
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
jakarta.xml.bind.annotation.XmlElement
;
import
jakarta.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
();
}
}
PC26-RestSerwer/src/main/java/sklep/rest/RProductXML.java
0 → 100644
View file @
df859ef2
package
sklep
.
rest
;
import
jakarta.ws.rs.Consumes
;
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
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"
)
@Produces
(
"application/xml"
)
@Consumes
(
"application/xml"
)
public
class
RProductXML
{
@GET
public
ProductList
allProducts
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
new
ProductList
(
productDAO
.
readAll
());
}
}
@GET
@Path
(
"/{id}"
)
// przykładowo /products.xml/3
public
Product
oneProduct
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
findById
(
productId
);
}
}
@POST
public
void
saveProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
}
}
@GET
@Path
(
"/{id}/price"
)
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
,
Price
newPrice
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
product
.
setPrice
(
newPrice
.
getValue
());
productDAO
.
save
(
product
);
db
.
commit
();
}
}
@GET
@Path
(
"/{id}/photo"
)
@Produces
(
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
PhotoUtil
.
readBytes
(
productId
);
}
@PUT
@Path
(
"/{id}/photo"
)
@Consumes
(
"image/jpeg"
)
public
void
getPhoto
(
@PathParam
(
"id"
)
int
productId
,
byte
[]
bajty
)
{
PhotoUtil
.
writeBytes
(
productId
,
bajty
);
}
}
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