package io.github.gernd.sqaas.endpoints;

import io.github.gernd.sqaas.endpoints.dto.QuoteDto;
import io.github.gernd.sqaas.model.QuoteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class QuotesEndpoint {

    @Autowired
    private QuoteRepository quoteRepository;

    @GetMapping(path = "/quotes")
    @Transactional
    public ResponseEntity<List<QuoteDto>> getAllQuotes() {
        List<QuoteDto> quotes = quoteRepository
                .streamQuotes()
                .map(q -> QuoteDto.builder().quote(q.getQuote()).author(q.getAuthor()).build())
                .collect(Collectors.toList());

        return ResponseEntity.ok(quotes);
    }
}

@@ -88,16 +88,64 @@
 oteDto;%0A
+import io.github.gernd.sqaas.model.QuoteEntity;%0A
 import i
@@ -188,16 +188,16 @@
 sitory;%0A
-
 import o
@@ -570,16 +570,63 @@
 l.List;%0A
+import java.util.concurrent.ThreadLocalRandom;%0A
 import j
@@ -1111,16 +1111,16 @@
 st());%0A%0A
-
         
@@ -1158,11 +1158,508 @@
 );%0A    %7D
+%0A%0A    @GetMapping(path = %22/quotes/random%22)%0A    @Transactional%0A    public ResponseEntity%3CQuoteDto%3E getRandomQuote() %7B%0A        List%3CQuoteDto%3E allQuotes = quoteRepository%0A                .streamQuotes()%0A                .map(q -%3E QuoteDto.builder().quote(q.getQuote()).author(q.getAuthor()).build())%0A                .collect(Collectors.toList());%0A%0A        QuoteDto randomQuote = allQuotes.get(ThreadLocalRandom.current().nextInt(allQuotes.size()));%0A        return ResponseEntity.ok(randomQuote);%0A    %7D
 %0A%7D%0A

Add endpoint for fetching a random quote