Skip to content

Commit 681da52

Browse files
committed
- Funções de API para buscar, substituir e para apagar pontos individuais
- Definição de uma rota separada para obtenção de pontos
1 parent 46ffe0f commit 681da52

File tree

5 files changed

+214
-42
lines changed

5 files changed

+214
-42
lines changed

API.cs

Lines changed: 134 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<connectionStrings>
4+
<add name="DefaultConnection" connectionString="Server=tcp:modsi-project.database.windows.net,1433;Initial Catalog=modsi-project;Persist Security Info=False;User ID=MODSIPROJECT;Password=#modsi2025;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
5+
</connectionStrings>
6+
</configuration>

DataFormats.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public class Point3D
44
{
55
public int ID { get; set; }
6-
public float X { get; set; }
7-
public float Y { get; set; }
8-
public float Z { get; set; }
6+
public double X { get; set; }
7+
public double Y { get; set; }
8+
public double Z { get; set; }
99
}
1010
}

DatabaseHandler.cs

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System;
21
using System.Collections.Generic;
2+
using System.Configuration;
33
using System.Data.SqlClient;
44
using System.Threading.Tasks;
55

@@ -11,9 +11,10 @@ public class DatabaseHandler
1111

1212
public DatabaseHandler()
1313
{
14-
_connectionString = "Server=tcp:modsi-project.database.windows.net,1433;Initial Catalog=modsi-project;Persist Security Info=False;User ID=MODSIPROJECT;Password=#modsi2025;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
14+
_connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
1515
}
1616

17+
#region 3D Points Visualization
1718
public async Task<List<Point3D>> GetAllPointsAsync()
1819
{
1920
var points = new List<Point3D>();
@@ -31,9 +32,9 @@ public async Task<List<Point3D>> GetAllPointsAsync()
3132
points.Add(new Point3D
3233
{
3334
ID = reader.GetInt32(0),
34-
X = reader.GetFloat(1),
35-
Y = reader.GetFloat(2),
36-
Z = reader.GetFloat(3)
35+
X = (double)reader.GetDouble(1),
36+
Y = (double)reader.GetDouble(2),
37+
Z = (double)reader.GetDouble(3)
3738
});
3839
}
3940
}
@@ -43,6 +44,36 @@ public async Task<List<Point3D>> GetAllPointsAsync()
4344
return points;
4445
}
4546

47+
public async Task<Point3D> GetPointByIdAsync(int id)
48+
{
49+
Point3D point = null;
50+
51+
using (SqlConnection conn = new SqlConnection(_connectionString))
52+
{
53+
await conn.OpenAsync();
54+
var query = "SELECT Id, x, y, z FROM Pontos3D WHERE Id = @id";
55+
using (SqlCommand cmd = new SqlCommand(query, conn))
56+
{
57+
cmd.Parameters.AddWithValue("@id", id);
58+
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
59+
{
60+
if (await reader.ReadAsync())
61+
{
62+
point = new Point3D
63+
{
64+
ID = reader.GetInt32(0),
65+
X = (double)reader.GetDouble(1),
66+
Y = (double)reader.GetDouble(2),
67+
Z = (double)reader.GetDouble(3)
68+
};
69+
}
70+
}
71+
}
72+
}
73+
74+
return point;
75+
}
76+
4677
public async Task AddPointsAsync(List<Point3D> points)
4778
{
4879
using (SqlConnection conn = new SqlConnection(_connectionString))
@@ -61,5 +92,37 @@ public async Task AddPointsAsync(List<Point3D> points)
6192
}
6293
}
6394
}
95+
96+
public async Task DeletePointByIdAsync(int id)
97+
{
98+
using (SqlConnection conn = new SqlConnection(_connectionString))
99+
{
100+
await conn.OpenAsync();
101+
var query = "DELETE FROM Pontos3D WHERE Id = @id";
102+
using (SqlCommand cmd = new SqlCommand(query, conn))
103+
{
104+
cmd.Parameters.AddWithValue("@id", id);
105+
await cmd.ExecuteNonQueryAsync();
106+
}
107+
}
108+
}
109+
110+
public async Task ReplacePointByIdAsync(Point3D point)
111+
{
112+
using (SqlConnection conn = new SqlConnection(_connectionString))
113+
{
114+
await conn.OpenAsync();
115+
var query = "UPDATE Pontos3D SET x = @x, y = @y, z = @z WHERE Id = @id";
116+
using (SqlCommand cmd = new SqlCommand(query, conn))
117+
{
118+
cmd.Parameters.AddWithValue("@id", point.ID);
119+
cmd.Parameters.AddWithValue("@x", point.X);
120+
cmd.Parameters.AddWithValue("@y", point.Y);
121+
cmd.Parameters.AddWithValue("@z", point.Z);
122+
await cmd.ExecuteNonQueryAsync();
123+
}
124+
}
125+
}
126+
#endregion
64127
}
65128
}

MODSI-SQLRestAPI.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<ItemGroup>
2323
<Folder Include="Properties\" />
2424
</ItemGroup>
25+
<ItemGroup>
26+
<Reference Include="System.Configuration" />
27+
</ItemGroup>
2528
</Project>

0 commit comments

Comments
 (0)