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
7c80fed7
Commit
7c80fed7
authored
Nov 08, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ROrders i RCustomers
parent
8f73e9ab
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
0 deletions
+83
-0
RCustomers.java
PC35-RestSerwer/src/main/java/rest/RCustomers.java
+36
-0
ROrders.java
PC35-RestSerwer/src/main/java/rest/ROrders.java
+47
-0
No files found.
PC35-RestSerwer/src/main/java/rest/RCustomers.java
0 → 100644
View file @
7c80fed7
package
rest
;
import
java.util.List
;
import
jakarta.ws.rs.GET
;
import
jakarta.ws.rs.Path
;
import
jakarta.ws.rs.PathParam
;
import
jakarta.ws.rs.Produces
;
import
jakarta.ws.rs.core.MediaType
;
import
sklep.db.CustomerDAO
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Customer
;
@Path
(
"/customers"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
RCustomers
{
@GET
@Path
(
"/{id}"
)
public
Customer
findById
(
@PathParam
(
"id"
)
final
String
email
)
throws
DBException
,
RecordNotFound
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
return
customerDAO
.
findByEmail
(
email
);
}
}
@GET
public
List
<
Customer
>
listAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
CustomerDAO
customerDAO
=
db
.
customerDAO
();
return
customerDAO
.
readAll
();
}
}
}
PC35-RestSerwer/src/main/java/rest/ROrders.java
0 → 100644
View file @
7c80fed7
package
rest
;
import
java.util.List
;
import
jakarta.ws.rs.GET
;
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.Response.Status
;
import
sklep.db.DBConnection
;
import
sklep.db.DBException
;
import
sklep.db.OrderDAO
;
import
sklep.db.RecordNotFound
;
import
sklep.model.Order
;
@Path
(
"/orders"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
ROrders
{
@GET
public
List
<
Order
>
listAll
()
throws
DBException
{
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
return
orderDAO
.
readAll
();
}
}
@GET
@Path
(
"/{id:[0-9]+}"
)
public
Response
findById
(
@PathParam
(
"id"
)
final
Integer
id
)
{
// Klasa Response pozwala nam z pełną precyzją przygotować odpowiedź, która ma zostać odesłana klientowi.
// W przypadku pozytywnym (ok) zostanie odesłany obiekt przetłumaczony na XML lub JSON, a kod wynikowy to będzie 200.
// Ale w przypadku błędów możemy sami zdecydować co odsyłami (tutaj odpowiednie kody HTTP).
try
(
DBConnection
db
=
DBConnection
.
open
())
{
OrderDAO
orderDAO
=
db
.
orderDAO
();
Order
order
=
orderDAO
.
findById
(
id
);
return
Response
.
ok
(
order
).
build
();
}
catch
(
DBException
e
)
{
e
.
printStackTrace
();
return
Response
.
status
(
Status
.
INTERNAL_SERVER_ERROR
).
build
();
}
catch
(
RecordNotFound
e
)
{
return
Response
.
status
(
Status
.
NOT_FOUND
).
build
();
}
}
}
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