Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
kroupja
TWA_Projekt1
Commits
234aecdb
Commit
234aecdb
authored
1 year ago
by
kroupja
Browse files
Options
Download
Email Patches
Plain Diff
odevzdani (nejde db i guess)
parent
408a67e8
master
No related merge requests found
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
package-lock.json
+23
-0
package-lock.json
package.json
+1
-0
package.json
src/controllers/orderController.ts
+4
-4
src/controllers/orderController.ts
src/controllers/product.controller.ts
+7
-6
src/controllers/product.controller.ts
src/controllers/userController.ts
+3
-3
src/controllers/userController.ts
src/frontend/admin-script.js
+2
-2
src/frontend/admin-script.js
src/frontend/backendConnection.js
+4
-7
src/frontend/backendConnection.js
src/frontend/checkout.js
+1
-1
src/frontend/checkout.js
src/frontend/login-script.js
+1
-1
src/frontend/login-script.js
src/frontend/register-script.js
+1
-1
src/frontend/register-script.js
src/pages/index.html
+0
-15
src/pages/index.html
src/pages/login.html
+0
-3
src/pages/login.html
src/routes/index.ts
+1
-3
src/routes/index.ts
src/services/mysqlConnection.ts
+0
-1
src/services/mysqlConnection.ts
src/services/userService.ts
+3
-3
src/services/userService.ts
with
51 additions
and
50 deletions
+51
-50
package-lock.json
+
23
-
0
View file @
234aecdb
...
...
@@ -20,6 +20,7 @@
"morgan"
:
"^1.10.0"
,
"mysql"
:
"^2.18.1"
,
"mysql2"
:
"^3.7.0"
,
"sql"
:
"^0.78.0"
,
"ts-jest"
:
"^29.1.1"
,
"typescript"
:
"^5.3.3"
},
...
...
@@ -4843,6 +4844,11 @@
"url"
:
"https://github.com/sponsors/sindresorhus"
}
},
"node_modules/lodash"
:
{
"version"
:
"4.1.0"
,
"resolved"
:
"https://registry.npmjs.org/lodash/-/lodash-4.1.0.tgz"
,
"integrity"
:
"sha512-B9sgtKUlz0xe7lkYb80BcOpwwJJw5iOiz4HkBDzF0+i5nJLiwfBnL08m7bBkCOPBfi+0aqvrJDMdZDfAvs8vYg=="
},
"node_modules/lodash.memoize"
:
{
"version"
:
"4.1.2"
,
"resolved"
:
"https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz"
,
...
...
@@ -6132,6 +6138,11 @@
"node"
:
">=8"
}
},
"node_modules/sliced"
:
{
"version"
:
"0.0.5"
,
"resolved"
:
"https://registry.npmjs.org/sliced/-/sliced-0.0.5.tgz"
,
"integrity"
:
"sha512-9bYT917D6H3+q8GlQBJmLVz3bc4OeVGfZ2BB12wvLnluTGfG6/8UdOUbKJDW1EEx9SZMDbjnatkau5/XcUeyOw=="
},
"node_modules/source-map"
:
{
"version"
:
"0.6.1"
,
"resolved"
:
"https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz"
,
...
...
@@ -6154,6 +6165,18 @@
"resolved"
:
"https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz"
,
"integrity"
:
"sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g=="
},
"node_modules/sql"
:
{
"version"
:
"0.78.0"
,
"resolved"
:
"https://registry.npmjs.org/sql/-/sql-0.78.0.tgz"
,
"integrity"
:
"sha512-+If27aF3UQxmnuF5JgTJKd/X6TiELa1f58Zo5TX9ULbPnqs1fBDODwwbtU0WwM5H3lSiwx2En4m2lB1rCoIhvQ=="
,
"dependencies"
:
{
"lodash"
:
"4.1.x"
,
"sliced"
:
"0.0.x"
},
"engines"
:
{
"node"
:
"*"
}
},
"node_modules/sqlstring"
:
{
"version"
:
"2.3.1"
,
"resolved"
:
"https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz"
,
...
...
This diff is collapsed.
Click to expand it.
package.json
+
1
-
0
View file @
234aecdb
...
...
@@ -25,6 +25,7 @@
"
morgan
"
:
"
^1.10.0
"
,
"
mysql
"
:
"
^2.18.1
"
,
"
mysql2
"
:
"
^3.7.0
"
,
"
sql
"
:
"
^0.78.0
"
,
"
ts-jest
"
:
"
^29.1.1
"
,
"
typescript
"
:
"
^5.3.3
"
},
...
...
This diff is collapsed.
Click to expand it.
src/controllers/orderController.ts
+
4
-
4
View file @
234aecdb
import
{
Request
,
Response
}
from
'
express
'
;
import
mysqlC
onnection
from
'
../services/mysqlConnection
'
;
import
c
onnection
from
'
../services/mysqlConnection
'
;
class
OrderController
{
static
async
createOrder
(
req
:
Request
,
res
:
Response
)
{
const
{
user_id
,
order_date
,
status
}:
{
user_id
:
number
;
order_date
:
string
;
status
:
string
}
=
req
.
body
;
try
{
await
mysqlC
onnection
.
query
(
'
INSERT INTO E_Orders (user_id, order_date, status) VALUES (?, ?, ?)
'
,
[
user_id
,
order_date
,
status
]);
await
c
onnection
.
query
(
'
INSERT INTO E_Orders (user_id, order_date, status) VALUES (?, ?, ?)
'
,
[
user_id
,
order_date
,
status
]);
res
.
status
(
201
).
json
({
message
:
'
Order created successfully
'
});
}
catch
(
error
)
{
...
...
@@ -16,7 +16,7 @@ class OrderController {
static
async
getAllOrders
(
req
:
Request
,
res
:
Response
)
{
try
{
const
orderList
=
await
mysqlC
onnection
.
query
(
'
SELECT * FROM E_Orders
'
);
const
orderList
=
await
c
onnection
.
query
(
'
SELECT * FROM E_Orders
'
);
res
.
status
(
200
).
json
(
orderList
);
}
catch
(
error
)
{
...
...
@@ -28,7 +28,7 @@ class OrderController {
const
orderId
:
number
=
parseInt
(
req
.
params
.
orderId
);
try
{
const
order
=
await
mysqlC
onnection
.
query
(
'
SELECT * FROM E_Orders WHERE order_id = ?
'
,
[
orderId
]);
const
order
=
await
c
onnection
.
query
(
'
SELECT * FROM E_Orders WHERE order_id = ?
'
,
[
orderId
]);
if
(
order
.
length
===
0
)
{
res
.
status
(
404
).
json
({
message
:
'
Order not found
'
});
...
...
This diff is collapsed.
Click to expand it.
src/controllers/product.controller.ts
+
7
-
6
View file @
234aecdb
import
{
Product
}
from
'
../models/product.model
'
;
import
mysqlConnection
from
'
../services/mysqlConnection
'
;
import
connection
from
'
../services/mysqlConnection
'
;
import
mysql
from
'
mysql
'
;
export
const
ProductService
=
{
getAllProducts
:
async
():
Promise
<
Product
[]
>
=>
{
const
products
:
Product
[]
=
await
mysqlC
onnection
.
query
(
'
SELECT * FROM E_Products
'
);
const
products
:
Product
[]
=
await
c
onnection
.
query
(
'
SELECT * FROM E_Products
'
);
return
products
;
},
getProductById
:
async
(
productId
:
string
):
Promise
<
Product
|
undefined
>
=>
{
const
product
:
Product
|
undefined
=
await
mysqlC
onnection
.
query
(
'
SELECT * FROM E_Products WHERE product_id = ?
'
,
[
productId
]);
const
product
:
Product
|
undefined
=
await
c
onnection
.
query
(
'
SELECT * FROM E_Products WHERE product_id = ?
'
,
[
productId
]);
return
product
;
},
addProduct
:
async
(
newProduct
:
Product
):
Promise
<
void
>
=>
{
await
mysqlC
onnection
.
query
(
'
INSERT INTO E_Products (product_id, name, price, imageUrl) VALUES (?, ?, ?, ?)
'
,
[
newProduct
.
product_id
,
newProduct
.
name
,
newProduct
.
price
,
newProduct
.
imageUrl
]);
await
c
onnection
.
query
(
'
INSERT INTO E_Products (product_id, name, price, imageUrl) VALUES (?, ?, ?, ?)
'
,
[
newProduct
.
product_id
,
newProduct
.
name
,
newProduct
.
price
,
newProduct
.
imageUrl
]);
},
updateProduct
:
async
(
productId
:
string
,
updatedProduct
:
Product
):
Promise
<
void
>
=>
{
await
mysqlC
onnection
.
query
(
'
UPDATE E_Products SET name = ?, price = ?, imageUrl = ? WHERE product_id = ?
'
,
[
updatedProduct
.
name
,
updatedProduct
.
price
,
updatedProduct
.
imageUrl
,
productId
]);
await
c
onnection
.
query
(
'
UPDATE E_Products SET name = ?, price = ?, imageUrl = ? WHERE product_id = ?
'
,
[
updatedProduct
.
name
,
updatedProduct
.
price
,
updatedProduct
.
imageUrl
,
productId
]);
},
deleteProduct
:
async
(
productId
:
string
):
Promise
<
void
>
=>
{
await
mysqlC
onnection
.
query
(
'
DELETE FROM E_Products WHERE product_id = ?
'
,
[
productId
]);
await
c
onnection
.
query
(
'
DELETE FROM E_Products WHERE product_id = ?
'
,
[
productId
]);
},
};
This diff is collapsed.
Click to expand it.
src/controllers/userController.ts
+
3
-
3
View file @
234aecdb
import
{
Request
,
Response
}
from
'
express
'
;
import
bcrypt
from
'
bcrypt
'
;
import
mysqlC
onnection
from
'
../services/mysqlConnection
'
;
import
c
onnection
from
'
../services/mysqlConnection
'
;
class
UserController
{
static
async
login
(
req
:
Request
,
res
:
Response
)
{
const
{
username
,
password
}:
{
username
:
string
;
password
:
string
}
=
req
.
body
;
try
{
const
user
=
await
mysqlC
onnection
.
query
(
'
SELECT * FROM E_Users WHERE username = ?
'
,
[
username
]);
const
user
=
await
c
onnection
.
query
(
'
SELECT * FROM E_Users WHERE username = ?
'
,
[
username
]);
if
(
user
.
length
===
0
)
{
res
.
status
(
401
).
json
({
message
:
'
Invalid credentials
'
});
...
...
@@ -33,7 +33,7 @@ class UserController {
try
{
const
hashedPassword
=
await
bcrypt
.
hash
(
password
,
10
);
await
mysqlC
onnection
.
query
(
'
INSERT INTO E_Users (username, password) VALUES (?, ?)
'
,
[
username
,
hashedPassword
]);
await
c
onnection
.
query
(
'
INSERT INTO E_Users (username, password) VALUES (?, ?)
'
,
[
username
,
hashedPassword
]);
res
.
status
(
201
).
json
({
message
:
'
User registered successfully
'
});
}
catch
(
error
)
{
...
...
This diff is collapsed.
Click to expand it.
src/frontend/admin-script.js
+
2
-
2
View file @
234aecdb
async
function
fetchAllProducts
()
{
try
{
const
response
=
await
fetch
(
'
/products
'
);
// Upozorňuji, že cesta '/api/products' závisí na implementaci vašeho backendu
const
response
=
await
fetch
(
'
http://localhost:3000
/products
'
);
// Upozorňuji, že cesta '/api/products' závisí na implementaci vašeho backendu
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Network response was not ok
'
);
}
...
...
@@ -15,7 +15,7 @@ async function fetchAllProducts() {
// načtení všech objednávek
async
function
fetchAllOrders
()
{
try
{
const
response
=
await
fetch
(
'
/orders
'
);
// Upozorňuji, že cesta '/api/orders' závisí na implementaci vašeho backendu
const
response
=
await
fetch
(
'
http://localhost:3000
/orders
'
);
// Upozorňuji, že cesta '/api/orders' závisí na implementaci vašeho backendu
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Network response was not ok
'
);
}
...
...
This diff is collapsed.
Click to expand it.
src/frontend/backendConnection.js
+
4
-
7
View file @
234aecdb
async
function
fetchProducts
()
{
try
{
const
response
=
await
fetch
(
'
/products
'
);
// Úprava cesty z '/api/products' na '
/products'
const
response
=
await
(
'
http://localhost:3000
/products
'
);
if
(
!
response
.
ok
)
{
throw
new
Error
(
'
Network response was not ok
'
);
}
...
...
@@ -15,7 +15,7 @@ async function fetchProducts() {
// Funkce pro odeslání objednávky na server
async
function
sendOrder
(
orderData
)
{
try
{
const
response
=
await
fetch
(
'
/orders
'
,
{
const
response
=
await
fetch
(
'
http://localhost:3000
/orders
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
...
...
@@ -36,7 +36,7 @@ async function sendOrder(orderData) {
// Funkce pro registraci uživatele
async
function
registerUser
(
userData
)
{
try
{
const
response
=
await
fetch
(
'
/register
'
,
{
const
response
=
await
fetch
(
'
http://localhost:3000
/register
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
...
...
@@ -57,7 +57,7 @@ async function registerUser(userData) {
// Funkce pro přihlášení uživatele
async
function
loginUser
(
userData
)
{
try
{
const
response
=
await
fetch
(
'
/login
'
,
{
const
response
=
await
fetch
(
'
http://localhost:3000
/login
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
...
...
@@ -75,9 +75,6 @@ async function loginUser(userData) {
}
}
// Další funkce pro práci s uživateli, produkty a objednávkami...
// Zaregistrování funkcí na objektu window
window
.
backendConnection
=
{
fetchProducts
,
sendOrder
,
...
...
This diff is collapsed.
Click to expand it.
src/frontend/checkout.js
+
1
-
1
View file @
234aecdb
...
...
@@ -42,7 +42,7 @@ async function handleSubmit(event) {
};
try
{
const
response
=
await
axios
.
post
(
'
/orders
'
,
orderData
);
const
response
=
await
axios
.
post
(
'
http://localhost:3000
/orders
'
,
orderData
);
if
(
response
.
status
===
201
)
{
alert
(
'
Objednávka byla úspěšně vytvořena!
'
);
window
.
location
.
href
=
'
/index.html
'
;
// Přesměrování na index.html po úspěšném vytvoření objednávky
...
...
This diff is collapsed.
Click to expand it.
src/frontend/login-script.js
+
1
-
1
View file @
234aecdb
...
...
@@ -4,7 +4,7 @@ document.getElementById('login-form').addEventListener('submit', async function(
const
username
=
document
.
getElementById
(
'
username
'
).
value
;
const
password
=
document
.
getElementById
(
'
password
'
).
value
;
const
response
=
await
fetch
(
'
/login
'
,
{
const
response
=
await
fetch
(
'
http://localhost:3000
/login
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
...
...
This diff is collapsed.
Click to expand it.
src/frontend/register-script.js
+
1
-
1
View file @
234aecdb
...
...
@@ -4,7 +4,7 @@ document.getElementById('register-form').addEventListener('submit', async functi
const
username
=
document
.
getElementById
(
'
username
'
).
value
;
const
password
=
document
.
getElementById
(
'
password
'
).
value
;
const
response
=
await
fetch
(
'
/register
'
,
{
const
response
=
await
fetch
(
'
http://localhost:3000
/register
'
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
...
...
This diff is collapsed.
Click to expand it.
src/pages/index.html
+
0
-
15
View file @
234aecdb
...
...
@@ -36,21 +36,6 @@
</div>
</div>
</main>
<script>
async
function
fetchProducts
()
{
const
response
=
await
fetch
(
'
/products
'
);
const
products
=
await
response
.
json
();
const
productList
=
document
.
getElementById
(
'
productList
'
);
products
.
forEach
(
product
=>
{
const
listItem
=
document
.
createElement
(
'
li
'
);
listItem
.
textContent
=
`
${
product
.
product_id
}
-
${
product
.
name
}
- $
${
product
.
price
}
`
;
productList
.
appendChild
(
listItem
);
});
}
fetchProducts
();
</script>
<script
src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
></script>
<script
src=
"/resources/backendConnection.js"
></script>
...
...
This diff is collapsed.
Click to expand it.
src/pages/login.html
+
0
-
3
View file @
234aecdb
...
...
@@ -5,15 +5,12 @@
<title>
Login - Žlabshop
</title>
<link
href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
rel=
"stylesheet"
>
<link
href=
"styles.css"
rel=
"stylesheet"
>
</head>
<body>
<header>
</header>
<main>
<div
class=
"container mt-5"
>
<div
class=
"row"
>
...
...
This diff is collapsed.
Click to expand it.
src/routes/index.ts
+
1
-
3
View file @
234aecdb
...
...
@@ -44,9 +44,7 @@ app.get('/error.middleware.ts', (req, res) => {
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
res
.
sendFile
(
path
.
join
(
__dirname
,
'
../pages/index.html
'
));
});
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
res
.
sendFile
(
path
.
join
(
__dirname
,
'
../pages/index.html
'
));
});
app
.
use
((
req
,
res
,
next
)
=>
{
res
.
status
(
404
).
sendFile
(
path
.
join
(
__dirname
,
'
../pages/404.html
'
));
...
...
This diff is collapsed.
Click to expand it.
src/services/mysqlConnection.ts
+
0
-
1
View file @
234aecdb
...
...
@@ -2,7 +2,6 @@ import mysql from 'mysql';
// Konfigurace připojení k MySQL databázi
const
connection
=
mysql
.
createConnection
({
host
:
'
lab.uzlabina.cz
'
,
// Adresa serveru, na kterém běží MySQL
port
:
3306
,
user
:
'
kroupja
'
,
// Uživatelské jméno pro přihlášení k MySQL
...
...
This diff is collapsed.
Click to expand it.
src/services/userService.ts
+
3
-
3
View file @
234aecdb
import
{
IUser
}
from
'
../models/user.model
'
;
import
db
from
'
./mysqlConnection
'
;
import
connection
from
'
./mysqlConnection
'
;
export
const
UserService
=
{
getAllUsers
:
async
():
Promise
<
IUser
[]
>
=>
{
const
[
rows
]
=
await
db
.
query
<
IUser
[]
>
(
'
SELECT * FROM E_Users
'
);
const
[
rows
]
=
await
connection
.
query
<
IUser
[]
>
(
'
SELECT * FROM E_Users
'
);
return
rows
;
},
addUser
:
async
(
newUser
:
IUser
):
Promise
<
IUser
>
=>
{
const
[
result
]
=
await
db
.
query
(
'
INSERT INTO E_Users SET ?
'
,
[
newUser
]);
const
[
result
]
=
await
connection
.
query
(
'
INSERT INTO E_Users SET ?
'
,
[
newUser
]);
const
insertedUserId
=
result
.
insertId
;
return
{
...
newUser
,
user_id
:
insertedUserId
};
},
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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
Menu
Explore
Projects
Groups
Snippets