From b1ef0ec948da6b8d623055c226e6bc30df014200 Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 00:56:44 +0100 Subject: [PATCH 1/6] Add a post table as well as the process of database handling of generating the id. --- .../java/se/kth/sda6/skeleton/posts/Post.java | 67 +++++++++++++------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src/main/java/se/kth/sda6/skeleton/posts/Post.java b/src/main/java/se/kth/sda6/skeleton/posts/Post.java index a95fabe..33487ac 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/Post.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/Post.java @@ -1,19 +1,34 @@ package se.kth.sda6.skeleton.posts; +import javax.persistence.*; + // @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; + @Column(name = "bandName") + private String bandName; + @Column(name = "tourName") + private String tourName; + @Column(name = "location") + private String location; + @Column(name = "date") + private String date; - private String body; - -// @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) -// private List comments = new ArrayList<>(); public Post() { } - public Post(String body) { - this.body = body; + public Post(Long id, String bandName, String tourName, String location, String date) { + this.id = id; + this.bandName = bandName; + this.tourName = tourName; + this.location = location; + this.date = date; } public Long getId() { @@ -24,21 +39,35 @@ public void setId(Long id) { this.id = id; } - public String getBody() { - return body; + public String getBandName() { + return bandName; + } + + public void setBandName(String bandName) { + this.bandName = bandName; + } + + public String getTourName() { + return tourName; } - public void setBody(String body) { - this.body = body; + public void setTourName(String tourName) { + this.tourName = tourName; } -// public void addComment(Comment comment) { -// comments.add( comment ); -// comment.setPost( this ); -// } -// -// public void removeComment(Comment comment) { -// comments.remove( comment ); -// comment.setPost( null ); -// } + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } } From 8ac25b6364084e258724f31930ed11a66ac5de44 Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 01:01:29 +0100 Subject: [PATCH 2/6] Add implementation of get a list of all posts, get a specific post by it's id, create a new post, update post and delete a specific post methods. --- .../sda6/skeleton/posts/PostController.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java index 1affd13..842bfe3 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java @@ -1,10 +1,56 @@ package se.kth.sda6.skeleton.posts; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; /* @TODO AutoWire PostService and create the methods needed to implement the API. Don't forget to add necessary annotations. */ public class PostController { + + @Autowired + private PostService postService; + + @GetMapping + // Get a list of all posts + public List getAll (@RequestParam String sort) + { + return postService.getAll(sort); + } + + // Get a specific post by it's id + @GetMapping("/{id}") + public Post getById(@PathVariable Long id) { + return postService.getByID(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); + } + + // Create a new post + @PostMapping("") + public Post create(@RequestBody Post newPost) + { + return postService.create(newPost); + } + + // Update post + @PutMapping("") + public Post update(@RequestBody Post updatedPost) + { + return postService.update(updatedPost); + } + + // Delete a specific post + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) + { + postService.deleteById(id); + } } + + + From d49ea5dabdb012d8a8342620d17ddbb225ebd874 Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 01:03:18 +0100 Subject: [PATCH 3/6] Add implementation of get a list of all posts, get a specific post by it's id, create a new post, update post and delete a specific post methods. --- .../kth/sda6/skeleton/posts/PostService.java | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java index 6035b91..23b21bd 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java @@ -1,34 +1,51 @@ package se.kth.sda6.skeleton.posts; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Comparator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; /* @TODO Autowire the PostRepository and use it to implement all the service methods. */ +@Service public class PostService { - public List getAll() { - // @TODO get all posts and return them as List - return null; + @Autowired + private PostRepository repository; + + //get all posts and return them as List + public List getAll(String sort) { + return repository.findAll().stream() + .sorted(Comparator.comparing(sort.equals("name") ? Post::getBandName : Post::getDate)) + .collect(Collectors.toList()); } + // get a post by ID if it exists public Optional getByID(Long id) { - // @TODO get a post by ID if it exists - return null; + return repository.findById(id); + } - public Post save(Post post) { - // @TODO save the post to DB and return the saved post - return null; + + public Post create(Post newPost) { + return repository.save(newPost); } - public Optional update(Post post) { - // @TODO update the post if it exists in DB and return the updated post. - return null; + + + // update the post if it exists in DB and return the updated post. + public Post update(Post updatedPost) { + return repository.save(updatedPost); } + // delete the post by id public void deleteById(Long id) { - // @TODO delete the post by id - return; + repository.deleteById(id); } } + + + From e1a023edb5e33416709c2be6d646e995567e3e73 Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 01:04:30 +0100 Subject: [PATCH 4/6] Add extends JpaRepository to generate an implimentation that has all needed method . --- .../java/se/kth/sda6/skeleton/posts/PostRepository.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java index 13b8bca..36b9230 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java @@ -1,8 +1,13 @@ package se.kth.sda6.skeleton.posts; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + /* @TODO extend the appropriate JpaRepository to get common database operations for Post Add also the correct annotation to describe the Repository. */ -public interface PostRepository { +@Repository +public interface PostRepository extends JpaRepository { + } From d5f0d5512ebe876bc35d754290117b6acacc8dcb Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 12:22:28 +0100 Subject: [PATCH 5/6] added classes for concert/relate post and concert/implementation of CRUD --- .../kth/sda6/skeleton/Concerts/Concert.java | 40 +++++++++++++++++ .../skeleton/Concerts/ConcertController.java | 43 +++++++++++++++++++ .../skeleton/Concerts/ConcertRepositioy.java | 12 ++++++ .../skeleton/Concerts/ConcertService.java | 36 ++++++++++++++++ .../java/se/kth/sda6/skeleton/posts/Post.java | 16 ++++++- .../sda6/skeleton/posts/PostController.java | 12 +++++- .../sda6/skeleton/posts/PostRepository.java | 3 ++ .../kth/sda6/skeleton/posts/PostService.java | 7 ++- 8 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java create mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java create mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java create mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java b/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java new file mode 100644 index 0000000..6b89df3 --- /dev/null +++ b/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java @@ -0,0 +1,40 @@ +package se.kth.sda6.skeleton.Concerts; + +import javax.persistence.*; + +// @TODO add Hibernate annotations to define which table and columns should be used to save the Concert Object. +@Entity +@Table(name = "concert") +public class Concert { + + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) // The DB will handle the process of generating the id. + private Long id; + @Column(name = "concertName") + private String concertName; + + public Concert() { + } + + public Concert(Long id, String concertName) { + this.id = id; + this.concertName = concertName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getConcertName() { + return concertName; + } + + public void setConcertName(String concertName) { + this.concertName = concertName; + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java new file mode 100644 index 0000000..d7b4a1f --- /dev/null +++ b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java @@ -0,0 +1,43 @@ +package se.kth.sda6.skeleton.Concerts; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +public class ConcertController { + @Autowired + private ConcertService concertService; + + @GetMapping + public List getAll () + { + return concertService.getAll(); + } + + @GetMapping("/{id}") + public Concert getById(@PathVariable Long id) { + return concertService.getByID(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); + } + + @PostMapping("") + public Concert create(@RequestBody Concert newConcert) + { + return concertService.create(newConcert); + } + + @PutMapping("") + public Concert update(@RequestBody Concert updatedConcert) + { + return concertService.update(updatedConcert); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable Long id) + { + concertService.deleteById(id); + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java new file mode 100644 index 0000000..118a55b --- /dev/null +++ b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java @@ -0,0 +1,12 @@ +package se.kth.sda6.skeleton.Concerts; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +public interface ConcertRepositioy { + + @Repository + public interface ConcertRepository extends JpaRepository { + + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java new file mode 100644 index 0000000..cf7b27c --- /dev/null +++ b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java @@ -0,0 +1,36 @@ +package se.kth.sda6.skeleton.Concerts; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestBody; + + +import java.util.List; +import java.util.Optional; + +@Service +public class ConcertService { + @Autowired + private ConcertRepositioy repository; + + public List getAll() { + return repository.findAll(); + } + + public Optional getByID(Long id) { + return repository.findById(id); + + } + + public Concert create(@RequestBody Concert newConcert) { + return repository.save(newConcert); + } + + public Concert update(@RequestBody Concert updatedConcert) { + return repository.save(updatedConcert); + } + + public void deleteById(Long id) { + repository.deleteById(id); + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/posts/Post.java b/src/main/java/se/kth/sda6/skeleton/posts/Post.java index 33487ac..43bc34b 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/Post.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/Post.java @@ -1,5 +1,7 @@ package se.kth.sda6.skeleton.posts; +import se.kth.sda6.skeleton.Concerts.Concert; + import javax.persistence.*; // @TODO add Hibernate annotations to define which table and columns should be used to save the Post Object. @@ -19,16 +21,20 @@ public class Post { @Column(name = "date") private String date; + //to connect many post to one concert + @ManyToOne + private Concert concert; public Post() { } - public Post(Long id, String bandName, String tourName, String location, String date) { + public Post(Long id, String bandName, String tourName, String location, String date, Concert concert) { this.id = id; this.bandName = bandName; this.tourName = tourName; this.location = location; this.date = date; + this.concert = concert; } public Long getId() { @@ -70,4 +76,12 @@ public String getDate() { public void setDate(String date) { this.date = date; } + + public Concert getConcert() { + return concert; + } + + public void setConcert(Concert concert) { + this.concert = concert; + } } diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java index 842bfe3..0e1c44a 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java @@ -18,9 +18,17 @@ public class PostController { @GetMapping // Get a list of all posts - public List getAll (@RequestParam String sort) + public List getAll (@RequestParam (required = false) String sort, @RequestParam (required = false ) Long concertId) { - return postService.getAll(sort); + if (sort == null){ + sort = "name"; + } + if (concertId == null){ + return postService.getAll(sort); + }else { + return postService.getAllByConcertID(concertId); + } + } // Get a specific post by it's id diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java index 36b9230..b8e38c0 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java @@ -3,6 +3,8 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + /* @TODO extend the appropriate JpaRepository to get common database operations for Post Add also the correct annotation to describe the Repository. @@ -10,4 +12,5 @@ @Repository public interface PostRepository extends JpaRepository { + List findAllByConcertId(Long concertId); } diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java index 23b21bd..6ee3147 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java @@ -34,8 +34,6 @@ public Post create(Post newPost) { return repository.save(newPost); } - - // update the post if it exists in DB and return the updated post. public Post update(Post updatedPost) { return repository.save(updatedPost); @@ -45,6 +43,11 @@ public Post update(Post updatedPost) { public void deleteById(Long id) { repository.deleteById(id); } + + public List getAllByConcertID(Long concertId) { + return repository.findAllByConcertId(concertId); + + } } From ca7f744cc3074136c0d8adf6f1ed2657749feaf9 Mon Sep 17 00:00:00 2001 From: Asmaa-Abdullah Date: Wed, 20 Nov 2019 14:48:59 +0100 Subject: [PATCH 6/6] update post class,PostRepository class and PostController --- .../kth/sda6/skeleton/Concerts/Concert.java | 40 ------- .../skeleton/Concerts/ConcertController.java | 43 ------- .../skeleton/Concerts/ConcertRepositioy.java | 12 -- .../skeleton/Concerts/ConcertService.java | 36 ------ .../kth/sda6/skeleton/comments/Comment.java | 103 +++++++++-------- .../skeleton/comments/CommentController.java | 107 ++++++++++-------- .../skeleton/comments/CommentRepository.java | 24 ++-- .../skeleton/comments/CommentService.java | 83 +++++++------- .../java/se/kth/sda6/skeleton/posts/Post.java | 77 ++++--------- .../sda6/skeleton/posts/PostController.java | 17 +-- .../sda6/skeleton/posts/PostRepository.java | 2 - .../kth/sda6/skeleton/posts/PostService.java | 10 +- 12 files changed, 206 insertions(+), 348 deletions(-) delete mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java delete mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java delete mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java delete mode 100644 src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java b/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java deleted file mode 100644 index 6b89df3..0000000 --- a/src/main/java/se/kth/sda6/skeleton/Concerts/Concert.java +++ /dev/null @@ -1,40 +0,0 @@ -package se.kth.sda6.skeleton.Concerts; - -import javax.persistence.*; - -// @TODO add Hibernate annotations to define which table and columns should be used to save the Concert Object. -@Entity -@Table(name = "concert") -public class Concert { - - @Id - @Column(name = "id") - @GeneratedValue(strategy = GenerationType.IDENTITY) // The DB will handle the process of generating the id. - private Long id; - @Column(name = "concertName") - private String concertName; - - public Concert() { - } - - public Concert(Long id, String concertName) { - this.id = id; - this.concertName = concertName; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getConcertName() { - return concertName; - } - - public void setConcertName(String concertName) { - this.concertName = concertName; - } -} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java deleted file mode 100644 index d7b4a1f..0000000 --- a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertController.java +++ /dev/null @@ -1,43 +0,0 @@ -package se.kth.sda6.skeleton.Concerts; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.server.ResponseStatusException; - -import java.util.List; - -public class ConcertController { - @Autowired - private ConcertService concertService; - - @GetMapping - public List getAll () - { - return concertService.getAll(); - } - - @GetMapping("/{id}") - public Concert getById(@PathVariable Long id) { - return concertService.getByID(id) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); - } - - @PostMapping("") - public Concert create(@RequestBody Concert newConcert) - { - return concertService.create(newConcert); - } - - @PutMapping("") - public Concert update(@RequestBody Concert updatedConcert) - { - return concertService.update(updatedConcert); - } - - @DeleteMapping("/{id}") - public void delete(@PathVariable Long id) - { - concertService.deleteById(id); - } -} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java deleted file mode 100644 index 118a55b..0000000 --- a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertRepositioy.java +++ /dev/null @@ -1,12 +0,0 @@ -package se.kth.sda6.skeleton.Concerts; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -public interface ConcertRepositioy { - - @Repository - public interface ConcertRepository extends JpaRepository { - - } -} diff --git a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java b/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java deleted file mode 100644 index cf7b27c..0000000 --- a/src/main/java/se/kth/sda6/skeleton/Concerts/ConcertService.java +++ /dev/null @@ -1,36 +0,0 @@ -package se.kth.sda6.skeleton.Concerts; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.RequestBody; - - -import java.util.List; -import java.util.Optional; - -@Service -public class ConcertService { - @Autowired - private ConcertRepositioy repository; - - public List getAll() { - return repository.findAll(); - } - - public Optional getByID(Long id) { - return repository.findById(id); - - } - - public Concert create(@RequestBody Concert newConcert) { - return repository.save(newConcert); - } - - public Concert update(@RequestBody Concert updatedConcert) { - return repository.save(updatedConcert); - } - - public void deleteById(Long id) { - repository.deleteById(id); - } -} diff --git a/src/main/java/se/kth/sda6/skeleton/comments/Comment.java b/src/main/java/se/kth/sda6/skeleton/comments/Comment.java index 0da1bbb..b1ab7db 100644 --- a/src/main/java/se/kth/sda6/skeleton/comments/Comment.java +++ b/src/main/java/se/kth/sda6/skeleton/comments/Comment.java @@ -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; + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/comments/CommentController.java b/src/main/java/se/kth/sda6/skeleton/comments/CommentController.java index c39a885..a2df82a 100644 --- a/src/main/java/se/kth/sda6/skeleton/comments/CommentController.java +++ b/src/main/java/se/kth/sda6/skeleton/comments/CommentController.java @@ -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 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(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 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 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(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); + } + +} diff --git a/src/main/java/se/kth/sda6/skeleton/comments/CommentRepository.java b/src/main/java/se/kth/sda6/skeleton/comments/CommentRepository.java index 47d4ce5..973f56d 100644 --- a/src/main/java/se/kth/sda6/skeleton/comments/CommentRepository.java +++ b/src/main/java/se/kth/sda6/skeleton/comments/CommentRepository.java @@ -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 { -// -// List 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 { + List findAllByPost(Post post); +} diff --git a/src/main/java/se/kth/sda6/skeleton/comments/CommentService.java b/src/main/java/se/kth/sda6/skeleton/comments/CommentService.java index 6060f4f..0b97841 100644 --- a/src/main/java/se/kth/sda6/skeleton/comments/CommentService.java +++ b/src/main/java/se/kth/sda6/skeleton/comments/CommentService.java @@ -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 getAllByPost(Post post) { -// List list = new ArrayList<>(); -// commentRepository.findAllByPost(post).forEach(list::add); -// return list; -// } -// -// public Optional 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 getAll() { + return commentRepository.findAll(); + } + + public List getAllByPost(Post post) { + return commentRepository.findAllByPost(post); + } + + public Optional 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); + } +} diff --git a/src/main/java/se/kth/sda6/skeleton/posts/Post.java b/src/main/java/se/kth/sda6/skeleton/posts/Post.java index 43bc34b..f28245c 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/Post.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/Post.java @@ -1,8 +1,10 @@ package se.kth.sda6.skeleton.posts; -import se.kth.sda6.skeleton.Concerts.Concert; - +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 @@ -12,76 +14,35 @@ public class Post { @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) // The DB will handle the process of generating the id. private Long id; - @Column(name = "bandName") - private String bandName; - @Column(name = "tourName") - private String tourName; - @Column(name = "location") - private String location; - @Column(name = "date") - private String date; - //to connect many post to one concert - @ManyToOne - private Concert concert; + @NotEmpty + @Column(name = "body") + private String body; - public Post() { + public String getBody() { + return body; } - public Post(Long id, String bandName, String tourName, String location, String date, Concert concert) { - this.id = id; - this.bandName = bandName; - this.tourName = tourName; - this.location = location; - this.date = date; - this.concert = concert; + public void setBody(String body) { + this.body = body; } - public Long getId() { - return id; + public Post() { } - public void setId(Long id) { + public Post(Long id, String body + ) { this.id = id; - } - - public String getBandName() { - return bandName; - } + this.body = body; - public void setBandName(String bandName) { - this.bandName = bandName; } - public String getTourName() { - return tourName; - } - - public void setTourName(String tourName) { - this.tourName = tourName; - } - - public String getLocation() { - return location; - } - - public void setLocation(String location) { - this.location = location; - } - - public String getDate() { - return date; - } - - public void setDate(String date) { - this.date = date; + public Long getId() { + return id; } - public Concert getConcert() { - return concert; + public void setId(Long id) { + this.id = id; } - public void setConcert(Concert concert) { - this.concert = concert; - } } diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java index 0e1c44a..9f9aac8 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostController.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostController.java @@ -11,24 +11,18 @@ @TODO AutoWire PostService and create the methods needed to implement the API. Don't forget to add necessary annotations. */ +@RestController +@RequestMapping("/posts") public class PostController { @Autowired private PostService postService; - @GetMapping + @GetMapping("") // Get a list of all posts - public List getAll (@RequestParam (required = false) String sort, @RequestParam (required = false ) Long concertId) + public List getAll () { - if (sort == null){ - sort = "name"; - } - if (concertId == null){ - return postService.getAll(sort); - }else { - return postService.getAllByConcertID(concertId); - } - + return postService.getAll(); } // Get a specific post by it's id @@ -43,6 +37,7 @@ public Post getById(@PathVariable Long id) { public Post create(@RequestBody Post newPost) { return postService.create(newPost); + } // Update post diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java index b8e38c0..40bc551 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostRepository.java @@ -11,6 +11,4 @@ */ @Repository public interface PostRepository extends JpaRepository { - - List findAllByConcertId(Long concertId); } diff --git a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java index 6ee3147..3187e57 100644 --- a/src/main/java/se/kth/sda6/skeleton/posts/PostService.java +++ b/src/main/java/se/kth/sda6/skeleton/posts/PostService.java @@ -17,9 +17,8 @@ public class PostService { private PostRepository repository; //get all posts and return them as List - public List getAll(String sort) { + public List getAll() { return repository.findAll().stream() - .sorted(Comparator.comparing(sort.equals("name") ? Post::getBandName : Post::getDate)) .collect(Collectors.toList()); } @@ -29,7 +28,6 @@ public Optional getByID(Long id) { } - public Post create(Post newPost) { return repository.save(newPost); } @@ -44,10 +42,10 @@ public void deleteById(Long id) { repository.deleteById(id); } - public List getAllByConcertID(Long concertId) { - return repository.findAllByConcertId(concertId); + // public List getAllByConcertID(Long commentId) { + // return repository.findAllByCommentId(commentId); - } + // } }