-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (117 loc) · 4.08 KB
/
Copy pathProgram.cs
File metadata and controls
134 lines (117 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Linq;
using lemmikkiAPI_esimerkki;
public partial class Program
{
public static void Main(string[] args)
{
// Create a new web application builder
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Initialize the database manager with the database name
DBManager dBManager = new DBManager("DataBase");
// Middleware to log each request
app.Use(async (context, next) =>
{
Console.WriteLine($"Request: {context.Request.Method} {context.Request.Path}");
await next.Invoke();
});
// Redirect root URL to the index.html page
app.MapGet("/", () => Results.Redirect("/index.html"));
// Endpoint to add a new pet
app.MapPost("/API/V1/Pets", (Pet pet) =>
{
bool success = dBManager.PostLemmikki(pet.name, pet.type, pet.omistaja_name);
if (!success)
{
return Results.BadRequest("Owner not found. Pet could not be added.");
}
return Results.Ok("Pet added successfully.");
});
// Endpoint to add a new owner
app.MapPost("/API/V1/Owners", (Owner owner) =>
{
dBManager.PostOmistaja(owner.name, owner.number);
});
// Endpoint to get the owner's number by pet name
app.MapGet("/API/V1/Pets/{petName}/Owner/Number", (string petName) =>
{
try
{
return dBManager.GetOmistajaNumberFromLemmikkiName(petName);
}
catch (Exception ex)
{
return Results.BadRequest(ex.Message); // Return error message
}
});
// Endpoint to get all owners
app.MapGet("/API/V1/Owners", () =>
{
return dBManager.GetAllOwners();
});
// Endpoint to get all pets
app.MapGet("/API/V1/Pets", () =>
{
return dBManager.GetAllPets();
});
// Endpoint to update an owner's number
app.MapPost("/API/V1/Owners/Update", (UpdateOwnerRequest request) =>
{
if (request.Id <= 0 || string.IsNullOrEmpty(request.NewNumber))
{
return Results.BadRequest("Id and NewNumber are required.");
}
dBManager.UpdateOmistajaNumber(request.Id, request.NewNumber);
return Results.Ok();
});
// Endpoint to update an owner's number using PUT
app.MapPut("/API/V1/Owners/{id}", (int id, UpdateOwnerRequest request) =>
{
if (string.IsNullOrEmpty(request.NewNumber))
{
return Results.BadRequest("NewNumber is required.");
}
try
{
dBManager.UpdateOmistajaNumber(id, request.NewNumber);
return Results.Ok("Owner number updated successfully.");
}
catch (Exception ex)
{
return Results.BadRequest(ex.Message); // Return error message
}
});
// Endpoint to delete an owner
app.MapDelete("/API/V1/Owners/{id}", (int id) =>
{
try
{
dBManager.DeleteOwner(id);
return Results.Ok("Owner deleted successfully.");
}
catch (Exception ex)
{
return Results.BadRequest(ex.Message); // Return error message
}
});
// Endpoint to delete a pet
app.MapDelete("/API/V1/Pets/{id}", (int id) =>
{
try
{
dBManager.DeletePet(id);
return Results.Ok("Pet deleted successfully.");
}
catch (Exception ex)
{
return Results.BadRequest(ex.Message); // Return error message
}
});
// Serve static files like index.html
app.UseStaticFiles(); // This should be after all route definitions
// Run the application
app.Run();
}
}