Skip to content

Commit e4c8e35

Browse files
author
Bavithbabu
committed
Done with Delete comment refacatoring
1 parent a2eebc3 commit e4c8e35

11 files changed

Lines changed: 212 additions & 10 deletions

File tree

.codex

Whitespace-only changes.

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ APPLE_CLIENT_SECRET=your-apple-client-secret-jwt-here
2222

2323

2424
CONTENT_MODERATION_WEBHOOK_URL=https://inscriptions.cdacb.in/n8n/webhook/content-moderation
25-
# CONTENT_MODERATION_INSECURE_SSL=true
25+
CONTENT_MODERATION_INSECURE_SSL=true
2626

2727
CONTENT_MODERATION_SAFE_THRESHOLD=0.7
2828
CONTENT_MODERATION_CONNECT_TIMEOUT_MS=5000
2929
CONTENT_MODERATION_READ_TIMEOUT_MS=10000
30-
CONTENT_MODERATION_INSECURE_SSL=false
30+
# CONTENT_MODERATION_INSECURE_SSL=false

src/main/java/com/cadac/stone_inscription/admin/entity/ArchiveComment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public class ArchiveComment {
3434
@JsonSerialize(using = ToStringSerializer.class)
3535
private ObjectId id;
3636

37+
@Field("originalCommentId")
38+
@JsonProperty("originalCommentId")
39+
@JsonSerialize(using = ToStringSerializer.class)
40+
private ObjectId originalCommentId;
41+
3742
@Field("postId")
3843
@JsonProperty("postId")
3944
@JsonSerialize(using = ToStringSerializer.class)

src/main/java/com/cadac/stone_inscription/admin/entity/ArchivePost.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class ArchivePost {
3535
@JsonSerialize(using = ToStringSerializer.class)
3636
private ObjectId id;
3737

38+
@Field("originalPostId")
39+
@JsonProperty("originalPostId")
40+
@JsonSerialize(using = ToStringSerializer.class)
41+
private ObjectId originalPostId;
42+
3843
@Field("user_id")
3944
@JsonProperty("user_id")
4045
@JsonSerialize(using = ToStringSerializer.class)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.cadac.stone_inscription.admin.repository;
2+
3+
import org.bson.types.ObjectId;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
import com.cadac.stone_inscription.admin.entity.ArchiveComment;
8+
9+
@Repository
10+
public interface ArchiveCommentRepository extends MongoRepository<ArchiveComment, ObjectId> {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.cadac.stone_inscription.admin.repository;
2+
3+
import org.bson.types.ObjectId;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
import com.cadac.stone_inscription.admin.entity.ArchivePost;
8+
9+
@Repository
10+
public interface ArchivePostRepository extends MongoRepository<ArchivePost, ObjectId> {
11+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.cadac.stone_inscription.content.delete;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import com.cadac.stone_inscription.admin.entity.ArchiveComment;
6+
import com.cadac.stone_inscription.admin.entity.ArchivePost;
7+
import com.cadac.stone_inscription.entity.InscriptionPost;
8+
import com.cadac.stone_inscription.entity.PublicPostDescription;
9+
10+
@Component
11+
public class ArchiveContentMapper {
12+
13+
public ArchivePost toArchivePost(InscriptionPost post) {
14+
return ArchivePost.builder()
15+
.originalPostId(post.getId())
16+
.userId(post.getUserId())
17+
.createdAt(post.getCreatedAt())
18+
.updatedAt(post.getUpdatedAt())
19+
.images(post.getImages() != null ? ArchivePost.Images.builder()
20+
.thumbnailImage(post.getImages().getThumbnailImage())
21+
.image(post.getImages().getImage())
22+
.build() : null)
23+
.description(post.getDescription() != null ? ArchivePost.Description.builder()
24+
.title(post.getDescription().getTitle())
25+
.subject(post.getDescription().getSubject())
26+
.description(post.getDescription().getDescription())
27+
.scriptLanguage(post.getDescription().getScriptLanguage())
28+
.language(post.getDescription().getLanguage())
29+
.englishTranslation(post.getDescription().getEnglishTranslation())
30+
.moderation(post.getDescription().getModeration())
31+
.upvote(post.getDescription().getUpvote())
32+
.geolocation(post.getDescription().getGeolocation() != null ? ArchivePost.GeoLocation.builder()
33+
.lat(post.getDescription().getGeolocation().getLat())
34+
.lon(post.getDescription().getGeolocation().getLon())
35+
.state(post.getDescription().getGeolocation().getState())
36+
.city(post.getDescription().getGeolocation().getCity())
37+
.country(post.getDescription().getGeolocation().getCountry())
38+
.build() : null)
39+
.createdAt(post.getDescription().getCreatedAt())
40+
.updatedAt(post.getDescription().getUpdatedAt())
41+
.build() : null)
42+
.topic(post.getTopic())
43+
.script(post.getScript())
44+
.type(post.getType())
45+
.status(post.getStatus())
46+
.report(post.getReport())
47+
.build();
48+
}
49+
50+
public ArchiveComment toArchiveComment(PublicPostDescription comment) {
51+
return ArchiveComment.builder()
52+
.originalCommentId(comment.getId())
53+
.postId(comment.getPostId())
54+
.userId(comment.getUserId())
55+
.username(comment.getUsername())
56+
.userImageUrl(comment.getUserImageUrl())
57+
.description(comment.getDescription())
58+
.moderation(comment.getModeration())
59+
.upvote(comment.getUpvote())
60+
.createdAt(comment.getCreatedAt())
61+
.updatedAt(comment.getUpdatedAt())
62+
.status(comment.getStatus())
63+
.report(comment.getReport())
64+
.build();
65+
}
66+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.cadac.stone_inscription.content.delete;
2+
3+
import lombok.Builder;
4+
import lombok.Value;
5+
6+
@Value
7+
@Builder
8+
public class ContentDeleteResult {
9+
int archivedPosts;
10+
int archivedComments;
11+
int deletedImages;
12+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.cadac.stone_inscription.content.delete;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import org.bson.types.ObjectId;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.stereotype.Service;
9+
10+
import com.cadac.stone_inscription.admin.repository.ArchiveCommentRepository;
11+
import com.cadac.stone_inscription.admin.repository.ArchivePostRepository;
12+
import com.cadac.stone_inscription.entity.InscriptionPost;
13+
import com.cadac.stone_inscription.entity.PublicPostDescription;
14+
import com.cadac.stone_inscription.entity.User;
15+
import com.cadac.stone_inscription.exception.StoneInscriptionException;
16+
import com.cadac.stone_inscription.repository.ImagesDataRepo;
17+
import com.cadac.stone_inscription.repository.InscriptionPostRepo;
18+
import com.cadac.stone_inscription.repository.PublicPostDescriptionRepo;
19+
import com.cadac.stone_inscription.repository.UserRepository;
20+
21+
import lombok.RequiredArgsConstructor;
22+
23+
@Service
24+
@RequiredArgsConstructor
25+
public class ContentDeleteService {
26+
27+
private final InscriptionPostRepo inscriptionPostRepo;
28+
private final PublicPostDescriptionRepo publicPostDescriptionRepo;
29+
private final ArchivePostRepository archivePostRepository;
30+
private final ArchiveCommentRepository archiveCommentRepository;
31+
private final ArchiveContentMapper archiveContentMapper;
32+
private final ImagesDataRepo imagesDataRepo;
33+
private final UserRepository userRepository;
34+
35+
public ContentDeleteResult deletePost(ObjectId postId) {
36+
InscriptionPost post = inscriptionPostRepo.findById(postId)
37+
.orElseThrow(() -> new StoneInscriptionException("Unprocesable request", HttpStatus.BAD_REQUEST));
38+
39+
List<PublicPostDescription> comments = publicPostDescriptionRepo.findByPostId(postId);
40+
List<String> imageIds = getImageIds(post);
41+
42+
archivePostRepository.save(archiveContentMapper.toArchivePost(post));
43+
comments.stream()
44+
.map(archiveContentMapper::toArchiveComment)
45+
.forEach(archiveCommentRepository::save);
46+
47+
imageIds.forEach(imagesDataRepo::deleteById);
48+
publicPostDescriptionRepo.deleteAll(comments);
49+
inscriptionPostRepo.delete(post);
50+
decrementUserImagesUploaded(post.getUserId(), imageIds.size());
51+
52+
return ContentDeleteResult.builder()
53+
.archivedPosts(1)
54+
.archivedComments(comments.size())
55+
.deletedImages(imageIds.size())
56+
.build();
57+
}
58+
59+
public ContentDeleteResult deleteComment(ObjectId commentId) {
60+
PublicPostDescription comment = publicPostDescriptionRepo.findById(commentId)
61+
.orElseThrow(() -> new StoneInscriptionException("Unprocesable request", HttpStatus.BAD_REQUEST));
62+
63+
archiveCommentRepository.save(archiveContentMapper.toArchiveComment(comment));
64+
publicPostDescriptionRepo.delete(comment);
65+
66+
return ContentDeleteResult.builder()
67+
.archivedPosts(0)
68+
.archivedComments(1)
69+
.deletedImages(0)
70+
.build();
71+
}
72+
73+
private List<String> getImageIds(InscriptionPost post) {
74+
if (post.getImages() == null || post.getImages().getImage() == null) {
75+
return Collections.emptyList();
76+
}
77+
78+
return post.getImages().getImage();
79+
}
80+
81+
private void decrementUserImagesUploaded(ObjectId userId, int imageCount) {
82+
if (imageCount <= 0) {
83+
return;
84+
}
85+
86+
User user = userRepository.findById(userId)
87+
.orElseThrow(() -> new StoneInscriptionException("User not found", HttpStatus.NOT_FOUND));
88+
89+
int currentCount = user.getImagesUploaded() == null ? 0 : user.getImagesUploaded();
90+
user.setImagesUploaded(Math.max(0, currentCount - imageCount));
91+
userRepository.save(user);
92+
}
93+
}

src/main/java/com/cadac/stone_inscription/post/controller/PostController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public ResponseEntity<?> deleteImagesFromPost(HttpServletRequest request,
264264
// TODO: Remove these methods when testing is done to restore normal behavior.
265265
// ============================
266266

267-
// // @PostMapping("/test/updatePost/{email}")
267+
// @PostMapping("/test/updatePost/{email}")
268268
// public ResponseEntity<?> updatePostForTest(
269269
// @PathVariable String email,
270270
// @RequestPart(value = "post", required = false) InscriptionPostDto InscriptionPostDto,

0 commit comments

Comments
 (0)