Skip to content
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
103 changes: 58 additions & 45 deletions src/main/java/se/kth/sda6/skeleton/comments/Comment.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
//package se.kth.sda6.skeleton.comments;
//
//
//import se.kth.sda6.skeleton.posts.Post;
//
//import javax.persistence.*;
//
///**
// * Represents a comment made by a user on a post.
// */
//@Entity
//public class Comment {
// @Id
// @GeneratedValue(strategy = GenerationType.AUTO)
// private Long id;
//
// private String body;
//
// @ManyToOne
// private Post post;
//
// public Comment() {
// }
//
// public Comment(String body, Post post) {
// this.body = body;
// this.post = post;
// }
//
// public String getBody() {
// return body;
// }
//
// public void setBody(String body) {
// this.body = body;
// }
//
// public Post getPost() {
// return post;
// }
//
// public void setPost(Post post) {
// this.post = post;
// }
//}
package se.kth.sda6.skeleton.comments;


import se.kth.sda6.skeleton.posts.Post;

import javax.persistence.*;

/**
* Represents a comment made by a user on a post.
*/
@Entity
@Table(name = "comment")
public class Comment {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "body")
private String body;

@ManyToOne()
private Post post;

public Comment() {

}

public Comment(Long id, String body, Post post) {
this.id = id;
this.body = body;
this.post = post;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public Post getPost() {
return post;
}

public void setPost(Post post) {
this.post = post;
}
}
107 changes: 62 additions & 45 deletions src/main/java/se/kth/sda6/skeleton/comments/CommentController.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
//package se.kth.sda6.skeleton.comments;
//
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.server.ResponseStatusException;
//import se.kth.sda6.skeleton.posts.Post;
//import se.kth.sda6.skeleton.posts.PostService;
//
//import javax.persistence.EntityNotFoundException;
//import java.util.List;
//
//@RestController
//public class CommentController {
//
// private CommentService commentService;
// private PostService postService;
//
// public CommentController(CommentService commentService, PostService postService) {
// this.commentService = commentService;
// this.postService = postService;
// }
//
// @GetMapping("/comments")
// public ResponseEntity<?> getAllCommentsOnPost(@RequestParam Long postId) {
// Post post = postService.getByID(postId)
// .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find post with id " + postId.toString()));
// List<Comment> comments = commentService.getAllByPost(post);
// return new ResponseEntity<>(comments, HttpStatus.OK);
// }
//
// @DeleteMapping("/comments/{id}")
// public ResponseEntity<?> deleteComment(@PathVariable("id") Long id) {
// commentService.deleteById(id);
// return new ResponseEntity<>(HttpStatus.NO_CONTENT);
// }
//
// @PostMapping("posts/{id}/comments")
// public ResponseEntity<?> postComment(@RequestBody Comment comment, @PathVariable("id") Long postId) {
// Post post = postService.getByID(postId)
// .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find post with id " + postId.toString()));
// Comment savedComment = commentService.save(comment, post);
// return new ResponseEntity<Comment>(savedComment, HttpStatus.CREATED);
// }
//}
package se.kth.sda6.skeleton.comments;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import se.kth.sda6.skeleton.posts.Post;
import se.kth.sda6.skeleton.posts.PostService;

import java.util.List;

