Добавил API проект

This commit is contained in:
2026-04-28 15:53:02 +03:00
parent d64447f0be
commit ee627e4878
20 changed files with 878 additions and 2 deletions
@@ -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();
}
}