-
Notifications
You must be signed in to change notification settings - Fork 0
Added: Comment-Service, Logging, Mongo persistence API logic. #9
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
Merged
ManishKumarKeshri
merged 4 commits into
TechDiscussion:master
from
pshroff04:service/comments
Jan 24, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package TechVault.config; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import com.mongodb.ConnectionString; | ||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; | ||
|
||
@Configuration | ||
public class PersistenceConfig extends AbstractMongoClientConfiguration { | ||
|
||
@Value("${spring.data.mongodb.database}") | ||
private String dbName; | ||
|
||
@Value("${spring.data.mongodb.uri}") | ||
private String uri; | ||
|
||
@Override | ||
protected String getDatabaseName() { | ||
return dbName; | ||
} | ||
|
||
@Override | ||
public MongoClient mongoClient() { | ||
//"mongodb+srv://prateek:p@cluster0.kf3n4.mongodb.net/TechVault?retryWrites=true&w=majority" | ||
ConnectionString connectionString = new ConnectionString(uri); | ||
MongoClientSettings mongoClientSettings = MongoClientSettings.builder() | ||
.applyConnectionString(connectionString) | ||
.build(); | ||
|
||
return MongoClients.create(mongoClientSettings); | ||
} | ||
|
||
@Override | ||
public Collection getMappingBasePackages() { | ||
return Collections.singleton("TechVault"); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/TechVault/services/comments/CommentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package TechVault.services.comments; | ||
|
||
import java.util.List; | ||
|
||
import TechVault.services.comments.model.CommentResponse; | ||
|
||
public interface CommentService { | ||
|
||
public List<CommentResponse> getComments (String contentId); | ||
|
||
public void addComment(String contentId, String parentCommentId, String userName, String comment); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/TechVault/services/comments/CommentServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package TechVault.services.comments; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import TechVault.services.comments.model.Comment; | ||
import TechVault.services.comments.model.CommentResponse; | ||
import TechVault.services.comments.persistence.CommentDao; | ||
|
||
@Service | ||
public class CommentServiceImpl implements CommentService { | ||
|
||
@Autowired | ||
private CommentDao commentDao; | ||
|
||
@Override | ||
public List<CommentResponse> getComments(String contentId) { | ||
|
||
List<Comment> comments = commentDao.getCommentsByBlog(contentId); | ||
Map<String, CommentResponse> map = new HashMap<>(); | ||
List<CommentResponse> result = new ArrayList<>(); | ||
for (Comment comment : comments) { | ||
CommentResponse commentResponse = new CommentResponse(comment); | ||
if (Strings.isNullOrEmpty(comment.getParentCommentId())) { | ||
result.add(commentResponse); | ||
} | ||
map.put(comment.getCommentId(), commentResponse); | ||
} | ||
for (Comment comment : comments) { | ||
if (!Strings.isNullOrEmpty(comment.getParentCommentId())) { | ||
CommentResponse parent = map.get(comment.getParentCommentId()); | ||
parent.getChildComments().add(map.get(comment.getCommentId())); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void addComment(String contentId, String parentCommentId, String userName, String comment) { | ||
commentDao.addComment(contentId, parentCommentId, userName, comment); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/TechVault/services/comments/controller/CommentController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package TechVault.services.comments.controller; | ||
|
||
import java.util.List; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import TechVault.services.comments.CommentService; | ||
import TechVault.services.comments.model.CommentResponse; | ||
|
||
@Validated | ||
@RestController | ||
@RequestMapping("comment") | ||
public class CommentController { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CommentController.class); | ||
|
||
@Autowired | ||
private CommentService commentService; | ||
|
||
/** | ||
* To the the profile of the user corresponding to the user id. | ||
* @return A Response entity which will have all the user details. | ||
*/ | ||
@RequestMapping(method = RequestMethod.GET, value = "/{contentId}") | ||
public ResponseEntity<?> getComments(@PathVariable String contentId) { | ||
List<CommentResponse> comments = null; | ||
try { | ||
comments = commentService.getComments(contentId); | ||
} catch (Exception e) { | ||
LOGGER.error("Unable to get comments ", e); | ||
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return new ResponseEntity<>(comments, HttpStatus.OK); | ||
} | ||
|
||
/** | ||
* To the the profile of the user corresponding to the user id. | ||
* @return A Response entity which will have all the user details. | ||
*/ | ||
@RequestMapping(method = RequestMethod.POST, value = "/postComment", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<?> postComment(@RequestBody String commentJson) { | ||
|
||
JsonObject commentBody = JsonParser.parseString(commentJson).getAsJsonObject(); | ||
try { | ||
commentService.addComment(commentBody.get("contentId").getAsString(), | ||
commentBody.get("parentCommentId").getAsString(), | ||
commentBody.get("userName").getAsString(), | ||
commentBody.get("comment").getAsString()); | ||
} catch (Exception e) { | ||
LOGGER.error("Error while posting comments", e); | ||
return new ResponseEntity<>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return new ResponseEntity<>("SUCCESS", HttpStatus.OK); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/TechVault/services/comments/model/Comment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package TechVault.services.comments.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.index.Indexed; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.data.mongodb.core.mapping.Field; | ||
|
||
@Document(collection = "comments") | ||
@Builder | ||
@Getter | ||
@Setter | ||
public class Comment { | ||
@Id | ||
private String commentId; | ||
|
||
@Indexed | ||
@Field(name = "contentId") | ||
private String contentId; | ||
|
||
@Field(name = "parentCommentId") | ||
private String parentCommentId; | ||
|
||
@Field(name = "userName") | ||
private String userName; | ||
|
||
@Field(name = "postedTime") | ||
private String postedTime; | ||
|
||
@Field(name = "comment") | ||
private String comment; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/TechVault/services/comments/model/CommentResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package TechVault.services.comments.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class CommentResponse { | ||
|
||
public CommentResponse (Comment comment ) { | ||
this(comment.getCommentId(), comment.getContentId(), | ||
comment.getUserName(), comment.getPostedTime(), comment.getComment(), | ||
new ArrayList<>()); | ||
} | ||
|
||
private String commentId; | ||
private String contentId; | ||
private String userName; | ||
private String postedTime; | ||
private String comment; | ||
private List<CommentResponse> childComments; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/TechVault/services/comments/persistence/CommentDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package TechVault.services.comments.persistence; | ||
|
||
import java.util.List; | ||
|
||
import TechVault.services.comments.model.Comment; | ||
|
||
public interface CommentDao { | ||
|
||
public List<Comment> getCommentsByBlog(String blogUUID); | ||
|
||
public void addComment(String contentId, String parentCommentId, String userName, String comment); | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/TechVault/services/comments/persistence/CommentDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package TechVault.services.comments.persistence; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import TechVault.services.comments.model.Comment; | ||
|
||
|
||
@Component | ||
public class CommentDaoImpl implements CommentDao { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CommentDaoImpl.class); | ||
private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; | ||
|
||
@Autowired | ||
private CommentRepository commentRepository; | ||
|
||
@Override | ||
public List<Comment> getCommentsByBlog(String contentId) { | ||
return commentRepository.findByContentIdOrderByPostedTimeDesc(contentId); | ||
} | ||
|
||
@Override | ||
public void addComment(String contentId, String parentCommentId, String userName, String comment) { | ||
commentRepository.save(Comment.builder() | ||
.parentCommentId(parentCommentId) | ||
.userName(userName) | ||
.contentId(contentId) | ||
.comment(comment) | ||
.postedTime(LocalDateTime.now().format(formatter)) | ||
.build()); | ||
|
||
LOGGER.info("Successfully save comment for user: {} and contentId: {}", userName, contentId); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/TechVault/services/comments/persistence/CommentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package TechVault.services.comments.persistence; | ||
|
||
import TechVault.services.comments.model.Comment; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface CommentRepository extends MongoRepository<Comment, String> { | ||
public List<Comment> findByContentIdOrderByPostedTimeDesc(String contentId); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
#spring.application.name: TechVault | ||
server.port= 8080 | ||
server.port= 8080 | ||
spring.data.mongodb.uri=mongodb+srv://wenqi:p@cluster0.kf3n4.mongodb.net/TechVault?retryWrites=true&w=majority | ||
spring.data.mongodb.database=TechVault |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be immediately reflected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think No. But we will have to look into that logic.