@RestController
public class CommentController {

private CommentService commentService;
private PostService postService;

public CommentController(CommentService commentService, PostService postService) {
this.commentService = commentService;
this.postService = postService;
}

@GetMapping("/comments")
public ResponseEntity<?> getAll() {
List<Comment> comments = commentService.getAll();
return new ResponseEntity<>(comments, HttpStatus.OK);
}

@GetMapping(value = "/comments", params = "postId")
public ResponseEntity<?> getAllOnPost(@RequestParam Long postId) {
Post post = postService.getByID(postId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
List<Comment> comments = commentService.getAllByPost(post);
return new ResponseEntity<>(comments, HttpStatus.OK);
}

@GetMapping("/comments/{id}")
public ResponseEntity<?> getById(@PathVariable Long id) {
Comment comment = commentService.getByID(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
return new ResponseEntity<>(comment, HttpStatus.OK);
}

@PostMapping("comments")
public ResponseEntity<?> create(@RequestBody Comment comment) {
Comment newComment = commentService.create(comment);
return new ResponseEntity<Comment>(newComment, HttpStatus.CREATED);
}

@PutMapping("comments")
public ResponseEntity<?> update(@RequestBody Comment comment) {
commentService.update(comment);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@DeleteMapping("/comments/{id}")
public ResponseEntity<?> delete(@PathVariable("id") Long id) {
commentService.delete(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

}
24 changes: 12 additions & 12 deletions src/main/java/se/kth/sda6/skeleton/comments/CommentRepository.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//package se.kth.sda6.skeleton.comments;
//
//import org.springframework.data.repository.CrudRepository;
//import se.kth.sda6.skeleton.posts.Post;
//
//import java.util.List;
//
//
//public interface CommentRepository extends CrudRepository<Comment, Long> {
//
// List<Comment> findAllByPost(Post post);
//}
package se.kth.sda6.skeleton.comments;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import se.kth.sda6.skeleton.posts.Post;

import java.util.List;


public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findAllByPost(Post post);
}
83 changes: 45 additions & 38 deletions src/main/java/se/kth/sda6/skeleton/comments/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
//package se.kth.sda6.skeleton.comments;
//
//import org.springframework.stereotype.Service;
//import se.kth.sda6.skeleton.posts.Post;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Optional;
//
//@Service
//public class CommentService {
//
// private CommentRepository commentRepository;
//
// // Implicitly autowired
// public CommentService(CommentRepository commentRepository) {
// this.commentRepository = commentRepository;
// }
//
// public List<Comment> getAllByPost(Post post) {
// List<Comment> list = new ArrayList<>();
// commentRepository.findAllByPost(post).forEach(list::add);
// return list;
// }
//
// public Optional<Comment> getByID(Long id) {
// return commentRepository.findById(id);
// }
//
// public Comment save(Comment comment, Post post) {
// post.addComment(comment);
// return commentRepository.save(comment);
// }
//
// public void deleteById(Long id) {
// commentRepository.deleteById(id);
// }
//}
package se.kth.sda6.skeleton.comments;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import se.kth.sda6.skeleton.posts.Post;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class CommentService {

private CommentRepository commentRepository;

@Autowired
public CommentService(CommentRepository commentRepository) {
this.commentRepository = commentRepository;
}

public List<Comment> getAll() {
return commentRepository.findAll();
}

public List<Comment> getAllByPost(Post post) {
return commentRepository.findAllByPost(post);
}

public Optional<Comment> getByID(Long id) {
return commentRepository.findById(id);
}

public Comment create(Comment comment) {
return commentRepository.save(comment);
}

public void delete(Long id) {
commentRepository.deleteById(id);
}

public void update(Comment comment) {
commentRepository.save(comment);
}
}
44 changes: 24 additions & 20 deletions src/main/java/se/kth/sda6/skeleton/posts/Post.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package se.kth.sda6.skeleton.posts;

import se.kth.sda6.skeleton.comments.Comment;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

// @TODO add Hibernate annotations to define which table and columns should be used to save the Post Object.
@Entity
@Table(name = "post")
public class Post {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY) // The DB will handle the process of generating the id.
private Long id;

@NotEmpty
@Column(name = "body")
private String body;

// @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
// private List<Comment> comments = new ArrayList<>();
public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public Post() {
}

public Post(String body) {
public Post(Long id, String body
) {
this.id = id;
this.body = body;

}

public Long getId() {
Expand All @@ -24,21 +45,4 @@ public void setId(Long id) {
this.id = id;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

// public void addComment(Comment comment) {
// comments.add( comment );
// comment.setPost( this );
// }
//
// public void removeComment(Comment comment) {
// comments.remove( comment );
// comment.setPost( null );
// }
}
Loading