package com.openisle.controller; import com.openisle.model.Post; import com.openisle.model.PostStatus; import com.openisle.repository.PostRepository; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Controller for dynamic sitemap generation. */ @RestController @RequiredArgsConstructor @RequestMapping("/api") public class SitemapController { private final PostRepository postRepository; @Value("${app.website-url}") private String websiteUrl; @GetMapping(value = "/sitemap.xml", produces = MediaType.APPLICATION_XML_VALUE) public ResponseEntity sitemap() { List posts = postRepository.findByStatus(PostStatus.PUBLISHED); StringBuilder body = new StringBuilder(); body.append("\n"); body.append("\n"); List staticRoutes = List.of( "/", "/about", "/activities", "/login", "/signup" ); for (String path : staticRoutes) { body.append(" ") .append(websiteUrl) .append(path) .append("\n"); } for (Post p : posts) { body.append(" \n") .append(" ") .append(websiteUrl) .append("/posts/") .append(p.getId()) .append("\n") .append(" ") .append(p.getCreatedAt().toLocalDate()) .append("\n") .append(" \n"); } body.append(""); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_XML) .body(body.toString()); } }