Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
final-exam
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nawasan Wisitsingkhon
final-exam
Commits
b8b82fe5
Commit
b8b82fe5
authored
Oct 02, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
show fav list and remove from fav
parent
5d88fd1f
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
178 additions
and
22 deletions
+178
-22
UserController.js
app/controllers/UserController.js
+2
-1
WishlistController.js
app/controllers/WishlistController.js
+2
-2
migration.sql
prisma/migrations/20231002144825_dev/migration.sql
+14
-0
schema.prisma
prisma/schema.prisma
+2
-2
login.js
src/pages/login.js
+6
-4
register.js
src/pages/register.js
+11
-13
wishlist.js
src/pages/wishlist.js
+141
-0
No files found.
app/controllers/UserController.js
View file @
b8b82fe5
...
@@ -12,7 +12,7 @@ const UserController = {
...
@@ -12,7 +12,7 @@ const UserController = {
async
index
(
req
,
res
)
{
async
index
(
req
,
res
)
{
let
userAll
=
await
db
.
user
.
findMany
();
let
userAll
=
await
db
.
user
.
findMany
();
db
.
$disconnect
();
db
.
$disconnect
();
for
(
let
i
=
0
;
i
<
userAll
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
userAll
.
length
;
i
++
)
{
delete
userAll
[
i
].
google_token
;
delete
userAll
[
i
].
google_token
;
delete
userAll
[
i
].
rank
;
delete
userAll
[
i
].
rank
;
delete
userAll
[
i
].
password
;
delete
userAll
[
i
].
password
;
...
@@ -52,6 +52,7 @@ const UserController = {
...
@@ -52,6 +52,7 @@ const UserController = {
name
,
name
,
email
,
email
,
phone
,
phone
,
address
:
""
,
photo
,
photo
,
username
,
username
,
password
,
password
,
...
...
app/controllers/WishlistController.js
View file @
b8b82fe5
...
@@ -58,8 +58,8 @@ const WishlistController = {
...
@@ -58,8 +58,8 @@ const WishlistController = {
user_id
:
Number
(
req
.
user
.
id
),
user_id
:
Number
(
req
.
user
.
id
),
},
},
});
});
if
(
!
wishlist_find
)
throw
400
;
if
(
!
wishlist_find
)
throw
400
;
await
db
.
wishlist
.
delete
({
where
:
{
id
:
wishlist_find
.
id
}})
await
db
.
wishlist
.
delete
({
where
:
{
id
:
wishlist_find
.
id
}
});
await
db
.
$disconnect
();
await
db
.
$disconnect
();
res
.
json
({
status
:
401
,
message
:
"remove fav success"
});
res
.
json
({
status
:
401
,
message
:
"remove fav success"
});
}
catch
(
err
)
{
}
catch
(
err
)
{
...
...
prisma/migrations/20231002144825_dev/migration.sql
0 → 100644
View file @
b8b82fe5
/*
Warnings:
- Added the required column `count` to the `order_detail` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER
TABLE
`order`
ADD
COLUMN
`address`
LONGTEXT
NOT
NULL
DEFAULT
''
;
-- AlterTable
ALTER
TABLE
`order_detail`
ADD
COLUMN
`count`
INTEGER
NOT
NULL
;
-- AlterTable
ALTER
TABLE
`user`
ADD
COLUMN
`address`
LONGTEXT
NOT
NULL
DEFAULT
''
;
prisma/schema.prisma
View file @
b8b82fe5
...
@@ -15,7 +15,7 @@ model user {
...
@@ -15,7 +15,7 @@ model user {
name
String
name
String
email
String
email
String
phone
String
phone
String
address
String
@
db
.
LongText
address
String
@
db
.
LongText
@
default
(
""
)
photo
String
@
db
.
LongText
photo
String
@
db
.
LongText
google_token
String
google_token
String
username
String
@
unique
username
String
@
unique
...
@@ -74,7 +74,7 @@ model order {
...
@@ -74,7 +74,7 @@ model order {
user
user
@
relation
(
fields
:
[
user_id
],
references
:
[
id
])
user
user
@
relation
(
fields
:
[
user_id
],
references
:
[
id
])
total_price
Int
total_price
Int
product_count
Int
product_count
Int
address
String
@
db
.
LongText
address
String
@
db
.
LongText
@
default
(
""
)
date
DateTime
@
default
(
now
())
date
DateTime
@
default
(
now
())
shipping_price
Int
shipping_price
Int
pay_status
Int
pay_status
Int
...
...
src/pages/login.js
View file @
b8b82fe5
import
{
Box
,
Button
,
Divider
,
Grid
,
TextField
}
from
"@mui/material"
;
import
{
Box
,
Button
,
Divider
,
Grid
,
Paper
,
TextField
}
from
"@mui/material"
;
import
{
Snackbar
}
from
"@mui/material"
;
import
{
Snackbar
}
from
"@mui/material"
;
import
React
from
"react"
;
import
React
from
"react"
;
import
{
useContext
}
from
"react"
;
import
{
useContext
}
from
"react"
;
...
@@ -20,7 +20,9 @@ export default function Login() {
...
@@ -20,7 +20,9 @@ export default function Login() {
if
(
user
.
value
?.
name
)
if
(
user
.
value
?.
name
)
return
(
return
(
<
div
>
<
div
>
<
h3
>
{
user
.
value
.
name
}
<
/h3
>
<
Paper
className
=
"m-auto"
sx
=
{{
p
:
1
,
maxWidth
:
200
}}
>
<
h3
className
=
"text-center"
>
คุณเข้าสู่ระบบแล้ว
<
/h3
>
<
/Paper
>
<
/div
>
<
/div
>
);
);
...
@@ -35,8 +37,8 @@ export default function Login() {
...
@@ -35,8 +37,8 @@ export default function Login() {
if
(
response
.
data
.
status
===
201
)
{
if
(
response
.
data
.
status
===
201
)
{
const
usrsto
=
new
userCookie
();
const
usrsto
=
new
userCookie
();
usrsto
.
store
(
response
.
data
.
token
);
usrsto
.
store
(
response
.
data
.
token
);
window
.
location
.
href
=
'/'
window
.
location
.
href
=
"/"
;
}
else
if
(
response
.
data
.
status
===
203
)
{
}
else
if
(
response
.
data
.
status
===
203
)
{
setErrorMessage
(
"ชื่อผู้ใช้หรือระหัสผ่านไม่ถูกต้อง"
);
setErrorMessage
(
"ชื่อผู้ใช้หรือระหัสผ่านไม่ถูกต้อง"
);
setTimeout
(()
=>
{
setTimeout
(()
=>
{
setErrorMessage
(
""
);
setErrorMessage
(
""
);
...
...
src/pages/register.js
View file @
b8b82fe5
...
@@ -8,8 +8,10 @@ import { Google } from "@mui/icons-material";
...
@@ -8,8 +8,10 @@ import { Google } from "@mui/icons-material";
import
{
useState
}
from
"react"
;
import
{
useState
}
from
"react"
;
import
axios
from
"axios"
;
import
axios
from
"axios"
;
import
Head
from
"next/head"
;
import
Head
from
"next/head"
;
import
{
useRouter
}
from
"next/router"
;
export
default
function
Login
()
{
export
default
function
Login
()
{
const
router
=
useRouter
();
const
[
errorMessage
,
setErrorMessage
]
=
useState
(
""
);
const
[
errorMessage
,
setErrorMessage
]
=
useState
(
""
);
const
[
successMessage
,
setSuccessMessage
]
=
useState
(
""
);
const
[
successMessage
,
setSuccessMessage
]
=
useState
(
""
);
const
user
=
useContext
(
UserContext
);
const
user
=
useContext
(
UserContext
);
...
@@ -20,13 +22,9 @@ export default function Login() {
...
@@ -20,13 +22,9 @@ export default function Login() {
const
[
username
,
setUsername
]
=
useState
(
""
);
const
[
username
,
setUsername
]
=
useState
(
""
);
const
[
password
,
setPassword
]
=
useState
(
""
);
const
[
password
,
setPassword
]
=
useState
(
""
);
if
(
user
.
value
?.
name
)
if
(
user
.
value
?.
name
)
{
return
(
router
.
push
({
pathname
:
"/"
});
<
div
>
}
<
h3
>
{
user
.
value
.
name
}
<
/h3
>
<
/div
>
);
/**
/**
*
*
* @param {FormDataEvent} e
* @param {FormDataEvent} e
...
@@ -46,12 +44,12 @@ export default function Login() {
...
@@ -46,12 +44,12 @@ export default function Login() {
setTimeout
(()
=>
{
setTimeout
(()
=>
{
setSuccessMessage
(
""
);
setSuccessMessage
(
""
);
},
3000
);
},
3000
);
setName
(
""
)
setName
(
""
)
;
setEmail
(
""
)
setEmail
(
""
)
;
setPhone
(
""
)
setPhone
(
""
)
;
setImage
(
""
)
setImage
(
""
)
;
setUsername
(
""
)
setUsername
(
""
)
;
setPassword
(
""
)
setPassword
(
""
)
;
}
else
if
(
response
.
data
.
status
===
202
)
{
}
else
if
(
response
.
data
.
status
===
202
)
{
setErrorMessage
(
"ชื่อผู้ใช้งานนี้ถูกใช้แล้ว กรุณาลองใหม่อีกครั้ง"
);
setErrorMessage
(
"ชื่อผู้ใช้งานนี้ถูกใช้แล้ว กรุณาลองใหม่อีกครั้ง"
);
setTimeout
(()
=>
{
setTimeout
(()
=>
{
...
...
src/pages/wishlist.js
0 → 100644
View file @
b8b82fe5
import
{
Box
,
Button
,
Paper
,
Table
,
TableBody
,
TableCell
,
TableHead
,
TableRow
,
}
from
"@mui/material"
;
import
React
,
{
useContext
,
useEffect
,
useState
}
from
"react"
;
import
{
UserContext
,
WishlistContext
}
from
"./_app"
;
import
{
useRouter
}
from
"next/router"
;
import
axios
from
"axios"
;
import
{
Delete
}
from
"@mui/icons-material"
;
import
{
ShoppingCart
}
from
"@mui/icons-material"
;
import
PopupAlert
from
"@/components/PopupAlert"
;
export
default
function
Wishlist
()
{
const
user
=
useContext
(
UserContext
);
const
wishlist
=
useContext
(
WishlistContext
);
const
[
products
,
setProducts
]
=
useState
([]);
const
[
message
,
setMessage
]
=
useState
({
error
:
false
,
message
:
""
});
const
router
=
useRouter
();
const
[
wishlistProduct
,
setWishlistProduct
]
=
useState
([]);
const
removeFromWishlist
=
async
(
id
)
=>
{
try
{
let
response
=
await
axios
.
delete
(
`/api/u/wishlist?id=
${
id
}
`
,
{
headers
:
{
token
:
user
.
value
.
token
},
});
fetchWishlist
();
setMessage
({
message
:
"นำออกจากรายการแล้ว"
,
error
:
false
});
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
2000
);
}
catch
(
err
)
{
console
.
log
(
err
);
}
};
const
fetchWishlist
=
async
()
=>
{
try
{
let
response
=
await
axios
.
get
(
"/api/u/wishlist"
,
{
headers
:
{
token
:
user
.
value
.
token
},
});
wishlist
.
set
(
response
.
data
.
wishlists
);
}
catch
(
err
)
{
console
.
log
(
err
);
}
};
const
fetchProduct
=
async
()
=>
{
try
{
let
product
=
await
axios
.
get
(
"/api/product"
);
setProducts
(
product
.
data
);
}
catch
(
err
)
{}
};
useEffect
(()
=>
{
fetchProduct
();
},
[]);
useEffect
(()
=>
{
console
.
log
(
wishlist
.
value
);
setWishlistProduct
(
wishlist
.
value
.
map
(
(
wish
)
=>
products
.
filter
((
pd
)
=>
pd
.
id
===
wish
.
product_id
)[
0
]
)
);
},
[
wishlist
,
products
]);
return
(
<
Box
>
<
PopupAlert
open
=
{
!!
message
.
message
.
length
}
isError
=
{
message
.
error
}
message
=
{
message
.
message
}
/
>
<
Paper
sx
=
{{
p
:
1
,
overflowX
:
"scroll"
}}
>
{
wishlist
.
value
.
length
>
0
?
(
<
Table
>
<
TableHead
>
<
TableRow
>
{[
"ลำดับ"
,
"รูปภาพ"
,
"ชื่อสินค้า"
,
"รายละเอียด"
,
"ราคา"
,
"เพิ่มลงตระกร้า"
,
"นำออกจากรายการ"
,
].
map
((
label
,
idx
)
=>
(
<
TableCell
key
=
{
idx
}
>
{
label
}
<
/TableCell
>
))}
<
/TableRow
>
<
/TableHead
>
<
TableBody
>
{
wishlistProduct
.
map
(
(
pdt
,
idx
)
=>
pdt
&&
(
<
TableRow
key
=
{
idx
}
>
<
TableCell
>
{
idx
+
1
}
<
/TableCell
>
<
TableCell
>
<
img
width
=
{
100
}
src
=
{
pdt
.
image
?.
length
?
pdt
.
image
:
"/empty.jpg"
}
alt
=
"รูปสินค้า"
/>
<
/TableCell
>
<
TableCell
>
{
pdt
.
name
}
<
/TableCell
>
<
TableCell
>
<
div
className
=
"max-h-10 overflow-scroll"
>
{
pdt
.
detail
}
<
/div
>
<
/TableCell
>
<
TableCell
>
{
pdt
.
price
}
<
/TableCell
>
<
TableCell
>
<
Button
color
=
"warning"
>
<
ShoppingCart
/>
<
/Button
>
<
/TableCell
>
<
TableCell
>
<
Button
color
=
"error"
onClick
=
{()
=>
removeFromWishlist
(
pdt
.
id
)}
>
<
Delete
/>
<
/Button
>
<
/TableCell
>
<
/TableRow
>
)
)}
<
/TableBody
>
<
/Table
>
)
:
(
<
div
className
=
"text-center"
>
รายการว่างเปล่า
<
/div
>
)}
<
/Paper
>
<
/Box
>
);
}
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