1
+ <?php
2
+
3
+ require_once '../model/Config.php ' ;
4
+ require_once '../model/DB.php ' ;
5
+ require_once '../model/Location.php ' ;
6
+ require_once '../model/Response.php ' ;
7
+
8
+ try {
9
+ $ writeDB = DB ::connectWriteDB ();
10
+ }
11
+ catch (PDOException $ e ) {
12
+ error_log ("Exception: " . $ e ->getMessage (), 0 );
13
+ $ response = new Response ();
14
+ $ response ->setHttpStatusCode (500 );
15
+ $ response ->setSuccess (false );
16
+ $ response ->addMessage ("Error: connection to database could not be established. " );
17
+ $ response ->send ();
18
+ exit ();
19
+ }
20
+
21
+ if ($ _SERVER ['REQUEST_METHOD ' ] !== 'POST ' ) {
22
+ $ response = new Response ();
23
+ $ response ->setHttpStatusCode (405 );
24
+ $ response ->setSuccess (false );
25
+ $ response ->addMessage ("Error: request method not permitted on the user endpoint. " );
26
+ $ response ->send ();
27
+ exit ();
28
+ }
29
+
30
+ if ($ _SERVER ['CONTENT_TYPE ' ] !== 'application/json ' ) {
31
+ $ response = new Response ();
32
+ $ response ->setHttpStatusCode (400 );
33
+ $ response ->setSuccess (false );
34
+ $ response ->addMessage ("Error: content type header not set to JSON. " );
35
+ $ response ->send ();
36
+ exit ();
37
+ }
38
+
39
+ $ $ raw_post_data = file_get_contents ('php://input ' );
40
+
41
+ if (!$ json_data = json_decode ($ raw_post_data )) {
42
+ $ response = new Response ();
43
+ $ response ->setHttpStatusCode (400 );
44
+ $ response ->setSuccess (false );
45
+ $ response ->addMessage ("Error: request body is not valid JSON. " );
46
+ $ response ->send ();
47
+ exit ();
48
+ }
49
+
50
+ if (!isset ($ json_data ->businessName , $ json_data ->authContact , $ json_data ->phone , $ json_data ->streetAddress , $ json_data ->suburb , $ json_data ->state , $ json_data ->postcode , $ json_data ->password )) {
51
+ $ response = new Response ();
52
+ $ response ->setHttpStatusCode (400 );
53
+ $ response ->setSuccess (false );
54
+ (!isset ($ json_data ->businessName ) ? $ response ->addMessage ("Error: request body does not contain a business name. " ) : false );
55
+ (!isset ($ json_data ->authContact ) ? $ response ->addMessage ("Error: request body does not contain an authorised contact. " ) : false );
56
+ (!isset ($ json_data ->phone ) ? $ response ->addMessage ("Error: request body does not contain a contact phone number. " ) : false );
57
+ (!isset ($ json_data ->streetAddress ) ? $ response ->addMessage ("Error: request body does not contain a street address. " ) : false );
58
+ (!isset ($ json_data ->suburb ) ? $ response ->addMessage ("Error: request body does not contain a suburb name. " ) : false );
59
+ (!isset ($ json_data ->state ) ? $ response ->addMessage ("Error: request body does not contain a state name. " ) : false );
60
+ (!isset ($ json_data ->postcode ) ? $ response ->addMessage ("Error: request body does not contain a postcode. " ) : false );
61
+ (!isset ($ json_data ->password ) ? $ response ->addMessage ("Error: request body does not contain a password. " ) : false );
62
+ $ response ->send ();
63
+ exit ();
64
+ }
65
+
66
+ try {
67
+
68
+ $ location = new Location ();
69
+ $ location ->setName (trim ($ json_data ->businessName ));
70
+ $ location ->setAuthContact (trim ($ json_data ->authContact ));
71
+ if (isset ($ json_data ->avatar )) $ location ->setAvatar (trim ($ json_data ->avatar ));
72
+ $ location ->setPhoneNumber (trim ($ json_data ->phone ));
73
+ $ location ->address ()->setStreetAddress (trim ($ json_data ->streetAddress ));
74
+ $ location ->address ()->setSuburb (trim ($ json_data ->suburb ));
75
+ $ location ->address ()->setState (trim ($ json_data ->state ));
76
+ $ location ->address ()->setPostCode (trim ($ json_data ->postcode ));
77
+ if (isset ($ json_data ->email )) $ location ->setEmailAddress (trim ($ json_data ->email ));
78
+ if (isset ($ json_data ->abn )) $ location ->setABN (trim ($ json_data ->abn ));
79
+
80
+ $ query_email = $ location ->getEmailAddress ();
81
+ $ query = $ writeDB ->prepare ("SELECT `id` FROM `accounts` WHERE `email` = :email " );
82
+ $ query ->bindParam (':email ' , $ query_email , PDO ::PARAM_STR );
83
+ $ query ->execute ();
84
+
85
+ $ row_count = $ query ->rowCount ();
86
+ if ($ row_count > 0 ) {
87
+ $ response = new Response ();
88
+ $ response ->setHttpStatusCode (409 );
89
+ $ response ->setSuccess (false );
90
+ $ response ->addMessage ("Error: email address already listed within the database. " );
91
+ $ response ->send ();
92
+ exit ();
93
+ }
94
+
95
+ $ passwordHash = password_hash ($ json_data ->password , PASSWORD_DEFAULT );
96
+
97
+ $ query_abn = $ location ->getABN ();
98
+ $ query_contact = $ location ->getAuthorisedContact ();
99
+ $ query_avatar = $ location ->getAvatar ();
100
+ $ query_name = $ location ->getName ();
101
+ $ query_email = $ location ->getEmailAddress ();
102
+ $ query_phone = $ location ->getPhoneNumber ();
103
+ $ query_postcode = $ location ->getPhoneNumber ();
104
+ $ query_state = $ location ->address ()->getPostcode ();
105
+ $ query_address = $ location ->address ()->getStreetAddress ();
106
+ $ query_suburb = $ location ->address ()->getSuburb ();
107
+ $ query = $ writeDB ->prepare ("INSERT INTO `accounts`
108
+ (ABN, auth, auth_contact, avatar, business_name, email, phone, postcode, `state`, street_address, suburb) VALUES
109
+ (:abn, :auth, :authContact, :avatar, :business, :email, :phone, :postcode, :state, :address, :suburb) " );
110
+ $ query ->bindParam (':abn ' , $ query_abn , PDO ::PARAM_STR );
111
+ $ query ->bindParam (':auth ' , $ passwordHash , PDO ::PARAM_STR );
112
+ $ query ->bindParam (':authContact ' , $ query_contact , PDO ::PARAM_STR );
113
+ $ query ->bindParam (':avatar ' , $ query_avatar , PDO ::PARAM_STR );
114
+ $ query ->bindParam (':business ' , $ query_name , PDO ::PARAM_STR );
115
+ $ query ->bindParam (':email ' , $ query_email , PDO ::PARAM_STR );
116
+ $ query ->bindParam (':phone ' , $ query_phone , PDO ::PARAM_STR );
117
+ $ query ->bindParam (':postcode ' , $ query_postcode , PDO ::PARAM_STR );
118
+ $ query ->bindParam (':state ' , $ query_state , PDO ::PARAM_STR );
119
+ $ query ->bindParam (':address ' , $ query_address , PDO ::PARAM_STR );
120
+ $ query ->bindParam (':suburb ' , $ query_suburb , PDO ::PARAM_STR );
121
+ $ query ->execute ();
122
+
123
+ $ row_count = $ query ->rowCount ();
124
+ if ($ row_count === 0 ) {
125
+ $ response = new Response ();
126
+ $ response ->setHttpStatusCode (500 );
127
+ $ response ->setSuccess (false );
128
+ $ response ->addMessage ("Error: database error during user creation. " );
129
+ $ response ->send ();
130
+ exit ();
131
+ }
132
+
133
+ $ response_data = [];
134
+ $ response_data ['id ' ] = $ writeDB ->lastInsertId ();
135
+ $ response_data ['name ' ] = $ query_name ;
136
+ $ response_data ['authorisedContact ' ] = $ query_contact ;
137
+ $ response_data ['contactPhone ' ] = $ query_phone ;
138
+ $ response_data ['contactEmail ' ] = $ query_email ;
139
+
140
+ $ response = new Response ();
141
+ $ response ->setHttpStatusCode (201 );
142
+ $ response ->setSuccess (true );
143
+ $ response ->addMessage ("Account successfully created. " );
144
+ $ response ->setData ($ responseData );
145
+ $ response ->send ();
146
+ exit ();
147
+
148
+ }
149
+ catch (PDOException $ e ) {
150
+ error_log ("Exception: " . $ e ->getMessage ());
151
+ $ response = new Response ();
152
+ $ response ->setHttpStatusCode (500 );
153
+ $ response ->setSuccess (false );
154
+ $ response ->addMessage ("Error: database error during user creation. " );
155
+ $ response ->send ();
156
+ exit ();
157
+ }
158
+ catch (APIException $ e ) {
159
+ $ response = new Response ();
160
+ $ response ->setHttpStatusCode (400 );
161
+ $ response ->setSuccess (false );
162
+ $ response ->addMessage ("Error: " . $ e ->getMessage ());
163
+ $ response ->send ();
164
+ exit ();
165
+ }
166
+ catch (Error $ e ) {
167
+ error_log ("Exception: " . $ e ->getMessage ());
168
+ $ response = new Response ();
169
+ $ response ->setHttpStatusCode (500 );
170
+ $ response ->setSuccess (false );
171
+ $ response ->addMessage ("Unknown error. " );
172
+ $ response ->send ();
173
+ exit ();
174
+ }
0 commit comments