Добавил API проект
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Api.BackgroundServices;
|
||||
|
||||
public class LlmProcessingBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<LlmProcessingBackgroundService> _logger;
|
||||
|
||||
public LlmProcessingBackgroundService(IServiceProvider services, ILogger<LlmProcessingBackgroundService> logger)
|
||||
{
|
||||
_services = services; _logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("LLM Processing Background Service started");
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var scope = _services.CreateScope();
|
||||
var llmService = scope.ServiceProvider.GetRequiredService<ILlmAnalysisService>();
|
||||
await llmService.ProcessPendingReviewsAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in LLM processing background service");
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Achievements;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/achievements")]
|
||||
[Authorize]
|
||||
public class AchievementsController : ControllerBase
|
||||
{
|
||||
private readonly IAchievementService _achievements;
|
||||
public AchievementsController(IAchievementService achievements) => _achievements = achievements;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll() => Ok(await _achievements.GetAllAsync());
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<AchievementDto>> Get(int id) => Ok(await _achievements.GetByIdAsync(id));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<AchievementDto>> Create([FromBody] CreateAchievementRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _achievements.CreateAsync(req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<AchievementDto>> Update(int id, [FromBody] UpdateAchievementRequest req) =>
|
||||
Ok(await _achievements.UpdateAsync(id, req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id) { await _achievements.DeleteAsync(id); return NoContent(); }
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Auth;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _auth;
|
||||
public AuthController(IAuthService auth) => _auth = auth;
|
||||
|
||||
[HttpPost("login/microsoft")]
|
||||
public async Task<ActionResult<AuthResponse>> LoginMicrosoft([FromBody] LoginMicrosoftRequest request)
|
||||
{
|
||||
var result = await _auth.LoginWithMicrosoftAsync(request.AuthorizationCode);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("login/dev")]
|
||||
public async Task<ActionResult<AuthResponse>> DevLogin([FromBody] DevLoginRequest request)
|
||||
{
|
||||
if (!HttpContext.RequestServices.GetRequiredService<IWebHostEnvironment>().IsDevelopment())
|
||||
return NotFound();
|
||||
var result = await _auth.DevLoginAsync(request.Email, request.DisplayName, request.Role);
|
||||
SetRefreshTokenCookie(result.AccessToken); // simplified: set cookie logic
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("refresh")]
|
||||
public async Task<ActionResult<AuthResponse>> Refresh()
|
||||
{
|
||||
var refreshToken = Request.Cookies["refreshToken"];
|
||||
if (string.IsNullOrEmpty(refreshToken)) return Unauthorized();
|
||||
var result = await _auth.RefreshTokenAsync(refreshToken);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("logout")]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
var refreshToken = Request.Cookies["refreshToken"];
|
||||
if (!string.IsNullOrEmpty(refreshToken))
|
||||
await _auth.RevokeRefreshTokenAsync(refreshToken);
|
||||
Response.Cookies.Delete("refreshToken");
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("me")]
|
||||
public async Task<ActionResult> Me()
|
||||
{
|
||||
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? User.FindFirstValue("sub") ?? "0");
|
||||
var user = await _auth.GetCurrentUserAsync(userId);
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
private void SetRefreshTokenCookie(string token)
|
||||
{
|
||||
Response.Cookies.Append("refreshToken", token, new CookieOptions
|
||||
{
|
||||
HttpOnly = true, Secure = true, SameSite = SameSiteMode.Strict,
|
||||
Expires = DateTime.UtcNow.AddDays(30)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Courses;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/courses")]
|
||||
[Authorize]
|
||||
public class CoursesController : ControllerBase
|
||||
{
|
||||
private readonly ICourseService _courses;
|
||||
public CoursesController(ICourseService courses) => _courses = courses;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll([FromQuery] CourseFilterRequest filter) =>
|
||||
Ok(await _courses.GetAllAsync(filter));
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<CourseDto>> Get(int id) => Ok(await _courses.GetByIdAsync(id));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CourseDto>> Create([FromBody] CreateCourseRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _courses.CreateAsync(req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<CourseDto>> Update(int id, [FromBody] UpdateCourseRequest req) =>
|
||||
Ok(await _courses.UpdateAsync(id, req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id) { await _courses.DeleteAsync(id); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost("{id:int}/tags")]
|
||||
public async Task<IActionResult> AddTag(int id, [FromBody] int tagId)
|
||||
{ await _courses.AddTagAsync(id, tagId); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}/tags/{tagId:int}")]
|
||||
public async Task<IActionResult> RemoveTag(int id, int tagId)
|
||||
{ await _courses.RemoveTagAsync(id, tagId); return NoContent(); }
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Common;
|
||||
using UniVerse.Application.DTOs.Lectures;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/lectures")]
|
||||
[Authorize]
|
||||
public class LecturesController : ControllerBase
|
||||
{
|
||||
private readonly ILectureService _lectures;
|
||||
private readonly IReviewService _reviews;
|
||||
public LecturesController(ILectureService lectures, IReviewService reviews)
|
||||
{ _lectures = lectures; _reviews = reviews; }
|
||||
private int CurrentUserId => int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub") ?? "0");
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll([FromQuery] LectureFilterRequest filter) =>
|
||||
Ok(await _lectures.GetAllAsync(filter));
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult> Get(int id) =>
|
||||
Ok(await _lectures.GetByIdAsync(id, CurrentUserId));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<LectureDto>> Create([FromBody] CreateLectureRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _lectures.CreateAsync(req));
|
||||
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<LectureDto>> Update(int id, [FromBody] UpdateLectureRequest req) =>
|
||||
Ok(await _lectures.UpdateAsync(id, req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id) { await _lectures.DeleteAsync(id); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Student")]
|
||||
[HttpPost("{id:int}/enroll")]
|
||||
public async Task<IActionResult> Enroll(int id) { await _lectures.EnrollAsync(id, CurrentUserId); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Student")]
|
||||
[HttpDelete("{id:int}/enroll")]
|
||||
public async Task<IActionResult> Unenroll(int id) { await _lectures.UnenrollAsync(id, CurrentUserId); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
[HttpPatch("{id:int}/attendance/{userId:int}")]
|
||||
public async Task<IActionResult> Attendance(int id, int userId, [FromBody] bool attended)
|
||||
{ await _lectures.MarkAttendanceAsync(id, userId, attended); return NoContent(); }
|
||||
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
[HttpGet("{id:int}/enrollments")]
|
||||
public async Task<ActionResult> Enrollments(int id, [FromQuery] PaginationRequest pagination) =>
|
||||
Ok(await _lectures.GetEnrollmentsAsync(id, pagination));
|
||||
|
||||
[HttpGet("{id:int}/reviews")]
|
||||
public async Task<ActionResult> Reviews(int id, [FromQuery] PaginationRequest pagination) =>
|
||||
Ok(await _reviews.GetByLectureAsync(id, pagination));
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Locations;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/locations")]
|
||||
[Authorize]
|
||||
public class LocationsController : ControllerBase
|
||||
{
|
||||
private readonly ILocationService _locations;
|
||||
public LocationsController(ILocationService locations) => _locations = locations;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll() => Ok(await _locations.GetAllAsync());
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<LocationDto>> Get(int id) => Ok(await _locations.GetByIdAsync(id));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<LocationDto>> Create([FromBody] CreateLocationRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _locations.CreateAsync(req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<LocationDto>> Update(int id, [FromBody] UpdateLocationRequest req) =>
|
||||
Ok(await _locations.UpdateAsync(id, req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id) { await _locations.DeleteAsync(id); return NoContent(); }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Common;
|
||||
using UniVerse.Application.DTOs.Reviews;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/reviews")]
|
||||
[Authorize]
|
||||
public class ReviewsController : ControllerBase
|
||||
{
|
||||
private readonly IReviewService _reviews;
|
||||
public ReviewsController(IReviewService reviews) => _reviews = reviews;
|
||||
private int CurrentUserId => int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub") ?? "0");
|
||||
|
||||
[Authorize(Roles = "Student")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<ReviewDto>> Create([FromBody] CreateReviewRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _reviews.CreateAsync(CurrentUserId, req));
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<ReviewDto>> Get(int id) => Ok(await _reviews.GetByIdAsync(id));
|
||||
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<ReviewDto>> Update(int id, [FromBody] UpdateReviewRequest req) =>
|
||||
Ok(await _reviews.UpdateAsync(id, CurrentUserId, req));
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
await _reviews.DeleteAsync(id, CurrentUserId, User.IsInRole("Admin"));
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpGet("pending")]
|
||||
public async Task<ActionResult> Pending([FromQuery] PaginationRequest pagination) =>
|
||||
Ok(await _reviews.GetPendingAsync(pagination));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost("{id:int}/reanalyze")]
|
||||
public async Task<IActionResult> Reanalyze(int id) { await _reviews.ReanalyzeAsync(id); return NoContent(); }
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Sync;
|
||||
using UniVerse.Application.Interfaces;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/sync")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class SyncController : ControllerBase
|
||||
{
|
||||
private readonly IScheduleSyncService _sync;
|
||||
public SyncController(IScheduleSyncService sync) => _sync = sync;
|
||||
|
||||
[HttpPost("schedule")]
|
||||
public async Task<ActionResult<SyncResultDto>> SyncSchedule([FromBody] SyncScheduleRequest req) =>
|
||||
Ok(await _sync.SyncScheduleAsync(req));
|
||||
|
||||
[HttpGet("status")]
|
||||
public async Task<ActionResult<SyncStatusDto>> Status() =>
|
||||
Ok(await _sync.GetLastSyncStatusAsync());
|
||||
|
||||
[HttpPost("rooms")]
|
||||
public async Task<ActionResult<SyncResultDto>> SyncRooms() =>
|
||||
Ok(await _sync.SyncRoomsAsync());
|
||||
|
||||
[HttpPost("employees")]
|
||||
public async Task<ActionResult> SearchEmployees([FromQuery] string fullname) =>
|
||||
Ok(await _sync.SearchEmployeesAsync(fullname));
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Tags;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Domain.Enums;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/tags")]
|
||||
[Authorize]
|
||||
public class TagsController : ControllerBase
|
||||
{
|
||||
private readonly ITagService _tags;
|
||||
public TagsController(ITagService tags) => _tags = tags;
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll([FromQuery] TagType? type, [FromQuery] int? parentId) =>
|
||||
Ok(await _tags.GetAllAsync(type, parentId));
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<TagDto>> Get(int id) => Ok(await _tags.GetByIdAsync(id));
|
||||
|
||||
[HttpGet("tree")]
|
||||
public async Task<ActionResult> GetTree() => Ok(await _tags.GetTreeAsync());
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<TagDto>> Create([FromBody] CreateTagRequest req) =>
|
||||
CreatedAtAction(nameof(Get), new { id = 0 }, await _tags.CreateAsync(req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<TagDto>> Update(int id, [FromBody] UpdateTagRequest req) =>
|
||||
Ok(await _tags.UpdateAsync(id, req));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id) { await _tags.DeleteAsync(id); return NoContent(); }
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using UniVerse.Application.DTOs.Common;
|
||||
using UniVerse.Application.DTOs.Users;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Domain.Enums;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace UniVerse.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/users")]
|
||||
[Authorize]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly IUserService _users;
|
||||
private readonly IReviewService _reviews;
|
||||
private readonly IGamificationService _gamification;
|
||||
public UsersController(IUserService users, IReviewService reviews, IGamificationService gamification)
|
||||
{
|
||||
_users = users; _reviews = reviews; _gamification = gamification;
|
||||
}
|
||||
private int CurrentUserId => int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub") ?? "0");
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<ActionResult<UserDto>> Get(int id) => Ok(await _users.GetByIdAsync(id));
|
||||
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<ActionResult<UserDto>> Update(int id, [FromBody] UpdateUserRequest req)
|
||||
{
|
||||
if (CurrentUserId != id && !User.IsInRole("Admin")) return Forbid();
|
||||
return Ok(await _users.UpdateProfileAsync(id, req));
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/stats")]
|
||||
public async Task<ActionResult<UserStatsDto>> Stats(int id) => Ok(await _users.GetStatsAsync(id));
|
||||
|
||||
[HttpGet("{id:int}/enrollments")]
|
||||
public async Task<ActionResult> Enrollments(int id, [FromQuery] PaginationRequest pagination)
|
||||
{
|
||||
if (CurrentUserId != id && !User.IsInRole("Admin")) return Forbid();
|
||||
// Delegate to lecture service would be more proper, but returning reviews for now
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}/reviews")]
|
||||
public async Task<ActionResult> Reviews(int id, [FromQuery] PaginationRequest pagination) =>
|
||||
Ok(await _reviews.GetByUserAsync(id, pagination));
|
||||
|
||||
[HttpGet("{id:int}/achievements")]
|
||||
public async Task<ActionResult> Achievements(int id) =>
|
||||
Ok(await _gamification.GetUserAchievementsAsync(id));
|
||||
|
||||
[HttpGet("{id:int}/transactions")]
|
||||
public async Task<ActionResult> Transactions(int id, [FromQuery] PaginationRequest pagination)
|
||||
{
|
||||
if (CurrentUserId != id && !User.IsInRole("Admin")) return Forbid();
|
||||
return Ok(await _gamification.GetTransactionsAsync(id, pagination));
|
||||
}
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetAll([FromQuery] UserFilterRequest filter) =>
|
||||
Ok(await _users.GetAllAsync(filter));
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPatch("{id:int}/role")]
|
||||
public async Task<IActionResult> SetRole(int id, [FromBody] UserRole role)
|
||||
{
|
||||
await _users.SetRoleAsync(id, role);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[Authorize(Roles = "Admin")]
|
||||
[HttpPatch("{id:int}/active")]
|
||||
public async Task<IActionResult> SetActive(int id, [FromBody] bool isActive)
|
||||
{
|
||||
await _users.SetActiveAsync(id, isActive);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["UniVerse/UniVerse.csproj", "UniVerse/"]
|
||||
RUN dotnet restore "UniVerse/UniVerse.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/UniVerse"
|
||||
RUN dotnet build "./UniVerse.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./UniVerse.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "UniVerse.dll"]
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using UniVerse.Domain.Exceptions;
|
||||
|
||||
namespace UniVerse.Api.Middleware;
|
||||
|
||||
public class ExceptionHandlingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
|
||||
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
|
||||
{
|
||||
_next = next; _logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
try { await _next(context); }
|
||||
catch (Exception ex) { await HandleExceptionAsync(context, ex); }
|
||||
}
|
||||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
var (statusCode, title) = exception switch
|
||||
{
|
||||
NotFoundException => ((int)HttpStatusCode.NotFound, "Not Found"),
|
||||
ForbiddenException => ((int)HttpStatusCode.Forbidden, "Forbidden"),
|
||||
ConflictException => ((int)HttpStatusCode.Conflict, "Conflict"),
|
||||
UnauthorizedAccessException => ((int)HttpStatusCode.Unauthorized, "Unauthorized"),
|
||||
_ => ((int)HttpStatusCode.InternalServerError, "Internal Server Error")
|
||||
};
|
||||
|
||||
if (statusCode == 500)
|
||||
_logger.LogError(exception, "Unhandled exception");
|
||||
else
|
||||
_logger.LogWarning("Handled exception: {Message}", exception.Message);
|
||||
|
||||
context.Response.ContentType = "application/problem+json";
|
||||
context.Response.StatusCode = statusCode;
|
||||
|
||||
var problem = new
|
||||
{
|
||||
type = $"https://httpstatuses.com/{statusCode}",
|
||||
title,
|
||||
status = statusCode,
|
||||
detail = exception.Message,
|
||||
traceId = context.TraceIdentifier
|
||||
};
|
||||
|
||||
await context.Response.WriteAsync(JsonSerializer.Serialize(problem,
|
||||
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace UniVerse.Api.Middleware;
|
||||
|
||||
public class RequestLoggingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<RequestLoggingMiddleware> _logger;
|
||||
|
||||
public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger)
|
||||
{
|
||||
_next = next; _logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
await _next(context);
|
||||
sw.Stop();
|
||||
_logger.LogInformation("{Method} {Path} → {StatusCode} ({Elapsed}ms)",
|
||||
context.Request.Method, context.Request.Path,
|
||||
context.Response.StatusCode, sw.ElapsedMilliseconds);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Serilog;
|
||||
using UniVerse.Api.BackgroundServices;
|
||||
using UniVerse.Api.Middleware;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Infrastructure.Services;
|
||||
using UniVerse.Infrastructure.Data;
|
||||
using UniVerse.Infrastructure.ExternalServices;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// --- Serilog ---
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
// --- DbContext ---
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"),
|
||||
npgsql =>
|
||||
{
|
||||
npgsql.EnableRetryOnFailure(3);
|
||||
npgsql.MigrationsAssembly("UniVerse.Infrastructure");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Authentication ---
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
||||
ValidAudience = builder.Configuration["Jwt:Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(
|
||||
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Secret"] ?? "default-dev-secret-key-change-in-production-32chars!!"))
|
||||
};
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
// --- CORS ---
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.WithOrigins(
|
||||
builder.Configuration.GetSection("Cors:Origins").Get<string[]>()
|
||||
?? ["http://localhost:5173", "http://localhost:3000"])
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials();
|
||||
});
|
||||
});
|
||||
|
||||
// --- Services DI ---
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
builder.Services.AddScoped<ITagService, TagService>();
|
||||
builder.Services.AddScoped<ILocationService, LocationService>();
|
||||
builder.Services.AddScoped<ICourseService, CourseService>();
|
||||
builder.Services.AddScoped<ILectureService, LectureService>();
|
||||
builder.Services.AddScoped<IReviewService, ReviewService>();
|
||||
builder.Services.AddScoped<IGamificationService, GamificationService>();
|
||||
builder.Services.AddScoped<IAchievementService, AchievementService>();
|
||||
builder.Services.AddScoped<ILlmAnalysisService, LlmAnalysisService>();
|
||||
builder.Services.AddScoped<IScheduleSyncService, ScheduleSyncService>();
|
||||
|
||||
// --- HTTP Clients ---
|
||||
builder.Services.AddHttpClient<ILlmClient, LlmClient>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(builder.Configuration["Llm:BaseUrl"] ?? "https://api.openai.com/v1/");
|
||||
client.Timeout = TimeSpan.FromSeconds(60);
|
||||
});
|
||||
|
||||
builder.Services.AddHttpClient<IModeusApiClient, ModeusApiClient>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(builder.Configuration["ModeusApi:BaseUrl"] ?? "https://schedule.rdcenter.ru");
|
||||
client.Timeout = TimeSpan.FromSeconds(30);
|
||||
});
|
||||
|
||||
// --- Background Services ---
|
||||
builder.Services.AddHostedService<LlmProcessingBackgroundService>();
|
||||
|
||||
// --- Controllers ---
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(o =>
|
||||
{
|
||||
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
o.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||
});
|
||||
|
||||
// --- Swagger ---
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo
|
||||
{
|
||||
Title = "UniVerse API",
|
||||
Version = "v1",
|
||||
Description = "University schedule, reviews, and gamification platform"
|
||||
});
|
||||
|
||||
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
||||
{
|
||||
Name = "Authorization",
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
BearerFormat = "JWT",
|
||||
In = ParameterLocation.Header,
|
||||
Description = "Enter your JWT token"
|
||||
});
|
||||
|
||||
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// --- Middleware Pipeline ---
|
||||
app.UseMiddleware<RequestLoggingMiddleware>();
|
||||
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "UniVerse API v1"));
|
||||
}
|
||||
|
||||
app.UseCors();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5019",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>UniVerse.Api</RootNamespace>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<AllowMissingPrunePackageData>true</AllowMissingPrunePackageData>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.0">
|
||||
<PackageReference Include="Microsoft.OpenApi" Version="3.5.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UniVerse.Application\UniVerse.Application.csproj" />
|
||||
<ProjectReference Include="..\UniVerse.Infrastructure\UniVerse.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Host=localhost;Port=5432;Database=universe;Username=postgres;Password=postgres"
|
||||
},
|
||||
"Jwt": {
|
||||
"Secret": "default-dev-secret-key-change-in-production-32chars!!",
|
||||
"Issuer": "UniVerse",
|
||||
"Audience": "UniVerse",
|
||||
"AccessTokenExpirationMinutes": "30",
|
||||
"RefreshTokenExpirationDays": "30"
|
||||
},
|
||||
"Cors": {
|
||||
"Origins": [
|
||||
"http://localhost:5173",
|
||||
"http://localhost:3000"
|
||||
]
|
||||
},
|
||||
"Llm": {
|
||||
"BaseUrl": "https://api.openai.com/v1/",
|
||||
"ApiKey": "",
|
||||
"Model": "gpt-4o-mini"
|
||||
},
|
||||
"ModeusApi": {
|
||||
"BaseUrl": "https://schedule.rdcenter.ru",
|
||||
"ApiKey": ""
|
||||
},
|
||||
"Gamification": {
|
||||
"XpThresholds": [0, 100, 300, 600, 1000, 1500, 2500, 4000]
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"System": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user