Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

257 implementation of json ld standard #361

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.group7.demo.dtos.PostRequest;
import com.group7.demo.dtos.PostResponse;
import com.group7.demo.dtos.jsonld.PostJsonLd;
import com.group7.demo.dtos.mapper.Mapper;
import com.group7.demo.models.Post;
import com.group7.demo.services.PostService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -17,6 +21,7 @@
@RequestMapping("/api/posts")
@AllArgsConstructor
public class PostController {
private final Mapper mapper;
private PostService postService;

@PostMapping
Expand Down Expand Up @@ -125,5 +130,14 @@ public ResponseEntity<Map<String, Object>> fetchPostsWithPagination(
return ResponseEntity.ok(response);
}

@GetMapping(value = "/{postId}", produces = "application/ld+json")
public ResponseEntity<PostJsonLd> getPostJsonLd(@PathVariable Long postId) {
Post post = postService.getPostById(postId);
PostJsonLd jsonLd = mapper.mapToPostJsonLd(post);
return ResponseEntity.ok()
.contentType(MediaType.valueOf("application/ld+json"))
.body(jsonLd);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.group7.demo.dtos.jsonld;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;

import java.util.Map;

@Data
@Builder
public class PostJsonLd {
@JsonProperty("@context")
private String context;

@JsonProperty("@type")
private String type;

private String identifier;
private String text;
private String datePublished;

@JsonProperty("author")
private Map<String, String> author;

private String image;

@JsonProperty("interactionStatistic")
private Map<String, Object>[] interactionStatistics;

private String[] keywords;

@JsonProperty("associatedProgram")
private Map<String, Object> trainingProgram;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.group7.demo.dtos.mapper;

import com.group7.demo.dtos.*;
import com.group7.demo.dtos.jsonld.PostJsonLd;
import com.group7.demo.models.*;
import com.group7.demo.models.enums.TrainingProgramWithTrackingStatus;
import com.group7.demo.services.AuthenticationService;
Expand All @@ -10,6 +11,7 @@

import java.time.LocalDate;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -222,6 +224,50 @@ public FeedbackResponse mapToFeedbackResponse(Feedback feedback) {
.build();
}

public PostJsonLd mapToPostJsonLd(Post post) {
// Create array of interaction statistics for both likes and bookmarks
Map<String, Object>[] interactionStats = new Map[2];

// Like statistics
interactionStats[0] = Map.of(
"@type", "InteractionCounter",
"interactionType", "https://schema.org/LikeAction",
"userInteractionCount", post.getLikedByUsers() != null ? post.getLikedByUsers().size() : 0
);

// Bookmark statistics
interactionStats[1] = Map.of(
"@type", "InteractionCounter",
"interactionType", "https://schema.org/BookmarkAction",
"userInteractionCount", post.getBookmarkedByUsers() != null ? post.getBookmarkedByUsers().size() : 0
);

return PostJsonLd.builder()
.context("https://schema.org")
.type("SocialMediaPosting")
.identifier(post.getId().toString())
.text(post.getContent())
.datePublished(post.getCreatedAt().toString())
.author(Map.of(
"@type", "Person",
"identifier", post.getUser().getId().toString(),
"name", post.getUser().getUsername()
))
.image(post.getImageUrl() != null ? post.getImageUrl() : "")
.interactionStatistics(interactionStats)
.keywords(post.getTags() != null ?
post.getTags().stream()
.map(Tag::getName)
.toArray(String[]::new)
: new String[0])
.trainingProgram(post.getTrainingProgram() != null ? Map.of(
"@type", "ExercisePlan",
"identifier", post.getTrainingProgram().getId().toString(),
"name", post.getTrainingProgram().getTitle(),
"description", post.getTrainingProgram().getDescription(),
"instructor", post.getTrainingProgram().getTrainer().getUsername()
) : null)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,9 @@ public Map<String, Object> getPostsByTagsWithPagination(Set<String> tagNames, in
return response;
}

public Post getPostById(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new EntityNotFoundException("Post not found with id: " + postId));
}

}