1+ const express = require ( "express" ) ;
2+ const recordRoutes = express . Router ( ) ;
3+ const dbo = require ( "../db/conn" ) ;
4+ const ObjectId = require ( "mongodb" ) . ObjectId ;
5+
6+ recordRoutes . use ( express . urlencoded ( { extended : true } ) ) ;
7+
8+ // menampilkan data
9+ recordRoutes . route ( "/customer" ) . get ( function ( req , res ) {
10+ let db_connect = dbo . getDb ( "menantea" ) ;
11+ db_connect
12+ . collection ( "customer" )
13+ . find ( { } )
14+ . toArray ( function ( err , result ) {
15+ if ( err ) throw err ;
16+ res . json ( result ) ;
17+ } )
18+ . then ( ( data ) => {
19+ console . log ( 'customer berhasil ditampilkan' )
20+ res . json ( {
21+ message : "customer berhasil ditampilkan" ,
22+ data : data ,
23+ } ) ;
24+ } ) ;
25+ } ) ;
26+
27+ // menampilkan data by id
28+ recordRoutes . route ( "/customer/:id" ) . get ( function ( req , res ) {
29+ let db_connect = dbo . getDb ( "menantea" ) ;
30+ let myquery = { _id : new ObjectId ( req . params . id ) } ;
31+ db_connect
32+ . collection ( "customer" )
33+ . findOne ( myquery , function ( err , result ) {
34+ if ( err ) throw err ;
35+ res . json ( result ) ;
36+ } )
37+ . then ( ( data ) => {
38+ console . log ( 'customer berhasil ditampilkan' )
39+ res . json ( {
40+ message : "customer berhasil ditampilkan" ,
41+ data : data ,
42+ } ) ;
43+ } ) ;
44+ } ) ;
45+
46+ // menambahkan data
47+ recordRoutes . route ( "/customer/add" ) . post ( function ( req , res ) {
48+ let db_connect = dbo . getDb ( "menantea" ) ;
49+ let myObj = {
50+ name : req . body . name ,
51+ alamat : req . body . alamat ,
52+ pemesanan : req . body . pemesanan ,
53+ telp : req . body . telp ,
54+ } ;
55+ db_connect
56+ . collection ( "customer" )
57+ . insertOne ( myObj , function ( err , result ) {
58+ if ( err ) throw err ;
59+ res . json ( result ) ;
60+ } )
61+ . then ( ( ) => {
62+ console . log ( 'customer berhasil ditambahkan' )
63+ res . json ( {
64+ message : "customer berhasil ditambahkan" ,
65+ data : myObj ,
66+ } ) ;
67+ } ) ;
68+ } ) ;
69+
70+ // mengupdate data
71+ recordRoutes . route ( "/customer/update/:id" ) . put ( function ( req , res ) {
72+ let db_connect = dbo . getDb ( "menantea" ) ;
73+ let myquery = { _id : new ObjectId ( req . params . id ) } ;
74+ let newValues = {
75+ $set : {
76+ name : req . body . name ,
77+ alamat : req . body . alamat ,
78+ pemesanan : req . body . pemesanan ,
79+ telp : req . body . telp ,
80+ } ,
81+ } ;
82+ db_connect
83+ . collection ( "customer" )
84+ . updateOne ( myquery , newValues , function ( err , result ) {
85+ if ( err ) throw err ;
86+ console . log ( "berhasil update customer" ) ;
87+ res . json ( result ) ;
88+ } )
89+ . then ( ( ) => {
90+ console . log ( "berhasil update customer" ) ;
91+ res . json ( {
92+ message : "Data berhasil diupdate" ,
93+ data : newValues ,
94+ } ) ;
95+ } ) ;
96+ } ) ;
97+
98+ // menghapus data
99+ recordRoutes . route ( "/customer/delete/:id" ) . delete ( function ( req , res ) {
100+ let db_connect = dbo . getDb ( "menantea" ) ;
101+ let myquery = { _id : new ObjectId ( req . params . id ) } ;
102+ db_connect
103+ . collection ( "customer" )
104+ . deleteOne ( myquery , function ( err , result ) {
105+ if ( err ) throw err ;
106+ console . log ( "customer berhasil dihapus" ) ;
107+ res . json ( result ) ;
108+ } )
109+ . then ( ( data ) => {
110+ console . log ( "customer berhasil dihapus" ) ;
111+ res . json ( {
112+ message : "customer berhasil dihapus" ,
113+ data : data ,
114+ } ) ;
115+ } ) ;
116+ } ) ;
117+
118+ module . exports = recordRoutes ;
0 commit comments