-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.cs
121 lines (91 loc) · 3.6 KB
/
program.cs
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
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//adding DBContext SQL Azure Database
builder.Services.AddDbContext<FacilityRequetsDb>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("SQLDatabaseConnectionString")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//GET all facility requests
app.MapGet("/allrequests", async (FacilityRequetsDb db) =>
await db.FacilityRequests.ToListAsync());
//GET facility ID request
app.MapGet("/allrequests/{id}", async (FacilityRequetsDb db, int id) =>
await db.FacilityRequests.FindAsync(id)
is FacilityRequest request
? Results.Ok(request)
: Results.NotFound("Sorry, facility ID not found"));
//POST new facility request
app.MapPost("/allrequests", async (FacilityRequetsDb db, FacilityRequest request) =>
{
db.FacilityRequests.Add(request);
await db.SaveChangesAsync();
return Results.Created($"/allrequests/{request.Id}", request);
});
//PUT facility request
app.MapPut("/allrequests/{id}", async (int id, FacilityRequest inputrequest, FacilityRequetsDb db) =>
{
var itemToUpdate = await db.FacilityRequests.FindAsync(id);
if (itemToUpdate is null) return Results.NotFound("Sorry, facility ID not found");
if (inputrequest.IdStatus != null)
itemToUpdate.IdStatus = inputrequest.IdStatus;
if (inputrequest.IdType != null)
itemToUpdate.IdType = inputrequest.IdType;
if (inputrequest.IdRequestor != null)
itemToUpdate.IdRequestor = inputrequest.IdRequestor;
if (inputrequest.IdRequestorEmail != null)
itemToUpdate.IdRequestorEmail = inputrequest.IdRequestorEmail;
if (inputrequest.IdRequestorDepartment != null)
itemToUpdate.IdRequestorDepartment = inputrequest.IdRequestorDepartment;
if (inputrequest.IdRequestorPhone != null)
itemToUpdate.IdRequestorPhone = inputrequest.IdRequestorPhone;
if (inputrequest.IdAssignment != null)
itemToUpdate.IdAssignment = inputrequest.IdAssignment;
await db.SaveChangesAsync();
return Results.Ok(itemToUpdate);
});
//DELETE facility ID request
app.MapDelete("/allrequests/{id}", async (int id, FacilityRequetsDb db) =>
{
if (await db.FacilityRequests.FindAsync(id) is FacilityRequest request)
{
db.FacilityRequests.Remove(request);
await db.SaveChangesAsync();
return Results.Ok(request);
}
return Results.NotFound();
});
app.Run();
class FacilityRequest
{
[Column("id")]
public int Id { get; set; }
[Column("id_status")]
public string? IdStatus { get; set; }
[Column("id_type")]
public string? IdType { get; set; }
[Column("id_requestor")]
public string? IdRequestor { get; set; }
[Column("id_requestor_email")]
public string? IdRequestorEmail { get; set; }
[Column("id_requestor_department")]
public string? IdRequestorDepartment { get; set; }
[Column("id_requestor_phone")]
public string? IdRequestorPhone { get; set; }
[Column("id_assignment")]
public string? IdAssignment { get; set; }
}
class FacilityRequetsDb : DbContext
{
public FacilityRequetsDb(DbContextOptions<FacilityRequetsDb> options)
: base(options) { }
public DbSet<FacilityRequest> FacilityRequests => Set<FacilityRequest>();
}