Skip to content
Merged
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
14 changes: 13 additions & 1 deletion src/main/java/com/example/demo/models/BlogPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;

@Entity
public class BlogPost {
Expand All @@ -18,16 +19,19 @@ public class BlogPost {
@ManyToOne(cascade = CascadeType.ALL)
@JsonIgnore
private User user;
@ManyToMany(cascade = CascadeType.ALL)
private List<Tag> tags;

public BlogPost() {
}

public BlogPost(Long id, String title, String imageUrl, Recipe recipe, User user) {
public BlogPost(Long id, String title, String imageUrl, Recipe recipe, User user, List<Tag> tags) {
this.id = id;
this.title = title;
this.imageUrl = imageUrl;
this.recipe = recipe;
this.user = user;
this.tags = tags;
}

public Long getId() {
Expand Down Expand Up @@ -61,4 +65,12 @@ public void setImageUrl(String imageUrl) {
public User getUser() { return user; }

public void setUser(User user) { this.user = user; }

public List<Tag> getTags() {
return tags;
}

public void setTags(List<Tag> tags) {
this.tags = tags;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/demo/models/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.List;

@Entity
public class Recipe {
Expand Down Expand Up @@ -63,4 +64,5 @@ public void setInstructions(String instructions) {
public BlogPost getBlogPost() { return blogPost; }

public void setBlogPost(BlogPost blogPost) { this.blogPost = blogPost; }

}
15 changes: 10 additions & 5 deletions src/main/java/com/example/demo/models/Tag.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.example.demo.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.List;

@Entity
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
String name;
@ManyToMany(cascade = CascadeType.ALL)
List<BlogPost> blogPosts;

public Tag() {
}

public Tag(Long id, String name) {
public Tag(Long id, String name, List<BlogPost> blogPosts) {
this.id = id;
this.name = name;
this.blogPosts = blogPosts;
}

public Long getId() {
Expand All @@ -35,6 +36,10 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public List<BlogPost> getBlogPosts() { return blogPosts; }

public void setBlogPosts(List<BlogPost> blogPosts) { this.blogPosts = blogPosts; }
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.repository;

import com.example.demo.models.BlogPost;
import com.example.demo.models.Tag;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
Expand All @@ -13,4 +14,7 @@ public interface BlogPostRepository extends CrudRepository<BlogPost, Long> {

@Query(value = "SELECT * FROM blog_post b WHERE b.user_id = :userId", nativeQuery = true)
List<BlogPost> findByUser(@Param("userId") Long userId);

@Query(value = "SELECT * FROM blog_post WHERE tag_id = :tagId", nativeQuery = true)
List<BlogPost> findByTag(@Param("tagId") Long tagId);
}
9 changes: 9 additions & 0 deletions src/main/java/com/example/demo/repository/TagRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.example.demo.repository;

import com.example.demo.models.BlogPost;
import com.example.demo.models.Recipe;
import com.example.demo.models.Tag;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TagRepository extends CrudRepository<Tag, Long> {

@Query(value = "SELECT * FROM tag WHERE tag.blog_post_id = :postId", nativeQuery = true)
List<Tag> findByBlogPost(@Param("postId") Long blogPostId);
}
13 changes: 12 additions & 1 deletion src/main/java/com/example/demo/service/BlogPostService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.demo.service;

import com.example.demo.models.BlogPost;
import com.example.demo.models.User;
import com.example.demo.repository.BlogPostRepository;
import com.example.demo.repository.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -12,15 +14,24 @@
public class BlogPostService {
@Autowired
BlogPostRepository repository;
@Autowired
TagRepository tagRepository;

public BlogPost create(BlogPost blogPost){return repository.save(blogPost);}

public BlogPost read(Long id){ return repository.findById(id).get();}
public BlogPost read(Long id){
BlogPost blogPost = repository.findById(id).get();
blogPost.setTags(tagRepository.findByBlogPost(id));
return blogPost;
}

public List<BlogPost> readAll(){
Iterable<BlogPost> blogPostsIterable = repository.findAll();
List<BlogPost> result = new ArrayList<>();
blogPostsIterable.forEach(result::add);
for(BlogPost blogPost : result) {
blogPost.setTags(tagRepository.findByBlogPost(blogPost.getId()));
}
return result;
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/example/demo/service/TagService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.demo.service;

import com.example.demo.models.BlogPost;
import com.example.demo.models.Tag;
import com.example.demo.repository.BlogPostRepository;
import com.example.demo.repository.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -12,15 +14,24 @@
public class TagService {
@Autowired
private TagRepository repository;
@Autowired
private BlogPostRepository blogPostRepository;

public Tag create(Tag tag){ return repository.save(tag); }

public Tag read(Long id){ return repository.findById(id).get(); }
public Tag read(Long id){
Tag tag = repository.findById(id).get();
tag.setBlogPosts(blogPostRepository.findByTag(id));
return tag;
}

public List<Tag> readAll(){
Iterable<Tag> tagIterable = repository.findAll();
List<Tag> result = new ArrayList<>();
tagIterable.forEach(result::add);
for(Tag tag : result) {
tag.setBlogPosts(blogPostRepository.findByTag(tag.getId()));
}
return result;
}

Expand Down
Binary file modified target/classes/com/example/demo/models/BlogPost.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/models/Recipe.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/models/Tag.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/repository/BlogPostRepository.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/repository/TagRepository.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/BlogPostService.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/TagService.class
Binary file not shown.