@@ -21,63 +21,163 @@ public Points(ILoggerFactory loggerFactory)
2121 _databaseHandler = new DatabaseHandler ( ) ;
2222 }
2323
24- [ Function ( "GetPoints" ) ]
25- public async Task < HttpResponseData > GetPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "get" ) ] HttpRequestData req )
24+ #region 3D Points Visualization
25+ [ Function ( "GetAllPoints" ) ]
26+ public async Task < HttpResponseData > GetAllPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "get" , Route = "3DPoints/GetAll" ) ] HttpRequestData req )
2627 {
27- _logger . LogInformation ( "Retrieving all 3D points." ) ;
28+ try
29+ {
30+ _logger . LogInformation ( "Retrieving all 3D points." ) ;
31+
32+ var points = await _databaseHandler . GetAllPointsAsync ( ) ;
33+ _logger . LogInformation ( $ "Retrieved points: { JsonSerializer . Serialize ( points ) } ") ;
34+
35+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
36+ response . Headers . Add ( "Content-Type" , "application/json; charset=utf-8" ) ;
37+ await response . WriteStringAsync ( JsonSerializer . Serialize ( points ) ) ;
38+
39+ return response ;
40+ }
41+ catch ( Exception ex )
42+ {
43+ _logger . LogError ( ex , "An error occurred while retrieving points." ) ;
44+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
45+ }
46+ }
47+
48+ [ Function ( "GetPointById" ) ]
49+ public async Task < HttpResponseData > GetPointById ( [ HttpTrigger ( AuthorizationLevel . Function , "get" , Route = "3DPoints/Get/{id:int}" ) ] HttpRequestData req , int id )
50+ {
51+ try
52+ {
53+ _logger . LogInformation ( $ "Retrieving point with ID: { id } ") ;
2854
29- var points = await _databaseHandler . GetAllPointsAsync ( ) ;
30- _logger . LogInformation ( $ "Retrieved points: { JsonSerializer . Serialize ( points ) } ") ;
55+ var point = await _databaseHandler . GetPointByIdAsync ( id ) ;
56+ if ( point == null )
57+ {
58+ return req . CreateResponse ( HttpStatusCode . NotFound ) ;
59+ }
3160
32- var response = req . CreateResponse ( HttpStatusCode . OK ) ;
33- response . Headers . Add ( "Content-Type" , "application/json; charset=utf-8" ) ;
34- await response . WriteStringAsync ( JsonSerializer . Serialize ( points ) ) ;
61+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
62+ response . Headers . Add ( "Content-Type" , "application/json; charset=utf-8" ) ;
63+ await response . WriteStringAsync ( JsonSerializer . Serialize ( point ) ) ;
3564
36- return response ;
65+ return response ;
66+ }
67+ catch ( Exception ex )
68+ {
69+ _logger . LogError ( ex , $ "An error occurred while retrieving point with ID { id } .") ;
70+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
71+ }
3772 }
3873
3974 [ Function ( "AddPoints" ) ]
40- public async Task < HttpResponseData > AddPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "post" ) ] HttpRequestData req )
75+ public async Task < HttpResponseData > AddPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "post" , Route = "3DPoints/Add" ) ] HttpRequestData req )
4176 {
42- _logger . LogInformation ( "Adding new 3D points." ) ;
77+ try
78+ {
79+ _logger . LogInformation ( "Adding new 3D points." ) ;
4380
44- var requestBody = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ;
45- var points = JsonSerializer . Deserialize < List < Point3D > > ( requestBody ) ;
81+ var requestBody = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ;
82+ var points = JsonSerializer . Deserialize < List < Point3D > > ( requestBody ) ;
4683
47- await _databaseHandler . AddPointsAsync ( points ) ;
84+ await _databaseHandler . AddPointsAsync ( points ) ;
4885
49- var response = req . CreateResponse ( HttpStatusCode . OK ) ;
50- response . Headers . Add ( "Content-Type" , "text/plain; charset=utf-8" ) ;
51- await response . WriteStringAsync ( "Points added successfully." ) ;
86+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
87+ response . Headers . Add ( "Content-Type" , "text/plain; charset=utf-8" ) ;
88+ await response . WriteStringAsync ( "Points added successfully." ) ;
5289
53- return response ;
90+ return response ;
91+ }
92+ catch ( Exception ex )
93+ {
94+ _logger . LogError ( ex , "An error occurred while adding points." ) ;
95+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
96+ }
5497 }
5598
5699 [ Function ( "SendRandomPoints" ) ]
57- public async Task < HttpResponseData > SendRandomPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "post" ) ] HttpRequestData req )
100+ public async Task < HttpResponseData > SendRandomPoints ( [ HttpTrigger ( AuthorizationLevel . Function , "post" , Route = "3DPoints/SendRandom" ) ] HttpRequestData req )
58101 {
59- _logger . LogInformation ( "Sending random 3D points." ) ;
102+ try
103+ {
104+ _logger . LogInformation ( "Sending random 3D points." ) ;
60105
61- var random = new Random ( ) ;
62- var points = new List < Point3D > ( ) ;
106+ var random = new Random ( ) ;
107+ var points = new List < Point3D > ( ) ;
63108
64- for ( int i = 0 ; i < 10 ; i ++ )
65- {
66- points . Add ( new Point3D
109+ for ( int i = 0 ; i < 10 ; i ++ )
67110 {
68- X = random . Next ( - 100 , 100 ) ,
69- Y = random . Next ( - 100 , 100 ) ,
70- Z = random . Next ( - 100 , 100 )
71- } ) ;
111+ points . Add ( new Point3D
112+ {
113+ X = random . Next ( - 100 , 100 ) ,
114+ Y = random . Next ( - 100 , 100 ) ,
115+ Z = random . Next ( - 100 , 100 )
116+ } ) ;
117+ }
118+
119+ await _databaseHandler . AddPointsAsync ( points ) ;
120+
121+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
122+ response . Headers . Add ( "Content-Type" , "application/json; charset=utf-8" ) ;
123+ await response . WriteStringAsync ( JsonSerializer . Serialize ( points ) ) ;
124+
125+ return response ;
126+ }
127+ catch ( Exception ex )
128+ {
129+ _logger . LogError ( ex , "An error occurred while sending random points." ) ;
130+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
131+ }
132+ }
133+
134+ [ Function ( "DeletePointById" ) ]
135+ public async Task < HttpResponseData > DeletePointById ( [ HttpTrigger ( AuthorizationLevel . Function , "delete" , Route = "3DPoints/Delete/{id:int}" ) ] HttpRequestData req , int id )
136+ {
137+ try
138+ {
139+ _logger . LogInformation ( $ "Deleting point with ID: { id } ") ;
140+
141+ await _databaseHandler . DeletePointByIdAsync ( id ) ;
142+
143+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
144+ response . Headers . Add ( "Content-Type" , "text/plain; charset=utf-8" ) ;
145+ await response . WriteStringAsync ( $ "Point with ID { id } deleted successfully.") ;
146+
147+ return response ;
148+ }
149+ catch ( Exception ex )
150+ {
151+ _logger . LogError ( ex , $ "An error occurred while deleting point with ID { id } .") ;
152+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
72153 }
154+ }
73155
74- await _databaseHandler . AddPointsAsync ( points ) ;
156+ [ Function ( "ReplacePointById" ) ]
157+ public async Task < HttpResponseData > ReplacePointById ( [ HttpTrigger ( AuthorizationLevel . Function , "put" , Route = "3DPoints/Replace/{id:int}" ) ] HttpRequestData req , int id )
158+ {
159+ try
160+ {
161+ _logger . LogInformation ( $ "Replacing point with ID: { id } ") ;
75162
76- var response = req . CreateResponse ( HttpStatusCode . OK ) ;
77- response . Headers . Add ( "Content-Type" , "application/json; charset=utf-8" ) ;
78- await response . WriteStringAsync ( JsonSerializer . Serialize ( points ) ) ;
163+ var requestBody = await new StreamReader ( req . Body ) . ReadToEndAsync ( ) ;
164+ var point = JsonSerializer . Deserialize < Point3D > ( requestBody ) ;
165+ point . ID = id ;
79166
80- return response ;
167+ await _databaseHandler . ReplacePointByIdAsync ( point ) ;
168+
169+ var response = req . CreateResponse ( HttpStatusCode . OK ) ;
170+ response . Headers . Add ( "Content-Type" , "text/plain; charset=utf-8" ) ;
171+ await response . WriteStringAsync ( $ "Point with ID { id } replaced successfully.") ;
172+
173+ return response ;
174+ }
175+ catch ( Exception ex )
176+ {
177+ _logger . LogError ( ex , $ "An error occurred while replacing point with ID { id } .") ;
178+ return req . CreateResponse ( HttpStatusCode . InternalServerError ) ;
179+ }
81180 }
181+ #endregion
82182 }
83183}
0 commit comments