-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
160 lines (91 loc) · 4.17 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using EntityFrame.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using EntityFrame.Models;
var builder = WebApplication.CreateBuilder(args);
//builder.
//Services.
//AddDbContext<APIContext>(options => options.UseInMemoryDatabase("API"));
//NO pueden usarse dos configuraciones para un solo contexto, es decir, dos conexiones, ya que eso dará
//un error de conflicto porque
builder.Services.AddNpgsql<APIContext>(builder.Configuration.GetConnectionString("API"));
var app = builder.Build();
app.MapGet("/", async ([FromServices] APIContext databasecontext ) =>
{
databasecontext.Database.EnsureCreated();
return Results.Ok("DB created " + databasecontext.Database.IsInMemory() );
});
//ejemplo de get con data. FromServices nos permite acceder a los servicios y al contexto
//de EntityFramework, dandonos acceso a los modelos y los registros
app.MapGet("/api/clients", async ([FromServices] APIContext dbContext) => {
return Results.Ok(dbContext.Clients);
});
//------------------------------------------------CLIENTES----------------------------------------------------
//ejemplo de get con filtrado
app.MapGet("/api/clients/filter", async ([FromServices] APIContext dbContext) => {
return Results.Ok(dbContext.Clients.Where(c => c.nombre_apellido == "cayo legal"));
});
//ejemplo de filtrado dinámico
app.MapGet("/api/clients/{cedula}", async ([FromServices] APIContext dbContext, string cedula ) => {
var findClient = dbContext.Clients.Find(cedula);
if(findClient == null)
{
return Results.NotFound("Cliente no registrado");
}
return Results.Ok(dbContext.Clients.Where(c => c.cedula == cedula));
});
//ejemplo de envio de datos
app.MapPost("api/clients", async ([FromServices] APIContext dbContext, [FromBody] Client client) => {
var newClientValidate = dbContext.Clients.Find(client.cedula);
if (newClientValidate != null)
{
return Results.Conflict("Cliente ya se encuentra registrado");
}
await dbContext.AddAsync(client);
//luego de hacer cualquier cambio sobre el contexto, debemos guardar los datos,
//con el método asíncrono de SaveChanges
await dbContext.SaveChangesAsync();
return Results.Ok();
});
//ejemplo de actualización de registro
app.MapPut("api/clients/{cedula}",
async ([FromServices] APIContext dbContext,
[FromBody] Client client,[FromRoute] string cedula) => {
var findClient = dbContext.Clients.Find(cedula);
if(findClient != null)
{
findClient.tipo_doc = client.tipo_doc;
findClient.nombre_apellido = client.nombre_apellido;
await dbContext.SaveChangesAsync();
return Results.Ok();
}
return Results.NotFound("Cliente no registrado");
});
//ejemplo de eliminación de registro
app.MapDelete("api/clients/{cedula}",
async ([FromServices] APIContext dbContext,[FromRoute] string cedula) => {
var findClient = dbContext.Clients.Find(cedula);
if (findClient != null)
{
dbContext.Remove(findClient);
await dbContext.SaveChangesAsync();
return Results.Ok();
}
return Results.NotFound("Cliente no registrado");
});
//----------------------------------------------------CLIENTES----------------------------------------------------
//-----------------------------------------------------BANCOS-----------------------------------------------------
app.MapGet("/api/banks", async ([FromServices] APIContext dbContext) => {
return Results.Ok(dbContext.Banks);
});
//-----------------------------------------------------BANCOS------------------------------------------------
//-----------------------------------------------------CUENTAS------------------------------------------------
app.MapGet("/api/accounts", async ([FromServices] APIContext dbContext) => {
return Results.Ok(dbContext.Accounts);
});
//-----------------------------------------------------CUENTAS------------------------------------------------
//-----------------------------------------------------TRANSFERENCIAS-----------------------------------------
app.MapGet("/api/transfers", async ([FromServices] APIContext dbContext) => {
return Results.Ok(dbContext.Transfers);
});
app.Run();