-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (186 loc) · 6.1 KB
/
Copy pathProgram.cs
File metadata and controls
200 lines (186 loc) · 6.1 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//RODAR ESSES COMANDOS NA PRIMEIRA VEZ
//dotnet new webapi -minimal -o NomeDoProjeto
//cd NomeDoProjeto
//dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 6.0
//dotnet add package Microsoft.EntityFrameworkCore.Design --version 6.0
//dotnet tool install --global dotnet-ef
//dotnet ef migrations add InitialCreate
//RODAR ESSE COMANDO SEMPRE QUE MEXER NAS CLASSES RELATIVAS A BASE DE DADOS
//dotnet ef database update
//INCLUIR ESSE NAMESPACE
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace Trabalho
{
class Pessoa
{
public int id { get; set; }
public string? nome { get; set; }
public string? telefone { get; set; }
public string? email { get; set; }
public bool isVeterinario { get; set; }
}
class Paciente
{
public int id { get; set; }
public string? nome { get; set; }
public string? especie { get; set; }
public string? raca { get; set; }
public char? sexo { get; set; }
public int idDono { get; set; }
}
class Agenda
{
public int id { get; set; }
public int idPaciente { get; set; }
public int idVeterinario { get; set; }
public DateTime dataHora { get; set; }
}
class Base : DbContext
{
public Base(DbContextOptions options) : base(options)
{
}
public DbSet<Pessoa> Pessoas { get; set; } = null!;
public DbSet<Paciente> Pacientes { get; set; } = null!;
public DbSet<Agenda> Agendamentos { get; set; } = null!;
}
class Program
{
static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("Base") ?? "Data Source=base.db";
builder.Services.AddSqlite<Base>(connectionString);
var app = builder.Build();
// Cadastrar Pessoa
app.MapPost("/pessoa", (Base db, Pessoa pessoa) =>
{
db.Pessoas.Add(pessoa);
db.SaveChanges();
return "Pessoa adicionado";
});
// Listar todos as Pessoa
app.MapGet("/pessoas", (Base db) => {
return db.Pessoas.ToList();
});
// Listar Pessoas específico (por tipo)
app.MapGet("/pessoas/{type}", (Base db, string type) => {
if (type == "Veterinario") {
return db.Pessoas.Where(p => p.isVeterinario == true);
} else {
return db.Pessoas.Where(p => p.isVeterinario == false);
}
});
// Listar Pessoa específico (por id)
app.MapGet("/pessoa/{id}", (Base db, int id) => {
return db.Pessoas.Find(id);
});
// Deletar Pessoa específico (por id)
app.MapDelete("/pessoa/{id}", (Base db, int id) => {
var vet = db.Pessoas.Find(id);
if (vet != null){
db.SaveChanges();
db.Pessoas.Remove(vet);
return "Pessoa Removido";
} else {
return "Pessoa não existe";
}
});
// Atualizar Pessoa
app.MapPut("/pessoa/{id}", (Base db, Pessoa pessoa, int id) =>
{
var vet = db.Pessoas.Find(id);
if (vet != null)
{
vet.nome = pessoa.nome;
vet.telefone = pessoa.telefone;
db.SaveChanges();
return "Pessoa atualizado";
} else {
return "Pessoa não encontrado";
}
});
// Cadastrar Paciente
app.MapPost("/cadastrarPaciente", (Base db, Paciente paciente) =>
{
db.Pacientes.Add(paciente);
db.SaveChanges();
return "Paciente adicionado";
});
// Listar todos as Pacientes
app.MapGet("/pacientes", (Base db) => {
return db.Pacientes.ToList();
});
// Listar Paciente específico (por id)
app.MapGet("/paciente/{id}", (Base db, int id) => {
return db.Pacientes.Find(id);
});
// Deletar Paciente específico (por id)
app.MapDelete("/paciente/{id}", (Base db, int id) => {
var pac = db.Pacientes.Find(id);
if (pac != null){
db.SaveChanges();
db.Pacientes.Remove(pac);
return "Paciente Removido";
} else {
return "Paciente não existe";
}
});
// Atualizar Paciente
app.MapPut("/paciente/{id}", (Base db, Paciente paciente, int id) =>
{
var pac = db.Pacientes.Find(id);
if (pac != null)
{
pac.nome = paciente.nome;
pac.especie = paciente.especie;
pac.raca = paciente.raca;
pac.sexo = paciente.sexo;
pac.idDono = paciente.idDono;
db.SaveChanges();
return "Paciente atualizado";
} else {
return "Paciente Indisponível";
}
});
// Cadastrar agendamento
app.MapPost("/agendamento", (Base db, Agenda agenda) =>
{
// ConvertToDateTime(agenda);
bool veterinarioIndisponivel = db.Agendamentos.Where(a => a.dataHora == Convert.ToDateTime(agenda.dataHora) && a.idVeterinario == agenda.idVeterinario).ToList().Count() > 0;
if (!veterinarioIndisponivel) {
db.Agendamentos.Add(agenda);
db.SaveChanges();
return "Agenda adicionada";
} else {
return "Veterinário Indisponível";
}
});
// Listar todos as Agendamentos
app.MapGet("/agendamentos", (Base db) => {
return db.Agendamentos.ToList();
});
// Listar Agendamento específico (por id)
app.MapGet("/agendamento/{id}", (Base db, int id) => {
return db.Agendamentos.Find(id);
});
// Deletar Adentamento
app.MapDelete("/agendamento/{id}", (Base db, int id) =>
{
var agenda = db.Agendamentos.Find(id);
if (agenda != null) {
db.Agendamentos.Add(agenda);
db.SaveChanges();
return "Agenda removida";
} else {
return "Agenda Indisponível";
}
});
app.Run();
}
}
}