Compare commits
3 Commits
product_re
...
day3
| Author | SHA1 | Date | |
|---|---|---|---|
| 73e0c0df14 | |||
| b1dfb6322e | |||
| f9caf17a18 |
30
build.gradle
30
build.gradle
@@ -1,7 +1,7 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'org.springframework.boot' version '2.3.0.RELEASE'
|
id 'org.springframework.boot' version '2.3.0.RELEASE'
|
||||||
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
|
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
|
||||||
id 'java'
|
id 'java'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'com.cubetiqs'
|
group = 'com.cubetiqs'
|
||||||
@@ -9,23 +9,23 @@ version = '0.0.1-SNAPSHOT'
|
|||||||
sourceCompatibility = '1.8'
|
sourceCompatibility = '1.8'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'io.springfox:springfox-swagger2:2.9.2'
|
implementation 'io.springfox:springfox-swagger2:2.9.2'
|
||||||
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
|
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
|
||||||
|
|
||||||
runtimeOnly 'org.postgresql:postgresql'
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
annotationProcessor 'org.projectlombok:lombok'
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
testImplementation('org.springframework.boot:spring-boot-starter-test') {
|
testImplementation('org.springframework.boot:spring-boot-starter-test') {
|
||||||
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
|
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,24 @@
|
|||||||
package com.cubetiqs.demo;
|
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.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DemoApplication {
|
public class DemoApplication implements CommandLineRunner {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
UserEntity userEntity = new UserEntity();
|
||||||
|
userEntity.setEmail("a@gm.com");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
47
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Persistable;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public class BaseEntity<ID extends Serializable> implements Serializable, Persistable<ID> {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private ID id;
|
||||||
|
|
||||||
|
@Column(name = "created_date")
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date createdDate;
|
||||||
|
|
||||||
|
public void setId(ID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNew() {
|
||||||
|
return id == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreatedDate() {
|
||||||
|
return createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedDate(Date createdDate) {
|
||||||
|
this.createdDate = createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
public void beforeSave() {
|
||||||
|
if (createdDate == null) {
|
||||||
|
createdDate = new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "comments")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CommentEntity extends BaseEntity<Long> {
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String contents;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "post_id")
|
||||||
|
private PostEntity post;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity user;
|
||||||
|
}
|
||||||
31
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
31
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "posts")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
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;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
|
||||||
|
private Collection<CommentEntity> comments;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package com.cubetiqs.demo.domain;
|
|
||||||
|
|
||||||
import lombok.*;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "products")
|
|
||||||
@Data
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class Product {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
private Long id;
|
|
||||||
private String name;
|
|
||||||
private BigDecimal price;
|
|
||||||
private Boolean deleted;
|
|
||||||
}
|
|
||||||
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserEntity extends BaseEntity<Long> {
|
||||||
|
@Column(name = "email", length = 100, unique = true, nullable = false)
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Column(length = 100)
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.cubetiqs.demo.repository;
|
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.Product;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package com.cubetiqs.demo.rest;
|
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.Product;
|
|
||||||
import com.cubetiqs.demo.repository.ProductRepository;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping(path = "/products")
|
|
||||||
public class ProductController {
|
|
||||||
private final ProductRepository productRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public ProductController(ProductRepository productRepository) {
|
|
||||||
this.productRepository = productRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(method = {RequestMethod.GET})
|
|
||||||
public List<Product> getAllProducts() {
|
|
||||||
return productRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public Product getOneProduct(@PathVariable Long id) {
|
|
||||||
return productRepository.findById(id).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public Product createProduct(@RequestBody Product item) {
|
|
||||||
return productRepository.save(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
33
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal file
33
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user