Skip to content

Commit

Permalink
Acrescentando comentários aos posts
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmaugusto committed May 30, 2020
1 parent 6dfc80e commit bc82c69
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.miguelaugusto.workshopmongo.domain.Post;
import com.miguelaugusto.workshopmongo.domain.User;
import com.miguelaugusto.workshopmongo.dto.AuthorDTO;
import com.miguelaugusto.workshopmongo.dto.CommentDTO;
import com.miguelaugusto.workshopmongo.repository.PostRepository;
import com.miguelaugusto.workshopmongo.repository.UserRepository;

Expand Down Expand Up @@ -41,6 +42,13 @@ public void run(String... args) throws Exception {
Post post1 = new Post(null, sdf.parse("21/03/2018"), "Partiu viagem","Vou viajar para São Paulo. Abraços!", new AuthorDTO(maria));
Post post2 = new Post(null, sdf.parse("23/03/2018"), "Bom dia","Acordei feliz hoje!", new AuthorDTO(maria));

CommentDTO c1 = new CommentDTO("Boa viagem mano!", sdf.parse("21/03/2018"), new AuthorDTO(alex));
CommentDTO c2 = new CommentDTO("Aproveite", sdf.parse("22/03/2018"), new AuthorDTO(bob));
CommentDTO c3 = new CommentDTO("Tenha um ótimo dia!", sdf.parse("23/03/2018"), new AuthorDTO(alex));

post1.getComments().addAll(Arrays.asList(c1,c2));
post2.getComments().addAll(Arrays.asList(c3));

postRepository.saveAll(Arrays.asList(post1, post2));

maria.getPosts().addAll(Arrays.asList(post1, post2));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/miguelaugusto/workshopmongo/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.miguelaugusto.workshopmongo.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.data.mongodb.core.mapping.Document;

import com.miguelaugusto.workshopmongo.dto.AuthorDTO;
import com.miguelaugusto.workshopmongo.dto.CommentDTO;

@Document
public class Post implements Serializable{
Expand All @@ -18,6 +21,8 @@ public class Post implements Serializable{

private AuthorDTO author;

private List<CommentDTO> comments = new ArrayList<>();

public Post () {

}
Expand Down Expand Up @@ -71,6 +76,16 @@ public void setAuthor(AuthorDTO author) {
this.author = author;
}



public List<CommentDTO> getComments() {
return comments;
}

public void setComments(List<CommentDTO> comments) {
this.comments = comments;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/miguelaugusto/workshopmongo/dto/CommentDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.miguelaugusto.workshopmongo.dto;

import java.io.Serializable;
import java.util.Date;

public class CommentDTO implements Serializable{
private static final long serialVersionUID = 1L;
private String text;
private Date date;
private AuthorDTO author;

public CommentDTO() {

}

public CommentDTO(String text, Date date, AuthorDTO author) {
super();
this.text = text;
this.date = date;
this.author = author;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public AuthorDTO getAuthor() {
return author;
}

public void setAuthor(AuthorDTO author) {
this.author = author;
}
}

0 comments on commit bc82c69

Please sign in to comment.