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
47fe6bf7
Commit
47fe6bf7
authored
Nov 09, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Oddzielna klasa na format JSON
parent
5ae51ba3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
100 additions
and
0 deletions
+100
-0
RProductsJSON.java
PC35-RestSerwer/src/main/java/rest/RProductsJSON.java
+100
-0
No files found.
PC35-RestSerwer/src/main/java/rest/RProductsJSON.java
0 → 100644
View file @
47fe6bf7
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
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.ProductDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Product
;
@Path
(
"/products.json"
)
public
class
RProductsJSON
{
@GET
@Produces
(
"application/json"
)
public
List
<
Product
>
readAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
readAll
();
}
}
@GET
@Path
(
"/{id}"
)
@Produces
(
"application/json"
)
public
Product
readOne
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
return
productDAO
.
findById
(
productId
);
}
}
@POST
@Consumes
(
"application/json"
)
// W metodach typu POST i PUT powinien znajdować się dokładnie jeden parametr nieozanczony żadną adnotacją.
// Do tego parametru zostanie przekazana wartość utworzona na podstawie treści zapytania (content / body / entity).
// W adnotacji @Consumes określamy format, w jakim te dane mają być przysłane.
public
void
saveProduct
(
Product
product
)
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
productDAO
.
save
(
product
);
db
.
commit
();
}
}
// 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"
)
public
byte
[]
getPhoto
(
@PathParam
(
"id"
)
int
productId
)
throws
DBException
,
RecordNotFound
{
return
PhotoUtil
.
readBytes
(
productId
);
}
}
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