Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
stadler2
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
stadler2
Commits
2c14a20d
Commit
2c14a20d
authored
Dec 10, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wersja JPA - odczyt i (prosty) zapis
parent
949ad06d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
1 deletions
+82
-1
ProductRepository.java
quarkus2/src/main/java/sklep/repo/ProductRepository.java
+9
-0
DProducts.java
quarkus2/src/main/java/sklep/rest/DProducts.java
+7
-1
EProducts.java
quarkus2/src/main/java/sklep/rest/EProducts.java
+66
-0
No files found.
quarkus2/src/main/java/sklep/repo/ProductRepository.java
0 → 100644
View file @
2c14a20d
package
sklep
.
repo
;
import
io.quarkus.hibernate.orm.panache.PanacheRepository
;
import
jakarta.enterprise.context.ApplicationScoped
;
import
sklep.model_jpa.Product
;
@ApplicationScoped
public
class
ProductRepository
implements
PanacheRepository
<
Product
>
{
}
quarkus2/src/main/java/sklep/rest/DProducts.java
View file @
2c14a20d
...
...
@@ -3,6 +3,7 @@ package sklep.rest;
import
jakarta.inject.Inject
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.TypedQuery
;
import
jakarta.transaction.Transactional
;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.core.MediaType
;
import
sklep.model_jpa.Product
;
...
...
@@ -22,6 +23,7 @@ public class DProducts {
}
@Path
(
"/{id}"
)
@GET
public
Product
oneProduct
(
@PathParam
(
"id"
)
Integer
id
)
{
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
==
null
)
{
...
...
@@ -32,9 +34,12 @@ public class DProducts {
@POST
@Consumes
(
MediaType
.
APPLICATION_JSON
)
public
void
addProduct
(
Product
product
)
{
@Transactional
public
Product
addProduct
(
Product
product
)
{
em
.
persist
(
product
);
em
.
flush
();
// odsyłam uzupełniony obiekt produktu, aby klient poznał ID, pod jakim został zapisany
return
product
;
}
}
\ No newline at end of file
quarkus2/src/main/java/sklep/rest/EProducts.java
0 → 100644
View file @
2c14a20d
package
sklep
.
rest
;
import
io.quarkus.panache.common.Parameters
;
import
jakarta.inject.Inject
;
import
jakarta.transaction.Transactional
;
import
jakarta.ws.rs.*
;
import
jakarta.ws.rs.core.MediaType
;
import
jakarta.ws.rs.core.Response
;
import
jakarta.ws.rs.core.UriBuilder
;
import
sklep.model_jpa.Product
;
import
sklep.repo.ProductRepository
;
import
java.math.BigDecimal
;
import
java.net.URI
;
import
java.util.List
;
@Path
(
"/e/products"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
EProducts
{
@Inject
ProductRepository
repository
;
@GET
public
List
<
Product
>
allProducts
()
{
return
repository
.
listAll
();
}
@Path
(
"/{id}"
)
@GET
public
Product
oneProduct
(
@PathParam
(
"id"
)
Long
id
)
{
return
repository
.
findByIdOptional
(
id
)
.
orElseThrow
(()
->
new
WebApplicationException
(
"Nie ma takiego produktu"
,
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
type
(
MediaType
.
TEXT_HTML
)
.
entity
(
"<p style='color: red'>Nie ma produktu nr "
+
id
+
"</p>"
)
.
build
()));
}
@POST
@Consumes
(
MediaType
.
APPLICATION_JSON
)
@Transactional
public
Response
addProduct
(
Product
product
)
{
repository
.
persist
(
product
);
URI
uri
=
UriBuilder
.
fromResource
(
EProducts
.
class
).
path
(
"/{id}"
).
build
(
product
.
getId
());
return
Response
.
created
(
uri
).
build
();
}
@Path
(
"/{id}/price"
)
@GET
public
BigDecimal
getPrice
(
@PathParam
(
"id"
)
Long
id
)
{
return
oneProduct
(
id
).
getPrice
();
}
@Path
(
"/{id}/price"
)
@PUT
@Consumes
({
MediaType
.
APPLICATION_JSON
,
MediaType
.
TEXT_PLAIN
})
@Transactional
public
void
setPrice
(
@PathParam
(
"id"
)
Long
id
,
BigDecimal
price
)
{
//repository.update("update Product p set p.price = ?1 where p.id = ?2", price, id);
repository
.
update
(
"update Product p set p.price = :price where p.id = :id"
,
Parameters
.
with
(
"price"
,
price
).
and
(
"id"
,
id
));
}
}
\ No newline at end of file
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