Files
soroush.asadi ab568c0663
Build backend images / build content-svc (push) Failing after 31s
Build backend images / build file-svc (push) Failing after 31s
Build backend images / build gateway (push) Failing after 31s
Build backend images / build identity-svc (push) Failing after 30s
Build backend images / build notification-svc (push) Failing after 30s
Build backend images / build render-svc (push) Failing after 31s
Build backend images / build studio-svc (push) Failing after 31s
feat(presets): admin preset stories (premade example videos) end-to-end
Epic A — admins author premade example videos per template; users pick one
on the template detail page to start a pre-filled project.

Backend (content-svc):
- PresetStory DTOs + PresetStoryService (admin CRUD + public published-only
  filter via role check + soft-delete) + PresetStoriesController (/v1/preset-stories)
- DI registration; gateway route /v1/preset-stories (optionalAuth, public read)

Frontend:
- ProjectPresetStories admin authoring UI (name/description/demo upload/published/
  sort + scene picker with order+duration + advanced scenes_spa); «ویدیوهای نمونه»
  button + modal in ProjectsAdmin
- TemplateDetailExamples renders real published stories (image/video preview,
  hover → "use this example" → creates a pre-bound project), falls back to
  placeholders when none; selected aspect's variant id keys the fetch
- public /api/preset-stories route; preset_story_id plumbed through
  createProjectFromTemplate + projects POST route; usePreset i18n (fa+en)

Verified: full CRUD via gateway (public hides unpublished); creating a project
with presetStoryId persists selected_preset_story_id on the saved project.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-11 05:24:14 +03:30

100 lines
4.2 KiB
C#

using FlatRender.ContentSvc.Domain.Entities;
using FlatRender.ContentSvc.Infrastructure.Data;
using FlatRender.ContentSvc.Models;
using Microsoft.EntityFrameworkCore;
namespace FlatRender.ContentSvc.Application.Services;
/// <summary>
/// CRUD for admin-authored preset stories (premade example videos) per template.
/// Public callers see published stories only; admins see drafts too.
/// </summary>
public class PresetStoryService(ContentDbContext db)
{
public async Task<List<PresetStorySummary>> GetByProjectAsync(Guid projectId, bool publishedOnly) =>
await db.PresetStories
.Where(s => s.ProjectId == projectId && (!publishedOnly || s.IsPublished))
.OrderBy(s => s.Sort).ThenByDescending(s => s.CreatedAt)
.Select(s => new PresetStorySummary(
s.Id, s.ProjectId, s.Name, s.Description, s.Demo, s.MusicId,
s.Sort, s.IsPublished, s.Scenes.Count, s.CreatedAt, s.UpdatedAt))
.ToListAsync();
public async Task<PresetStoryResponse?> GetAsync(Guid id, bool publishedOnly)
{
var s = await db.PresetStories
.Include(x => x.Scenes).ThenInclude(ps => ps.Scene)
.FirstOrDefaultAsync(x => x.Id == id);
if (s == null || (publishedOnly && !s.IsPublished)) return null;
return ToResponse(s);
}
public async Task<PresetStoryResponse> CreateAsync(SavePresetStoryRequest req)
{
var story = new PresetStory
{
ProjectId = req.ProjectId,
Name = req.Name,
Description = req.Description,
Demo = req.Demo,
MusicId = req.MusicId,
ScenesSpa = req.ScenesSpa,
Sort = req.Sort,
IsPublished = req.IsPublished,
Scenes = MapScenes(req.Scenes),
};
db.PresetStories.Add(story);
await db.SaveChangesAsync();
return await ReloadAsync(story.Id);
}
public async Task<PresetStoryResponse> UpdateAsync(Guid id, SavePresetStoryRequest req)
{
var story = await db.PresetStories.Include(s => s.Scenes).FirstOrDefaultAsync(s => s.Id == id)
?? throw new KeyNotFoundException($"Preset story {id} not found");
story.Name = req.Name;
story.Description = req.Description;
story.Demo = req.Demo;
story.MusicId = req.MusicId;
if (req.ScenesSpa != null) story.ScenesSpa = req.ScenesSpa;
story.Sort = req.Sort;
story.IsPublished = req.IsPublished;
story.UpdatedAt = DateTime.UtcNow;
// Replace the scene set wholesale (small, ordered list).
if (req.Scenes != null)
{
db.PresetScenes.RemoveRange(story.Scenes);
story.Scenes = MapScenes(req.Scenes);
}
await db.SaveChangesAsync();
return await ReloadAsync(story.Id);
}
public async Task DeleteAsync(Guid id)
{
var story = await db.PresetStories.FirstOrDefaultAsync(s => s.Id == id)
?? throw new KeyNotFoundException($"Preset story {id} not found");
story.DeletedAt = DateTime.UtcNow; // soft delete (global query filter hides it)
await db.SaveChangesAsync();
}
// ── helpers ───────────────────────────────────────────────────────────────
private static List<PresetScene> MapScenes(List<PresetSceneInput>? scenes) =>
(scenes ?? []).Select(s => new PresetScene
{
SceneId = s.SceneId,
Sort = s.Sort,
DefaultDurationSec = s.DefaultDurationSec,
}).ToList();
private async Task<PresetStoryResponse> ReloadAsync(Guid id) =>
ToResponse(await db.PresetStories.Include(x => x.Scenes).ThenInclude(ps => ps.Scene)
.FirstAsync(x => x.Id == id));
private static PresetStoryResponse ToResponse(PresetStory s) => new(
s.Id, s.ProjectId, s.Name, s.Description, s.Demo, s.MusicId, s.ScenesSpa,
s.Sort, s.IsPublished, s.CreatedAt, s.UpdatedAt,
s.Scenes.OrderBy(ps => ps.Sort).Select(ps => new PresetSceneResponse(
ps.Id, ps.SceneId, ps.Scene?.Key, ps.Scene?.Title, ps.Sort, ps.DefaultDurationSec)).ToList());
}