1
+ const { Appointment, Customer, Price } = require ( '../models' )
2
+
3
+ class Controller {
4
+ static async getAppointment ( req , res , next ) {
5
+ try {
6
+ let appointments
7
+ if ( req . query . status ) {
8
+ appointments = await Appointment . findAll ( {
9
+ where : { status : req . query . status } ,
10
+ include : { model : Customer }
11
+ } )
12
+ } else {
13
+ appointments = await Appointment . findAll ( { include : { model : Customer } } )
14
+ }
15
+ console . log ( appointments ) ;
16
+ res . status ( 200 ) . json ( { message : 'read data success' , data : appointments } )
17
+ } catch ( error ) {
18
+ next ( error ) ;
19
+ }
20
+ }
21
+
22
+ static async postAppointment ( req , res , next ) {
23
+ try {
24
+ console . log ( req . body ) ;
25
+ const { CustomerId, childName, childAge, startDate, endDate, status, childCategory, packageCategory, quantity, note} = req . body
26
+ const price = await Price . findOne ( { where : { category : childCategory , package : packageCategory } } )
27
+ const total = price . price * quantity
28
+ const PriceId = price . id
29
+ const insertedData = await Appointment . create ( { CustomerId, childName, childAge, startDate, endDate, status, PriceId, quantity, total, note} )
30
+ res . status ( 201 ) . json ( insertedData )
31
+ } catch ( error ) {
32
+ console . log ( error ) ;
33
+ next ( error ) ;
34
+ }
35
+ }
36
+
37
+ //gak dipake
38
+ static async getAppointmentById ( req , res , next ) {
39
+ try {
40
+ const appointment = await Appointment . findOne ( { where : { id : req . params . id } , include : { model : Customer } } )
41
+ if ( appointment ) {
42
+ res . status ( 200 ) . json ( appointment )
43
+ } else {
44
+ next ( { status :404 , msg : 'data not found' } )
45
+ }
46
+ } catch ( error ) {
47
+ next ( error ) ;
48
+ }
49
+ }
50
+
51
+ static async getAppointmentByCustomerId ( req , res , next ) {
52
+ try {
53
+ const appointment = await Appointment . findAll ( { where : { Customerid : req . params . Customerid } } )
54
+ if ( appointment ) {
55
+ res . status ( 200 ) . json ( appointment )
56
+ } else {
57
+ next ( { status : 404 , msg : 'data not found' } )
58
+ }
59
+ } catch ( error ) {
60
+ next ( error ) ;
61
+ }
62
+ }
63
+
64
+ static async patchAppointment ( req , res , next ) {
65
+ const { status } = req . body
66
+ try {
67
+ let updatedData = await Appointment . update ( { status : status } , {
68
+ where : {
69
+ id : req . params . id
70
+ } ,
71
+ returning : true
72
+ } )
73
+ if ( updatedData ) {
74
+ res . status ( 200 ) . json ( updatedData )
75
+ } else {
76
+ res . status ( 400 ) . json ( { message : 'Data not found' } )
77
+ }
78
+ } catch ( error ) {
79
+ next ( error ) ;
80
+ }
81
+ }
82
+ }
83
+
84
+ module . exports = Controller
0 commit comments