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
4541bf7c
Commit
4541bf7c
authored
Oct 01, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add category from admin;
parent
0bca33ff
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
128 additions
and
4 deletions
+128
-4
CategoryController.js
app/controllers/CategoryController.js
+24
-1
admin.js
app/routes/admin.js
+4
-3
AddCategory.js
src/components/AddCategory.js
+82
-0
category.js
src/pages/admin/category.js
+18
-0
No files found.
app/controllers/CategoryController.js
View file @
4541bf7c
...
...
@@ -7,10 +7,33 @@ const CategoryController = {
* @param {Response} res
*/
async
index
(
req
,
res
)
{
let
category
=
await
db
.
category
.
findMany
({
orderBy
:
{
id
:
"asc"
}
});
let
category
=
await
db
.
category
.
findMany
({
orderBy
:
{
id
:
"asc"
}
});
await
db
.
$disconnect
();
res
.
json
(
category
);
},
/**
*
* @param {Request} req
* @param {Response} res
*/
async
create
(
req
,
res
)
{
try
{
const
{
name
}
=
req
.
body
;
let
check_cate
=
await
db
.
category
.
findFirst
({
where
:
{
name
}
});
if
(
check_cate
)
throw
202
;
await
db
.
category
.
create
({
data
:
{
name
}
});
return
res
.
json
({
status
:
201
,
message
:
"create success"
});
}
catch
(
err
)
{
if
(
err
===
202
)
return
res
.
json
({
status
:
202
,
message
:
"this category has already used"
,
});
else
{
res
.
json
({
status
:
200
,
message
:
"some error on server"
});
}
}
},
};
export
default
CategoryController
;
app/routes/admin.js
View file @
4541bf7c
import
express
from
"express"
;
import
AdminUserController
from
"../controllers/AdminUserController"
;
import
AdminProductController
from
"../controllers/AdminProductController"
;
import
CategoryController
from
"../controllers/CategoryController"
;
const
adminRouter
=
express
.
Router
();
adminRouter
.
get
(
'/user'
,
AdminUserController
.
index
)
adminRouter
.
post
(
'/product'
,
AdminProductController
.
create
)
adminRouter
.
get
(
"/user"
,
AdminUserController
.
index
);
adminRouter
.
post
(
"/product"
,
AdminProductController
.
create
);
adminRouter
.
post
(
"/category"
,
CategoryController
.
create
);
export
default
adminRouter
;
src/components/AddCategory.js
0 → 100644
View file @
4541bf7c
import
{
useState
,
useEffect
,
useContext
}
from
"react"
;
import
Button
from
"@mui/material/Button"
;
import
TextField
from
"@mui/material/TextField"
;
import
Dialog
from
"@mui/material/Dialog"
;
import
DialogActions
from
"@mui/material/DialogActions"
;
import
DialogContent
from
"@mui/material/DialogContent"
;
import
DialogTitle
from
"@mui/material/DialogTitle"
;
import
{
FormControl
,
InputLabel
,
MenuItem
,
Select
,
Snackbar
,
}
from
"@mui/material"
;
import
axios
from
"axios"
;
import
{
UserContext
}
from
"@/pages/_app"
;
export
default
function
AddCategory
({
open
,
handleClose
})
{
const
user
=
useContext
(
UserContext
);
const
[
message
,
setMessage
]
=
useState
({
message
:
""
,
error
:
false
});
const
[
name
,
setName
]
=
useState
(
""
);
/**
*
* @param {FormDataEvent} e
*/
async
function
fetchAddProduct
(
e
)
{
e
.
preventDefault
();
let
response
=
await
axios
.
post
(
"/api/admin/category"
,
{
name
},
{
headers
:
{
token
:
user
.
value
.
token
}
}
);
if
(
response
.
data
.
status
===
202
)
{
setMessage
({
message
:
"หมวดหมู่นี้ถูกใช้แล้ว"
,
error
:
true
});
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
3000
);
}
else
if
(
response
.
data
.
status
===
201
)
{
setName
(
""
)
handleClose
()
setMessage
({
message
:
"เพิ่มหมวดหมู่สำเร็จ"
,
error
:
false
})
setTimeout
(()
=>
{
setMessage
({
message
:
""
,
error
:
false
});
},
3000
);
}
}
return
(
<>
<
Snackbar
anchorOrigin
=
{{
horizontal
:
"center"
,
vertical
:
"top"
}}
ContentProps
=
{{
className
:
message
.
error
?
"bg-red-500"
:
"bg-green-500"
,
}}
open
=
{
!!
message
.
message
.
length
}
message
=
{
message
.
message
}
/
>
<
Dialog
open
=
{
open
}
onClose
=
{
handleClose
}
>
<
form
onSubmit
=
{
fetchAddProduct
}
>
<
DialogTitle
>
เพิ่มหมวดหมู่
<
/DialogTitle
>
<
DialogContent
className
=
"text-center"
>
<
TextField
className
=
"m-1"
label
=
"ชื่อหมวดหมู่"
variant
=
"standard"
value
=
{
name
}
onChange
=
{(
e
)
=>
setName
(
e
.
target
.
value
)}
required
/>
<
/DialogContent
>
<
DialogActions
>
<
Button
color
=
"error"
onClick
=
{
handleClose
}
>
ยกเลิก
<
/Button
>
<
Button
type
=
"submit"
>
เพิ่ม
<
/Button
>
<
/DialogActions
>
<
/form
>
<
/Dialog
>
<
/
>
);
}
src/pages/admin/category.js
0 → 100644
View file @
4541bf7c
import
{
Add
}
from
"@mui/icons-material"
;
import
{
Box
,
Button
}
from
"@mui/material"
;
import
React
,
{
useState
}
from
"react"
;
import
AddCategory
from
"@/components/AddCategory"
;
export
default
function
AdminCategory
()
{
const
[
modal
,
setModal
]
=
useState
(
false
);
return
(
<
Box
>
<
Box
sx
=
{{
mb
:
2
,
textAlign
:
"right"
}}
>
<
Button
onClick
=
{()
=>
setModal
(
true
)}
>
<
Add
/>
เพิ่มหมวดหมู่
<
/Button
>
<
/Box
>
<
AddCategory
open
=
{
modal
}
handleClose
=
{()
=>
setModal
(
false
)}
/
>
<
/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