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
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/models/BlogPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class BlogPost {
private String title;
private String imageUrl;
@ManyToOne(cascade = CascadeType.ALL)
@JsonIgnore
private User user;

public BlogPost() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.example.demo.repository;

import com.example.demo.models.BlogPost;
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 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.example.demo.models.Recipe;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RecipeRepository extends CrudRepository<Recipe, Long> {

}
2 changes: 2 additions & 0 deletions src/main/java/com/example/demo/repository/TagRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.demo.models.Tag;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TagRepository extends CrudRepository<Tag, Long> {
}
9 changes: 9 additions & 0 deletions src/main/java/com/example/demo/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.example.demo.repository;

import com.example.demo.models.BlogPost;
import com.example.demo.models.User;
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 org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}
11 changes: 10 additions & 1 deletion src/main/java/com/example/demo/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.service;

import com.example.demo.models.User;
import com.example.demo.repository.BlogPostRepository;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -12,10 +13,16 @@
public class UserService {
@Autowired
private UserRepository repository;
@Autowired
private BlogPostRepository blogPostRepository;

public User create(User user){ return repository.save(user); }

public User read(Long id){ return repository.findById(id).get(); }
public User read(Long id){
User user = repository.findById(id).get();
user.setBlogPostList(blogPostRepository.findByUser(id));
return user;
}

public List<User> readAll(){
Iterable<User> userIterable = repository.findAll();
Expand All @@ -41,3 +48,5 @@ public User delete(Long id){
return delete(read(id));
}
}


Binary file modified target/classes/com/example/demo/models/BlogPost.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/UserRepository.class
Binary file not shown.
Binary file modified target/classes/com/example/demo/service/UserService.class
Binary file not shown.