Changeset a6e33d1


Ignore:
Timestamp:
07/02/26 23:35:59 (3 days ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Parents:
3ae4bab
Message:

AI suggestions fixed

Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • ChapterX.API/Controllers/AISuggestionsController.cs

    r3ae4bab ra6e33d1  
    55using Microsoft.AspNetCore.Mvc;
    66using Microsoft.Extensions.Logging;
     7using System.Linq;
    78
    89namespace ChapterX.API.Controllers
     
    3738            var response = await _mediator.Send(new GetRequest(id));
    3839            return Ok(response);
     40        }
     41
     42        [HttpGet("chapter/{chapterId:int}")]
     43        [AllowAnonymous]
     44        public async Task<ActionResult> GetByChapter(int chapterId)
     45        {
     46            _logger.LogInformation("Fetching AI suggestions for ChapterId: {ChapterId}", chapterId);
     47            var response = await _mediator.Send(new GetByChapterRequest(chapterId));
     48            var result = response.AISuggestions.Select(s => new
     49            {
     50                id = s.Id,
     51                originalText = s.OriginalText,
     52                suggestedText = s.SuggestedText,
     53                accepted = s.Accepted,
     54                createdAt = s.CreatedAt,
     55                appliedAt = s.AppliedAt,
     56                storyId = s.StoryId,
     57                suggestionTypes = s.SuggestionTypes.Select(t => t.SuggestionTypeValue),
     58            });
     59            return Ok(result);
    3960        }
    4061
  • ChapterX.Infrastructure/Repositories/AISuggestionRepository.cs

    r3ae4bab ra6e33d1  
    2121        {
    2222            return await _dbSet
    23                 .Where(a => a.StoryId == chapterId)
     23                .Include(a => a.SuggestionTypes)
     24                .Where(a => a.NeedApprovals.Any(na => na.ChapterId == chapterId))
    2425                .ToListAsync(cancellationToken);
    2526        }
  • chapterx-frontend/src/components/writer/AISuggestionPanel.tsx

    r3ae4bab ra6e33d1  
    100100}
    101101
    102 export const AISuggestionPanel: React.FC<AISuggestionPanelProps> = ({ chapterId, storyId }) => {
     102export const AISuggestionPanel: React.FC<AISuggestionPanelProps> = ({ chapterId }) => {
    103103  const { aiSuggestions, acceptSuggestion, rejectSuggestion, fetchSuggestions } = useStoryStore()
    104104  const { addToast } = useUIStore()
     
    108108  useEffect(() => {
    109109    setLoading(true)
    110     fetchSuggestions().finally(() => setLoading(false))
     110    fetchSuggestions(chapterId).finally(() => setLoading(false))
    111111  }, [chapterId])
    112112
    113   // Filter by storyId if available (backend), else fall back to chapterId (mock)
    114   const chapterSuggestions = aiSuggestions.filter(s =>
    115     storyId ? (s as any).story_id === storyId : s.chapter_id === chapterId
    116   )
     113  // Backend already scopes results to this chapter via NEED_APPROVAL
     114  const chapterSuggestions = aiSuggestions
    117115  const pending = chapterSuggestions.filter(s => s.accepted === null)
    118116  const accepted = chapterSuggestions.filter(s => s.accepted === true)
  • chapterx-frontend/src/store/storyStore.ts

    r3ae4bab ra6e33d1  
    77  Collaboration,
    88  AISuggestion,
     9  SuggestionType,
    910  Genre,
    1011  ReadingList,
     
    105106
    106107  // AI Suggestion actions
    107   fetchSuggestions: () => Promise<void>
     108  fetchSuggestions: (chapterId: number) => Promise<void>
    108109  acceptSuggestion: (id: number) => Promise<void>
    109110  rejectSuggestion: (id: number) => Promise<void>
     
    434435  },
    435436
    436   fetchSuggestions: async () => {
    437     try {
    438       const res = await axios.get(`${API}/aisuggestions`)
    439       const data = res.data.aiSuggestions ?? res.data
     437  fetchSuggestions: async (chapterId) => {
     438    try {
     439      const res = await axios.get(`${API}/aisuggestions/chapter/${chapterId}`)
     440      const data: any[] = res.data ?? []
    440441      const mapped: AISuggestion[] = data.map((s: any) => ({
    441442        suggestion_id: s.id,
    442         chapter_id: s.storyId,
     443        chapter_id: chapterId,
    443444        story_id: s.storyId,
    444445        original_text: s.originalText,
    445446        suggested_text: s.suggestedText,
    446         suggestion_type: 'style' as const,
     447        suggestion_type: (s.suggestionTypes?.[0] ?? 'style') as SuggestionType,
    447448        accepted: s.accepted === true ? true : s.accepted === false ? false : null,
    448449        applied_at: s.appliedAt ?? undefined,
     
    471472        suggestedText: s.suggested_text,
    472473        accepted: true,
    473       })
     474      }, { headers: getAuthHeaders() })
    474475    } catch {
    475476      // keep optimistic update even if backend fails
     
    491492        suggestedText: s.suggested_text,
    492493        accepted: false,
    493       })
     494      }, { headers: getAuthHeaders() })
    494495    } catch {
    495496      // keep optimistic update even if backend fails
     
    502503    set(state => ({ aiSuggestions: [...state.aiSuggestions, { ...suggestion, suggestion_id: tempId }] }))
    503504    try {
    504       const res = await axios.post('${API}/aisuggestions', {
     505      const res = await axios.post(`${API}/aisuggestions`, {
    505506        originalText: suggestion.original_text,
    506507        suggestedText: suggestion.suggested_text,
    507508        storyId: suggestion.chapter_id,
    508       })
     509      }, { headers: getAuthHeaders() })
    509510      const newId = res.data.id ?? tempId
    510511      set(state => ({
Note: See TracChangeset for help on using the changeset viewer.