Добавил слой Infrastructure
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniVerse.Application.DTOs.Sync;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Domain.Entities;
|
||||
using UniVerse.Infrastructure.Data;
|
||||
|
||||
namespace UniVerse.Infrastructure.Services;
|
||||
|
||||
public class ScheduleSyncService : IScheduleSyncService
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly IModeusApiClient _modeus;
|
||||
private readonly ILogger<ScheduleSyncService> _logger;
|
||||
private static SyncStatusDto _lastStatus = new(null, "idle", null);
|
||||
|
||||
public ScheduleSyncService(AppDbContext db, IModeusApiClient modeus, ILogger<ScheduleSyncService> logger)
|
||||
{
|
||||
_db = db; _modeus = modeus; _logger = logger;
|
||||
}
|
||||
|
||||
public async Task<SyncResultDto> SyncScheduleAsync(SyncScheduleRequest request)
|
||||
{
|
||||
int created = 0, updated = 0, skipped = 0;
|
||||
try
|
||||
{
|
||||
var events = await _modeus.SearchEventsAsync(request);
|
||||
foreach (var ev in events.Events)
|
||||
{
|
||||
var existing = await _db.Lectures.FirstOrDefaultAsync(l => l.ExternalId == ev.Id);
|
||||
if (existing != null) { updated++; existing.StartsAt = ev.StartsAt; existing.EndsAt = ev.EndsAt; existing.UpdatedAt = DateTime.UtcNow; }
|
||||
else
|
||||
{
|
||||
var course = await _db.Courses.FirstOrDefaultAsync(c => c.ExternalId == ev.TypeId);
|
||||
if (course == null) { course = new Course { Name = ev.Name, ExternalId = ev.TypeId, IsSynced = true }; _db.Courses.Add(course); await _db.SaveChangesAsync(); }
|
||||
_db.Lectures.Add(new Lecture { CourseId = course.Id, Title = ev.Name, ExternalId = ev.Id, StartsAt = ev.StartsAt, EndsAt = ev.EndsAt });
|
||||
created++;
|
||||
}
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
var result = new SyncResultDto(created, updated, skipped, null);
|
||||
_lastStatus = new SyncStatusDto(DateTime.UtcNow, "completed", result);
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Schedule sync failed");
|
||||
var result = new SyncResultDto(created, updated, skipped, ex.Message);
|
||||
_lastStatus = new SyncStatusDto(DateTime.UtcNow, "failed", result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SyncResultDto> SyncRoomsAsync()
|
||||
{
|
||||
int created = 0, updated = 0;
|
||||
var rooms = await _modeus.SearchRoomsAsync();
|
||||
foreach (var room in rooms.Rooms)
|
||||
{
|
||||
var existing = await _db.Locations.FirstOrDefaultAsync(l => l.ExternalId == room.Id);
|
||||
if (existing != null) { existing.Name = room.Name; existing.Building = room.Building; updated++; }
|
||||
else { _db.Locations.Add(new Location { Name = room.Name, Building = room.Building, ExternalId = room.Id }); created++; }
|
||||
}
|
||||
await _db.SaveChangesAsync();
|
||||
return new SyncResultDto(created, updated, 0, null);
|
||||
}
|
||||
|
||||
public async Task<List<EmployeeDto>> SearchEmployeesAsync(string fullname)
|
||||
{
|
||||
var employees = await _modeus.SearchEmployeeAsync(fullname);
|
||||
return employees.Select(e => new EmployeeDto(e.Id, e.FullName, e.Department)).ToList();
|
||||
}
|
||||
|
||||
public Task<SyncStatusDto> GetLastSyncStatusAsync() => Task.FromResult(_lastStatus);
|
||||
}
|
||||
Reference in New Issue
Block a user