1212import jakarta .servlet .http .HttpServletRequest ;
1313import lombok .RequiredArgsConstructor ;
1414import lombok .extern .slf4j .Slf4j ;
15- import org .springframework .beans . factory . annotation . Value ;
15+ import org .springframework .http . HttpStatus ;
1616import org .springframework .http .ResponseEntity ;
17- import jakarta .servlet .http .HttpServletRequest ;
1817import org .springframework .web .bind .annotation .*;
19- import org .springframework .web .client .RestTemplate ;
2018
2119import java .util .List ;
2220import java .util .Map ;
@@ -29,9 +27,6 @@ public class CourseController {
2927
3028 private final CourseService courseService ;
3129
32- @ Value ("${user.service.uri:http://user-service:8082}" )
33- private String userServiceUri ;
34-
3530 @ PostMapping
3631 public ResponseEntity <CourseResponse > createCourse (@ RequestBody CourseRequest request ) {
3732 log .info ("Creating new course: {}" , request .getTitle ());
@@ -139,6 +134,7 @@ public ResponseEntity<List<CourseResponse>> searchCourses(
139134 List <CourseResponse > responses = courseService .advancedSearch (instructor , level , language , skill , category , title , isPublished , isPublic );
140135 return ResponseEntity .ok (responses );
141136 }
137+
142138 @ GetMapping ("/search/instructor/{instructor}" )
143139 public ResponseEntity <List <CourseResponse >> getCoursesByInstructor (@ PathVariable String instructor ) {
144140 log .info ("Fetching courses by instructor: {}" , instructor );
@@ -182,32 +178,63 @@ public ResponseEntity<List<CourseResponse>> searchCoursesByTitle(@PathVariable S
182178 }
183179
184180 /**
185- * Generates a brand-new course via GenAI + RAG, then persists & returns it.
186- * Chosen as POST because we are **creating** a new server-side resource
187- */
181+ * Generates a brand-new course via GenAI + RAG, then persists & returns it.
182+ * Chosen as POST because we are **creating** a new server-side resource
183+ */
188184 @ PostMapping ("/generate/learning_path/{userId}" )
189185 public ResponseEntity <CourseRequest > generateCourseForUser (@ PathVariable String userId , @ RequestBody LearningPathRequest req , HttpServletRequest servletRequest ) {
190- log .info ("Generating course for user: {}" , userId );
191- CourseRequest generated = courseService .generateCourseFromGenAi (req , userId , servletRequest .getHeader ("Authorization" ));
192- return ResponseEntity .ok (generated );
186+ log .info ("Generating course for user: {}" , userId );
187+ CourseRequest generated = courseService .generateCourseFromGenAi (req , userId , servletRequest .getHeader ("Authorization" ));
188+ return ResponseEntity .ok (generated );
193189 }
194190
195-
196191 /**
197- * Confirms the generation of a course from a Learning Path request.
198- * This method is called after the course has been generated and the user has reviewed it.
199- * It retrieves the last generated course details, creates the course in the database, enrolls the user, and returns the course response.
200- *
201- * @param userId The ID of the user confirming the course generation.
202- * @return CourseResponse containing the confirmed course details.
203- */
192+ * Confirms the generation of a course from a Learning Path request.
193+ * This method is called after the course has been generated and the user has reviewed it.
194+ * It retrieves the last generated course details, creates the course in the database, enrolls the user, and returns the course response.
195+ *
196+ * @param userId The ID of the user confirming the course generation.
197+ * @return CourseResponse containing the confirmed course details.
198+ */
204199 @ PostMapping ("/generate/learning_path/{userId}/confirm" )
205200 public ResponseEntity <CourseResponse > confirmGeneratedCourse (@ PathVariable String userId ) {
206- log .info ("Confirming course generation for user: {}" , userId );
207- CourseResponse confirmed = courseService .confirmCourseGeneration (userId );
208- return ResponseEntity .ok (confirmed );
201+ log .info ("Confirming course generation for user: {}" , userId );
202+ CourseResponse confirmed = courseService .confirmCourseGeneration (userId );
203+ return ResponseEntity .ok (confirmed );
209204 }
210205
206+ /**
207+ * Generates a response to a given Prompt.
208+ * This is delegated to the GenAi Service
209+ *
210+ * @param prompt The prompt to generate a response for.
211+ * @return The generated response as a String.
212+ */
213+ @ PostMapping ("/generate/prompt" )
214+ public ResponseEntity <String > generateResponseFromPrompt (@ RequestBody String prompt ) {
215+ log .info ("Generating response to prompt: {}" , prompt );
216+ String generatedResponse = courseService .generateResponseFromGenAi (prompt );
217+ return ResponseEntity .ok (generatedResponse );
218+ }
211219
220+ /**
221+ * Embeds a url into the GenAI service for future retrieval.
222+ *
223+ * @param urlPayload The JSON object containing the URL to embed, e.g. {"url": "https://example.com"}
224+ * @return ResponseEntity with the status of the operation.
225+ */
226+ @ PostMapping ("/crawl/url" )
227+ public ResponseEntity <?> crawlWebUrl (@ RequestBody Map <String , String > urlPayload ) {
228+ String url = urlPayload .get ("url" );
229+ log .info ("Embedding URL into GenAI service: {}" , url );
230+
231+ EmbedResult result = courseService .crawlWebForCourseContent (url );
232+
233+ if (result .isSuccess ()) {
234+ return ResponseEntity .ok (result );
235+ } else {
236+ return ResponseEntity .status (HttpStatus .BAD_GATEWAY ).body (result );
237+ }
238+ }
212239
213240}
0 commit comments