mirror of
https://github.com/opelly27/WinStudentGoalTracker.git
synced 2026-05-20 02:57:36 +00:00
API Silent Merge fail
This commit is contained in:
@@ -1,9 +1,52 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WinStudentGoalTracker.Api.Configuration;
|
||||
using WinStudentGoalTracker.Services;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
ConfigHelper.Configuration = builder.Configuration;
|
||||
|
||||
var jwtKey = builder.Configuration["Jwt:Key"] ?? "super_secret_key_change_me_in_production_123!";
|
||||
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "WinStudentGoalTrackerAPI";
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = jwtIssuer,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)),
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
RoleClaimType = System.Security.Claims.ClaimTypes.Role
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddScoped<TokenService>();
|
||||
|
||||
builder.Services.AddHttpClient<TranscriptionService>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri("https://stt.opelly.me");
|
||||
client.Timeout = TimeSpan.FromMinutes(5);
|
||||
});
|
||||
|
||||
builder.Services.AddHttpClient<OllamaService>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(builder.Configuration["Ollama:BaseUrl"] ?? "https://llm.opelly.me");
|
||||
client.Timeout = TimeSpan.FromMinutes(3);
|
||||
});
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
@@ -25,6 +68,10 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
app.UseCors();
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
||||
<PackageReference Include="MySql.Data" Version="8.4.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=localhost;Port=3306;Database=win_student_goal_tracker;Uid=root;Pwd=change_me;"
|
||||
},
|
||||
"Jwt": {
|
||||
"Key": "super_secret_key_change_me_in_production_123!",
|
||||
"Issuer": "WinStudentGoalTrackerAPI"
|
||||
},
|
||||
"Ollama": {
|
||||
"BaseUrl": "https://llm.opelly.me",
|
||||
"Model": "gpt-oss:20b"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
@@ -5,14 +5,14 @@ namespace WinStudentGoalTracker.BaseClasses;
|
||||
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
protected (int userId, ActionResult? error) GetUserIdFromClaims()
|
||||
protected (Guid userId, ActionResult? error) GetUserIdFromClaims()
|
||||
{
|
||||
var userIdClaim = User.FindFirst("user_id")?.Value
|
||||
?? User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(userIdClaim) || !int.TryParse(userIdClaim, out var userId))
|
||||
if (string.IsNullOrWhiteSpace(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
||||
{
|
||||
return (0, Unauthorized("Missing or invalid user_id claim."));
|
||||
return (Guid.Empty, Unauthorized("Missing or invalid user_id claim."));
|
||||
}
|
||||
|
||||
return (userId, null);
|
||||
|
||||
@@ -29,10 +29,10 @@ public class StudentController : BaseController
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet("{idStudent:int}")]
|
||||
[HttpGet("{idStudent:guid}")]
|
||||
[ProducesResponseType(typeof(ResponseResult<StudentResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ResponseResult<StudentResponse>), StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<ResponseResult<StudentResponse>>> GetById(int idStudent)
|
||||
public async Task<ActionResult<ResponseResult<StudentResponse>>> GetById(Guid idStudent)
|
||||
{
|
||||
var student = await _studentRepository.GetByIdAsync(idStudent);
|
||||
if (student is null)
|
||||
@@ -84,10 +84,10 @@ public class StudentController : BaseController
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut("{idStudent:int}")]
|
||||
[HttpPut("{idStudent:guid}")]
|
||||
[ProducesResponseType(typeof(ResponseResult<StudentResponse>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ResponseResult<StudentResponse>), StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<ResponseResult<StudentResponse>>> Update(int idStudent, [FromBody] UpdateStudentDto request)
|
||||
public async Task<ActionResult<ResponseResult<StudentResponse>>> Update(Guid idStudent, [FromBody] UpdateStudentDto request)
|
||||
{
|
||||
var existing = await _studentRepository.GetByIdAsync(idStudent);
|
||||
if (existing is null)
|
||||
@@ -118,10 +118,10 @@ public class StudentController : BaseController
|
||||
});
|
||||
}
|
||||
|
||||
[HttpDelete("{idStudent:int}")]
|
||||
[HttpDelete("{idStudent:guid}")]
|
||||
[ProducesResponseType(typeof(ResponseResult<object>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ResponseResult<object>), StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<ResponseResult<object>>> Delete(int idStudent)
|
||||
public async Task<ActionResult<ResponseResult<object>>> Delete(Guid idStudent)
|
||||
{
|
||||
var deleted = await _studentRepository.DeleteAsync(idStudent);
|
||||
if (!deleted)
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace WinStudentGoalTracker.DataAccess;
|
||||
|
||||
public class CreateStudentDto
|
||||
{
|
||||
public required int IdStudent { get; set; }
|
||||
public int? IdProgram { get; set; }
|
||||
public required Guid IdStudent { get; set; }
|
||||
public Guid? IdProgram { get; set; }
|
||||
public string? Identifier { get; set; }
|
||||
public int? ProgramYear { get; set; }
|
||||
public DateTime? EnrollmentDate { get; set; }
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace WinStudentGoalTracker.DataAccess;
|
||||
|
||||
public class UpdateStudentDto
|
||||
{
|
||||
public int? IdProgram { get; set; }
|
||||
public Guid? IdProgram { get; set; }
|
||||
public string? Identifier { get; set; }
|
||||
public int? ProgramYear { get; set; }
|
||||
public DateTime? EnrollmentDate { get; set; }
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace WinStudentGoalTracker.DataAccess;
|
||||
|
||||
public class dbStudent
|
||||
{
|
||||
public required int IdStudent { get; set; }
|
||||
public int? IdProgram { get; set; }
|
||||
public required Guid IdStudent { get; set; }
|
||||
public Guid? IdProgram { get; set; }
|
||||
public string? Identifier { get; set; }
|
||||
public int? ProgramYear { get; set; }
|
||||
public DateTime? EnrollmentDate { get; set; }
|
||||
|
||||
@@ -16,12 +16,12 @@ public class StudentRepository
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<dbStudent?> GetByIdAsync(int idStudent)
|
||||
public async Task<dbStudent?> GetByIdAsync(Guid idStudent)
|
||||
{
|
||||
using var db = Connection;
|
||||
return await db.QuerySingleOrDefaultAsync<dbStudent>(
|
||||
"sp_Student_GetById",
|
||||
new { p_id_student = idStudent },
|
||||
new { p_id_student = idStudent.ToString() },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ public class StudentRepository
|
||||
"sp_Student_Insert",
|
||||
new
|
||||
{
|
||||
p_id_student = dto.IdStudent,
|
||||
p_id_program = dto.IdProgram,
|
||||
p_id_student = dto.IdStudent.ToString(),
|
||||
p_id_program = dto.IdProgram?.ToString(),
|
||||
p_identifier = dto.Identifier,
|
||||
p_program_year = dto.ProgramYear,
|
||||
p_enrollment_date = dto.EnrollmentDate,
|
||||
@@ -42,15 +42,15 @@ public class StudentRepository
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(int idStudent, UpdateStudentDto dto)
|
||||
public async Task<bool> UpdateAsync(Guid idStudent, UpdateStudentDto dto)
|
||||
{
|
||||
using var db = Connection;
|
||||
var rowsAffected = await db.ExecuteScalarAsync<int>(
|
||||
"sp_Student_Update",
|
||||
new
|
||||
{
|
||||
p_id_student = idStudent,
|
||||
p_id_program = dto.IdProgram,
|
||||
p_id_student = idStudent.ToString(),
|
||||
p_id_program = dto.IdProgram?.ToString(),
|
||||
p_identifier = dto.Identifier,
|
||||
p_program_year = dto.ProgramYear,
|
||||
p_enrollment_date = dto.EnrollmentDate,
|
||||
@@ -60,12 +60,12 @@ public class StudentRepository
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int idStudent)
|
||||
public async Task<bool> DeleteAsync(Guid idStudent)
|
||||
{
|
||||
using var db = Connection;
|
||||
var rowsAffected = await db.ExecuteScalarAsync<int>(
|
||||
"sp_Student_Delete",
|
||||
new { p_id_student = idStudent },
|
||||
new { p_id_student = idStudent.ToString() },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ namespace WinStudentGoalTracker.Models;
|
||||
|
||||
public class StudentResponse
|
||||
{
|
||||
public int IdStudent { get; set; }
|
||||
public int? IdProgram { get; set; }
|
||||
public Guid IdStudent { get; set; }
|
||||
public Guid? IdProgram { get; set; }
|
||||
public string? Identifier { get; set; }
|
||||
public int? ProgramYear { get; set; }
|
||||
public DateTime? EnrollmentDate { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user