-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (89 loc) · 3.7 KB
/
Program.cs
File metadata and controls
107 lines (89 loc) · 3.7 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
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Conexión a MySQL — los valores se leen de variables de entorno o appsettings
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? $"Server={Environment.GetEnvironmentVariable("DB_SERVER") ?? "localhost"};" +
$"Database={Environment.GetEnvironmentVariable("DB_NAME") ?? "clinic_control"};" +
$"Uid={Environment.GetEnvironmentVariable("DB_USER") ?? "root"};" +
$"Pwd={Environment.GetEnvironmentVariable("DB_PASSWORD") ?? ""};";
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 30))));
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
var app = builder.Build();
app.UseCors("AllowAll");
app.MapGet("/", () => "API funcionando - Endpoints: /api/register y /api/login");
app.MapPost("/api/register", async (RegisterRequest request, AppDbContext db) =>
{
if (await db.Users.AnyAsync(u => u.Email == request.Email))
return Results.BadRequest(new { success = false, message = "Email ya registrado" });
var user = new User
{
Email = request.Email,
PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.Password),
Role = request.Role.ToLower(),
IsActive = true
};
db.Users.Add(user);
await db.SaveChangesAsync();
return Results.Ok(new { success = true, message = "Usuario registrado", userId = user.UserId });
});
app.MapPost("/api/login", async (LoginRequest request, AppDbContext db) =>
{
var user = await db.Users.FirstOrDefaultAsync(u => u.Email == request.Email);
if (user == null || !user.IsActive)
return Results.BadRequest(new { success = false, message = "Email o contraseña incorrectos" });
if (!BCrypt.Net.BCrypt.Verify(request.Password, user.PasswordHash))
return Results.BadRequest(new { success = false, message = "Email o contraseña incorrectos" });
return Results.Ok(new
{
success = true,
message = "Login exitoso",
userId = user.UserId,
email = user.Email,
role = user.Role
});
});
app.Run();
public class User
{
public int UserId { get; set; }
public string Email { get; set; } = string.Empty;
public string PasswordHash { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public bool IsActive { get; set; } = true;
}
public class RegisterRequest
{
public string Name { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
}
public class LoginRequest
{
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(entity =>
{
entity.ToTable("users");
entity.HasKey(e => e.UserId);
entity.Property(e => e.UserId).HasColumnName("user_id");
entity.Property(e => e.Email).HasColumnName("email");
entity.Property(e => e.PasswordHash).HasColumnName("password_hash");
entity.Property(e => e.Role).HasColumnName("role");
entity.Property(e => e.IsActive).HasColumnName("is_active");
});
}
}