159 lines
7.3 KiB
C#
159 lines
7.3 KiB
C#
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Tiku.Application.Content;
|
|
using Tiku.Application.Learning;
|
|
using Tiku.Domain.Catalog;
|
|
using Tiku.Domain.Common;
|
|
using Tiku.Domain.Content;
|
|
using Tiku.Domain.Learning;
|
|
using Tiku.Domain.Operations;
|
|
|
|
namespace Tiku.Infrastructure.Content;
|
|
|
|
internal abstract partial class DirectContentServiceBase
|
|
{
|
|
protected async Task<Banner> UpsertBannerAsync(DirectContentActor actor, OperationContentCommand command,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await AssertReferenceAsync<Region>(actor.TenantId, command.RegionId, "region_not_found", cancellationToken);
|
|
var item = await ResolveByIdOrLegacyAsync(jobsOperationsPersistence.Banners, actor.TenantId, command.Id, command.LegacyId,
|
|
cancellationToken);
|
|
var isNew = item is null;
|
|
item ??= new Banner { Id = command.Id ?? Guid.NewGuid(), TenantId = actor.TenantId };
|
|
item.RegionId = command.RegionId;
|
|
item.LegacyId = Normalize(command.LegacyId);
|
|
item.Title = Normalize(command.Title);
|
|
item.Subtitle = Normalize(command.Subtitle);
|
|
item.Content = Normalize(command.Content);
|
|
item.ButtonText = Normalize(command.ButtonText);
|
|
item.ButtonLink = Normalize(command.ButtonLink);
|
|
item.BackgroundColor = Normalize(command.BackgroundColor);
|
|
item.BorderColor = Normalize(command.BorderColor);
|
|
item.SortOrder = command.Order ?? item.SortOrder;
|
|
item.IsActive = command.IsActive ?? item.IsActive;
|
|
if (isNew) jobsOperationsPersistence.Banners.Add(item);
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return item;
|
|
}
|
|
|
|
protected async Task<Faq> UpsertFaqAsync(DirectContentActor actor, OperationContentCommand command,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await AssertReferenceAsync<Region>(actor.TenantId, command.RegionId, "region_not_found", cancellationToken);
|
|
var item = await ResolveByIdOrLegacyAsync(jobsOperationsPersistence.Faqs, actor.TenantId, command.Id, command.LegacyId,
|
|
cancellationToken);
|
|
var isNew = item is null;
|
|
item ??= new Faq { Id = command.Id ?? Guid.NewGuid(), TenantId = actor.TenantId };
|
|
item.RegionId = command.RegionId;
|
|
item.LegacyId = Normalize(command.LegacyId);
|
|
item.Question = Normalize(command.Question) ?? Normalize(command.Title);
|
|
item.Answer = Normalize(command.Answer) ?? Normalize(command.Content);
|
|
item.SortOrder = command.Order ?? item.SortOrder;
|
|
item.IsActive = command.IsActive ?? item.IsActive;
|
|
if (isNew) jobsOperationsPersistence.Faqs.Add(item);
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return item;
|
|
}
|
|
|
|
protected async Task<Announcement> UpsertAnnouncementAsync(DirectContentActor actor, OperationContentCommand command,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var item = await ResolveByIdOrLegacyAsync(jobsOperationsPersistence.Announcements, actor.TenantId, command.Id, command.LegacyId,
|
|
cancellationToken);
|
|
var isNew = item is null;
|
|
item ??= new Announcement { Id = command.Id ?? Guid.NewGuid(), TenantId = actor.TenantId };
|
|
item.LegacyId = Normalize(command.LegacyId);
|
|
item.Content = Normalize(command.Content) ?? Normalize(command.Title);
|
|
item.Link = Normalize(command.Link);
|
|
item.BackgroundColor = Normalize(command.BackgroundColor);
|
|
item.SortOrder = command.Order ?? item.SortOrder;
|
|
item.IsActive = command.IsActive ?? item.IsActive;
|
|
if (isNew) jobsOperationsPersistence.Announcements.Add(item);
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return item;
|
|
}
|
|
|
|
protected async Task<ExamDate> UpsertExamDateAsync(DirectContentActor actor, OperationContentCommand command,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(command.ExamName);
|
|
await AssertReferenceAsync<Region>(actor.TenantId, command.RegionId, "region_not_found", cancellationToken);
|
|
await AssertReferenceAsync<School>(actor.TenantId, command.SchoolId, "school_not_found", cancellationToken);
|
|
var item = await ResolveByIdOrLegacyAsync(learningPersistence.ExamDates, actor.TenantId, command.Id, command.LegacyId,
|
|
cancellationToken);
|
|
var isNew = item is null;
|
|
item ??= new ExamDate { Id = command.Id ?? Guid.NewGuid(), TenantId = actor.TenantId };
|
|
item.RegionId = command.RegionId;
|
|
item.SchoolId = command.SchoolId;
|
|
item.LegacyId = Normalize(command.LegacyId);
|
|
item.ExamName = command.ExamName.Trim();
|
|
item.ExamAt = command.ExamAt;
|
|
item.ExamType = Normalize(command.ExamType);
|
|
item.Description = Normalize(command.Description) ?? Normalize(command.Content);
|
|
item.SortOrder = command.Order ?? item.SortOrder;
|
|
item.IsActive = command.IsActive ?? item.IsActive;
|
|
item.Metadata = JsonObjectOrDefault(command.Metadata);
|
|
if (isNew) learningPersistence.ExamDates.Add(item);
|
|
|
|
await unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return item;
|
|
}
|
|
|
|
protected async Task<(Guid? EntryId, Guid? ContentNodeId)> ResolveVocabularyNavigationAsync(
|
|
Guid tenantId,
|
|
Guid? unitId,
|
|
Guid? entryId,
|
|
Guid? contentNodeId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!unitId.HasValue) return (entryId, contentNodeId);
|
|
|
|
var unit = await contentAssetPersistence.VocabularyUnits.AsNoTracking().SingleOrDefaultAsync(
|
|
item => item.TenantId == tenantId && item.Id == unitId.Value,
|
|
cancellationToken);
|
|
if (unit is null)
|
|
throw new ContentManagementException("Vocabulary unit was not found.", "vocabulary_unit_not_found");
|
|
|
|
return (entryId ?? unit.EntryId, contentNodeId ?? unit.ContentNodeId);
|
|
}
|
|
|
|
protected async Task<(Guid? EntryId, Guid? ContentNodeId)> ResolveHandbookSubjectNavigationAsync(
|
|
Guid tenantId,
|
|
Guid? subjectId,
|
|
Guid? entryId,
|
|
Guid? contentNodeId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!subjectId.HasValue) return (entryId, contentNodeId);
|
|
|
|
var subject = await contentAssetPersistence.HandbookSubjects.AsNoTracking().SingleOrDefaultAsync(
|
|
item => item.TenantId == tenantId && item.Id == subjectId.Value,
|
|
cancellationToken);
|
|
if (subject is null)
|
|
throw new ContentManagementException("Handbook subject was not found.", "handbook_subject_not_found");
|
|
|
|
return (entryId ?? subject.EntryId, contentNodeId ?? subject.ContentNodeId);
|
|
}
|
|
|
|
protected async Task<(Guid? EntryId, Guid? ContentNodeId)> ResolveHandbookChapterNavigationAsync(
|
|
Guid tenantId,
|
|
Guid? chapterId,
|
|
Guid? entryId,
|
|
Guid? contentNodeId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (!chapterId.HasValue) return (entryId, contentNodeId);
|
|
|
|
var chapter = await contentAssetPersistence.HandbookChapters.AsNoTracking().SingleOrDefaultAsync(
|
|
item => item.TenantId == tenantId && item.Id == chapterId.Value,
|
|
cancellationToken);
|
|
if (chapter is null)
|
|
throw new ContentManagementException("Handbook chapter was not found.", "handbook_chapter_not_found");
|
|
|
|
return (entryId ?? chapter.EntryId, contentNodeId ?? chapter.ContentNodeId);
|
|
}
|
|
}
|