@@ -4,70 +4,119 @@ const Posts = db.posts;
44const Op = db . Sequelize . op ;
55
66const createPost = async ( req , res ) => {
7- if ( ! req . body . title ) {
8- res . status ( 400 ) . send ( {
9- message : "Post title cannot be empty"
7+ try {
8+ const { title, subtitle, body, author_id } = req . body ;
9+ if ( ! req . body . title ) {
10+ res . status ( 400 ) . send ( {
11+ message : "Post title cannot be empty"
12+ } ) ;
13+ return ;
14+ }
15+ if ( ! req . body . body ) {
16+ res . status ( 400 ) . send ( {
17+ message : "Post body cannot be empty"
18+ } ) ;
19+ return ;
20+ }
21+ const newpost = await db . sequelize . query (
22+ `INSERT INTO Posts (title,subtitle,body,author_id,createdAt,updatedAt,updated_at) values ('${ title } ','${ subtitle } ','${ body } ',${ author_id } ,CURDATE(),CURDATE(),CURDATE())` , {
23+ type : db . sequelize . QueryTypes . INSERT
1024 } ) ;
11- return ;
12- }
13- if ( ! req . body . body ) {
14- res . status ( 400 ) . send ( {
15- message : "Post body cannot be empty"
25+ res . send ( {
26+ id : newpost [ 0 ] ,
27+ title,
28+ subtitle,
29+ body,
30+ author_id,
1631 } ) ;
17- return ;
1832 }
19- const newpost = await db . sequelize . query (
20- `INSERT INTO Post (title,subtitle,body,author,category_id,updated_at) values ('${ req . body . title } ','${ req . body . subtitle } ','${ req . body . body } ','${ req . body . author_id } ',datetime('now'))`
21- , {
22- replacements : { id : req . user . id } ,
23- type : db . sequelize . QueryTypes . INSERT
24- } ) ;
25- if ( newpost ) {
26- res . status ( 200 ) . json ( {
27- status :200 ,
28- message : "Post created successfully" ,
29- post : newpost
33+ catch ( e ) {
34+ res . status ( 500 ) . send ( {
35+ message : e . message
3036 } )
3137 }
3238}
3339
34- const updatePostByID = ( req , res ) => {
40+ const updatePostByID = async ( req , res ) => {
41+ try {
42+ const { title, subtitle, body, author_id } = req . body ;
43+ const { id } = req . params ;
44+
45+ const sql = `UPDATE posts
46+ SET title = '${ title } ',
47+ subtitle = '${ subtitle } ',
48+ body = '${ body } ',
49+ author_id = ${ author_id } ,
50+ updatedAt = CURDATE(),
51+ updated_at = CURDATE()
52+ WHERE id = ${ id } ;`
53+
54+ const result = await db . sequelize . query ( sql , {
55+ type : db . sequelize . QueryTypes . UPDATE
56+ } )
57+ res . send ( {
58+ id : result [ 0 ] ,
59+ title,
60+ subtitle,
61+ body,
62+ author_id,
63+ } ) ;
64+ }
65+ catch ( e ) {
66+ res . status ( 500 ) . send ( {
67+ message : e . message
68+ } )
69+ }
3570
3671}
3772
38- const deletePostById = ( req , res ) => {
39-
73+ const deletePostById = async ( req , res ) => {
74+ try {
75+ const { id} = req . params ;
76+ const sql = `DELETE from posts where id=${ id } ` ;
77+ const result = await db . sequelize . query ( sql , {
78+ type : db . sequelize . QueryTypes . DELETE
79+ } )
80+ res . send ( {
81+ message : "Deleted Successfully"
82+ } ) ;
83+ return ;
84+ }
85+ catch ( e ) {
86+ res . status ( 500 ) . send ( {
87+ message : e . message
88+ } )
89+ }
4090}
4191
4292const getAllPosts = async ( req , res ) => {
43- const posts = await db . sequelize . query ( `SELECT * from Post` ) ;
44- if ( posts ) {
45- res . status ( 200 ) . json ( {
46- status :200 ,
47- message : "Posts retrieved successfully" ,
48- posts : posts
49- } )
93+ try {
94+ const posts = await db . sequelize . query ( `SELECT * from Posts` , {
95+ type : db . sequelize . QueryTypes . SELECT
96+ } ) ;
97+ res . send ( posts ) ;
5098 }
51- else {
99+ catch ( e ) {
52100 res . status ( 500 ) . send ( {
53- message : "Server error"
101+ message : e . message
54102 } )
55103 }
56104}
57105
58106const getPostById = async ( req , res ) => {
59- const id = req . params . id ;
60- const selectedPost = await db . sequelize . query ( `SELECT * from Post where id='${ id } '` ) ;
61- if ( selectedPost ) {
62- res . status ( 200 ) . json ( {
63- status :200 ,
64- message : "Post retrieved successfully" ,
65- post : selectedPost
66- } )
107+ try {
108+ const id = req . params . id ;
109+ const selectedPost = await db . sequelize . query ( `SELECT * from Posts where id=${ id } ` , {
110+ type : db . sequelize . QueryTypes . SELECT
111+ } ) ;
112+ res . send ( {
113+ ...selectedPost [ 0 ]
114+ } ) ;
115+ return res ;
67116 }
68- else {
117+ catch ( e ) {
69118 res . status ( 500 ) . send ( {
70- message : "Server error"
119+ message : e . message
71120 } )
72121 }
73122}
0 commit comments