Add Repository, Controller (User)

This commit is contained in:
Sambo Chea 2020-06-11 19:13:29 +07:00
parent b1dfb6322e
commit 73e0c0df14
6 changed files with 71 additions and 1 deletions

View File

@ -1,10 +1,13 @@
package com.cubetiqs.demo;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.rest.UserController;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.File;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

View File

@ -7,7 +7,6 @@ import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.Collection;
import java.util.List;
@Entity
@Table(name = "posts")
@ -18,8 +17,10 @@ import java.util.List;
public class PostEntity extends BaseEntity<Long> {
@Column
private String title;
@Column(columnDefinition = "TEXT")
private String contents;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "user_id")
private UserEntity user;
@ -27,3 +28,4 @@ public class PostEntity extends BaseEntity<Long> {
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
private Collection<CommentEntity> comments;
}

View File

@ -0,0 +1,10 @@
package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.domain.CommentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommentRepository extends JpaRepository<CommentEntity, Long> {
}

View File

@ -0,0 +1,10 @@
package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.domain.PostEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PostRepository extends JpaRepository<PostEntity, Long> {
}

View File

@ -0,0 +1,12 @@
package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.domain.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
}

View File

@ -0,0 +1,33 @@
package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = {"/users"})
public class UserController {
private final UserRepository userRepository;
@Autowired
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping
public Page<UserEntity> getAllUsers(Pageable pageable) {
return userRepository.findAll(pageable);
}
@PostMapping
public UserEntity createUser(
@RequestBody UserEntity user
) {
return userRepository.save(user);
}
}