File size: 2,085 Bytes
8fb6238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using ClipForge.Contracts.Requests;
using ClipForge.Contracts.Responses;
using ClipForge.Core.Interfaces;
using ClipForge.Core.Interfaces.Services;

namespace ClipForge.Api.Endpoints;

public static class IdeaEndpoints
{
    public static void MapIdeaEndpoints(this WebApplication app)
    {
        var group = app.MapGroup("/api/ideas").WithTags("Ideas");

        group.MapPost("/generate", async (GenerateIdeaRequest request, IIdeaGenerationService service) =>
        {
            if (request.Count > 1)
            {
                var ideas = await service.GenerateBatchIdeasAsync(request.Niche, request.Count, request.Platform);
                return Results.Ok(ideas.Select(MapToResponse));
            }

            var idea = await service.GenerateIdeaAsync(request.Niche, request.Platform);
            return Results.Ok(MapToResponse(idea));
        });

        group.MapGet("/", async (IVideoIdeaRepository repository) =>
        {
            var ideas = await repository.GetAllAsync();
            return Results.Ok(ideas.Select(MapToResponse));
        });

        group.MapGet("/{id:guid}", async (Guid id, IVideoIdeaRepository repository) =>
        {
            var idea = await repository.GetByIdAsync(id);
            return idea is null ? Results.NotFound() : Results.Ok(MapToResponse(idea));
        });

        group.MapGet("/status/{status}", async (string status, IVideoIdeaRepository repository) =>
        {
            var ideas = await repository.GetByStatusAsync(status);
            return Results.Ok(ideas.Select(MapToResponse));
        });

        group.MapGet("/niche/{niche}", async (string niche, IVideoIdeaRepository repository) =>
        {
            var ideas = await repository.GetByNicheAsync(niche);
            return Results.Ok(ideas.Select(MapToResponse));
        });
    }

    private static VideoIdeaResponse MapToResponse(ClipForge.Core.Entities.VideoIdea idea) => new(
        idea.Id, idea.Topic, idea.Title, idea.Hook, idea.TargetAudience,
        idea.Platform, idea.Status, idea.Niche, idea.Tags, idea.CreatedAt);
}