Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
f682cc91
Commit
f682cc91
authored
Nov 27, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Skopiowanie klas modelu z PC30
parent
c98c2e85
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
573 additions
and
0 deletions
+573
-0
Customer.java
PC32-RestSpring/src/main/java/sklep/model/Customer.java
+113
-0
Order.java
PC32-RestSpring/src/main/java/sklep/model/Order.java
+121
-0
OrderProduct.java
PC32-RestSpring/src/main/java/sklep/model/OrderProduct.java
+97
-0
OrderProductPK.java
...-RestSpring/src/main/java/sklep/model/OrderProductPK.java
+58
-0
Product.java
PC32-RestSpring/src/main/java/sklep/model/Product.java
+76
-0
PhotoUtil.java
PC32-RestSpring/src/main/java/sklep/photo/PhotoUtil.java
+58
-0
CustomerRepository.java
...ng/src/main/java/sklep/repository/CustomerRepository.java
+11
-0
ProductRepository.java
...ing/src/main/java/sklep/repository/ProductRepository.java
+39
-0
No files found.
PC32-RestSpring/src/main/java/sklep/model/Customer.java
0 → 100644
View file @
f682cc91
package
sklep
.
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.util.List
;
/**
* The persistent class for the customers database table.
*
*/
@Entity
@Table
(
name
=
"customers"
)
@NamedQuery
(
name
=
"Customer.findAll"
,
query
=
"SELECT c FROM Customer c"
)
public
class
Customer
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"customer_email"
)
private
String
customerEmail
;
private
String
address
;
private
String
city
;
private
String
name
;
@Column
(
name
=
"phone_number"
)
private
String
phoneNumber
;
@Column
(
name
=
"postal_code"
)
private
String
postalCode
;
//bi-directional many-to-one association to Order
@OneToMany
(
mappedBy
=
"customer"
)
private
List
<
Order
>
orders
;
public
Customer
()
{
}
public
String
getCustomerEmail
()
{
return
this
.
customerEmail
;
}
public
void
setCustomerEmail
(
String
customerEmail
)
{
this
.
customerEmail
=
customerEmail
;
}
public
String
getAddress
()
{
return
this
.
address
;
}
public
void
setAddress
(
String
address
)
{
this
.
address
=
address
;
}
public
String
getCity
()
{
return
this
.
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getName
()
{
return
this
.
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getPhoneNumber
()
{
return
this
.
phoneNumber
;
}
public
void
setPhoneNumber
(
String
phoneNumber
)
{
this
.
phoneNumber
=
phoneNumber
;
}
public
String
getPostalCode
()
{
return
this
.
postalCode
;
}
public
void
setPostalCode
(
String
postalCode
)
{
this
.
postalCode
=
postalCode
;
}
public
List
<
Order
>
getOrders
()
{
return
this
.
orders
;
}
public
void
setOrders
(
List
<
Order
>
orders
)
{
this
.
orders
=
orders
;
}
public
Order
addOrder
(
Order
order
)
{
getOrders
().
add
(
order
);
order
.
setCustomer
(
this
);
return
order
;
}
public
Order
removeOrder
(
Order
order
)
{
getOrders
().
remove
(
order
);
order
.
setCustomer
(
null
);
return
order
;
}
}
\ No newline at end of file
PC32-RestSpring/src/main/java/sklep/model/Order.java
0 → 100644
View file @
f682cc91
package
sklep
.
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.util.Date
;
import
java.sql.Timestamp
;
import
java.util.List
;
/**
* The persistent class for the orders database table.
*
*/
@Entity
@Table
(
name
=
"orders"
)
@NamedQuery
(
name
=
"Order.findAll"
,
query
=
"SELECT o FROM Order o"
)
public
class
Order
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@Column
(
name
=
"order_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
orderId
;
@Temporal
(
TemporalType
.
DATE
)
@Column
(
name
=
"delivery_date"
)
private
Date
deliveryDate
;
@Column
(
name
=
"order_date"
)
private
Timestamp
orderDate
;
@Temporal
(
TemporalType
.
DATE
)
@Column
(
name
=
"planned_delivery_date"
)
private
Date
plannedDeliveryDate
;
private
String
status
;
//bi-directional many-to-one association to OrderProduct
@OneToMany
(
mappedBy
=
"order"
)
private
List
<
OrderProduct
>
orderProducts
;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn
(
name
=
"customer_email"
)
private
Customer
customer
;
public
Order
()
{
}
public
Integer
getOrderId
()
{
return
this
.
orderId
;
}
public
void
setOrderId
(
Integer
orderId
)
{
this
.
orderId
=
orderId
;
}
public
Date
getDeliveryDate
()
{
return
this
.
deliveryDate
;
}
public
void
setDeliveryDate
(
Date
deliveryDate
)
{
this
.
deliveryDate
=
deliveryDate
;
}
public
Timestamp
getOrderDate
()
{
return
this
.
orderDate
;
}
public
void
setOrderDate
(
Timestamp
orderDate
)
{
this
.
orderDate
=
orderDate
;
}
public
Date
getPlannedDeliveryDate
()
{
return
this
.
plannedDeliveryDate
;
}
public
void
setPlannedDeliveryDate
(
Date
plannedDeliveryDate
)
{
this
.
plannedDeliveryDate
=
plannedDeliveryDate
;
}
public
String
getStatus
()
{
return
this
.
status
;
}
public
void
setStatus
(
String
status
)
{
this
.
status
=
status
;
}
public
List
<
OrderProduct
>
getOrderProducts
()
{
return
this
.
orderProducts
;
}
public
void
setOrderProducts
(
List
<
OrderProduct
>
orderProducts
)
{
this
.
orderProducts
=
orderProducts
;
}
public
OrderProduct
addOrderProduct
(
OrderProduct
orderProduct
)
{
getOrderProducts
().
add
(
orderProduct
);
orderProduct
.
setOrder
(
this
);
return
orderProduct
;
}
public
OrderProduct
removeOrderProduct
(
OrderProduct
orderProduct
)
{
getOrderProducts
().
remove
(
orderProduct
);
orderProduct
.
setOrder
(
null
);
return
orderProduct
;
}
public
Customer
getCustomer
()
{
return
this
.
customer
;
}
public
void
setCustomer
(
Customer
customer
)
{
this
.
customer
=
customer
;
}
}
\ No newline at end of file
PC32-RestSpring/src/main/java/sklep/model/OrderProduct.java
0 → 100644
View file @
f682cc91
package
sklep
.
model
;
import
java.io.Serializable
;
import
java.math.BigDecimal
;
import
javax.persistence.Column
;
import
javax.persistence.EmbeddedId
;
import
javax.persistence.Entity
;
import
javax.persistence.JoinColumn
;
import
javax.persistence.ManyToOne
;
import
javax.persistence.NamedQuery
;
import
javax.persistence.Table
;
/**
* The persistent class for the order_products database table.
*
*/
@Entity
@Table
(
name
=
"order_products"
)
@NamedQuery
(
name
=
"OrderProduct.findAll"
,
query
=
"SELECT o FROM OrderProduct o"
)
public
class
OrderProduct
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@EmbeddedId
private
OrderProductPK
id
;
@Column
(
name
=
"actual_price"
)
private
BigDecimal
actualPrice
;
@Column
(
name
=
"actual_vat"
)
private
BigDecimal
actualVat
;
private
Integer
quantity
;
//bi-directional many-to-one association to Order
@ManyToOne
@JoinColumn
(
name
=
"order_id"
,
insertable
=
false
,
updatable
=
false
)
private
Order
order
;
//uni-directional many-to-one association to Product
@ManyToOne
@JoinColumn
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Product
product
;
public
OrderProduct
()
{
}
public
OrderProductPK
getId
()
{
return
this
.
id
;
}
public
void
setId
(
OrderProductPK
id
)
{
this
.
id
=
id
;
}
public
BigDecimal
getActualPrice
()
{
return
this
.
actualPrice
;
}
public
void
setActualPrice
(
BigDecimal
actualPrice
)
{
this
.
actualPrice
=
actualPrice
;
}
public
BigDecimal
getActualVat
()
{
return
this
.
actualVat
;
}
public
void
setActualVat
(
BigDecimal
actualVat
)
{
this
.
actualVat
=
actualVat
;
}
public
Integer
getQuantity
()
{
return
this
.
quantity
;
}
public
void
setQuantity
(
Integer
quantity
)
{
this
.
quantity
=
quantity
;
}
public
Order
getOrder
()
{
return
this
.
order
;
}
public
void
setOrder
(
Order
order
)
{
this
.
order
=
order
;
}
public
Product
getProduct
()
{
return
this
.
product
;
}
public
void
setProduct
(
Product
product
)
{
this
.
product
=
product
;
}
}
\ No newline at end of file
PC32-RestSpring/src/main/java/sklep/model/OrderProductPK.java
0 → 100644
View file @
f682cc91
package
sklep
.
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
/**
* The primary key class for the order_products database table.
*
*/
@Embeddable
public
class
OrderProductPK
implements
Serializable
{
//default serial version id, required for serializable classes.
private
static
final
long
serialVersionUID
=
1L
;
@Column
(
name
=
"order_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
orderId
;
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
productId
;
public
OrderProductPK
()
{
}
public
Integer
getOrderId
()
{
return
this
.
orderId
;
}
public
void
setOrderId
(
Integer
orderId
)
{
this
.
orderId
=
orderId
;
}
public
Integer
getProductId
()
{
return
this
.
productId
;
}
public
void
setProductId
(
Integer
productId
)
{
this
.
productId
=
productId
;
}
public
boolean
equals
(
Object
other
)
{
if
(
this
==
other
)
{
return
true
;
}
if
(!(
other
instanceof
OrderProductPK
))
{
return
false
;
}
OrderProductPK
castOther
=
(
OrderProductPK
)
other
;
return
this
.
orderId
.
equals
(
castOther
.
orderId
)
&&
this
.
productId
.
equals
(
castOther
.
productId
);
}
public
int
hashCode
()
{
final
int
prime
=
31
;
int
hash
=
17
;
hash
=
hash
*
prime
+
this
.
orderId
.
hashCode
();
hash
=
hash
*
prime
+
this
.
productId
.
hashCode
();
return
hash
;
}
}
\ No newline at end of file
PC32-RestSpring/src/main/java/sklep/model/Product.java
0 → 100644
View file @
f682cc91
package
sklep
.
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
/**
* The persistent class for the products database table.
*
*/
@Entity
@Table
(
name
=
"products"
)
@NamedQuery
(
name
=
"Product.findAll"
,
query
=
"SELECT p FROM Product p"
)
public
class
Product
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
productId
;
private
String
description
;
private
BigDecimal
price
;
@Column
(
name
=
"product_name"
)
private
String
productName
;
private
BigDecimal
vat
;
public
Product
()
{
}
public
Integer
getProductId
()
{
return
this
.
productId
;
}
public
void
setProductId
(
Integer
productId
)
{
this
.
productId
=
productId
;
}
public
String
getDescription
()
{
return
this
.
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
public
BigDecimal
getPrice
()
{
return
this
.
price
;
}
public
void
setPrice
(
BigDecimal
price
)
{
this
.
price
=
price
;
}
public
String
getProductName
()
{
return
this
.
productName
;
}
public
void
setProductName
(
String
productName
)
{
this
.
productName
=
productName
;
}
public
BigDecimal
getVat
()
{
return
this
.
vat
;
}
public
void
setVat
(
BigDecimal
vat
)
{
this
.
vat
=
vat
;
}
}
\ No newline at end of file
PC32-RestSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
f682cc91
package
sklep
.
photo
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
@Component
public
class
PhotoUtil
{
@Value
(
"${alx.photo_dir}"
)
private
String
photoDir
;
private
static
final
String
EXT
=
".jpg"
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
// System.err.println(e);
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
}
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
photoDir
,
fileName
);
}
}
PC32-RestSpring/src/main/java/sklep/repository/CustomerRepository.java
0 → 100644
View file @
f682cc91
package
sklep
.
repository
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
import
sklep.model.Customer
;
@Repository
public
interface
CustomerRepository
extends
JpaRepository
<
Customer
,
String
>
{
}
PC32-RestSpring/src/main/java/sklep/repository/ProductRepository.java
0 → 100644
View file @
f682cc91
package
sklep
.
repository
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
import
sklep.model.Product
;
/* Gdy w projekcie umieścimy interfejs rozszerzający interfejs JpaRepository (albo podobny)
* i oznaczymy go adnotacją @Repository (albo skonfigurujemy w inny sposób...),
* to Spring AUTOMATYCZNIE UTWORZY IMPLEMENTACJĘ tego interfejsu.
* Dzięki temu "za darmo" mamy metody dający podstawowy dostęp do tabel.
* Dodatkowo w interfejsie można dopisać własne metody, w których nazwie kryje się zasada działania.
* Np. findByPriceBetween szuka produktów o cenie między min i max.
*
* findByEmail - szuka rekordów z polem email równym parametrowi.
*
* https://www.baeldung.com/spring-data-derived-queries
*/
@Repository
public
interface
ProductRepository
extends
JpaRepository
<
Product
,
Integer
>
{
List
<
Product
>
findByProductName
(
String
name
);
List
<
Product
>
findByProductNameContains
(
String
name
);
List
<
Product
>
findByProductNameContainingIgnoringCase
(
String
name
);
List
<
Product
>
findByPriceBetween
(
BigDecimal
min
,
BigDecimal
max
);
List
<
Product
>
findByProductNameContainingIgnoringCaseAndPriceBetween
(
String
name
,
BigDecimal
min
,
BigDecimal
max
);
}
\ 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