6 Commits

Author SHA1 Message Date
9ee0199b50 Add user service 2020-06-16 19:19:26 +07:00
017e4c1643 completed user controller 2020-06-15 19:38:02 +07:00
232662adea Add Guide and Todo Tasks for Days 2020-06-11 21:41:47 +07:00
73e0c0df14 Add Repository, Controller (User) 2020-06-11 19:13:29 +07:00
b1dfb6322e Add domain for users, posts, comments 2020-06-10 19:38:38 +07:00
f9caf17a18 Removed classes 2020-06-10 17:58:31 +07:00
19 changed files with 327 additions and 90 deletions

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
### Backend Classrom Demo
CUBETIQ Backend Training
### TODO
- [x] Entity
- [x] Repository
- [ ] Service
- [x] Controller

View File

@@ -1,7 +1,7 @@
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.cubetiqs'
@@ -9,23 +9,24 @@ version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
useJUnitPlatform()
}

View File

@@ -1,13 +1,24 @@
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 {
public class DemoApplication implements CommandLineRunner {
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");
}
}

View 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", updatable = false)
@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();
}
}
}

View 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;
}

View 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;
}

View File

@@ -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;
}

View 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;
}

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

@@ -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> {
}

View File

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

View File

@@ -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);
}
}

View File

@@ -0,0 +1,74 @@
package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.repository.UserRepository;
import com.cubetiqs.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(path = {"/users"})
public class UserController {
private final UserRepository userRepository;
private final UserService userService;
@Autowired
public UserController(UserRepository userRepository, UserService userService) {
this.userRepository = userRepository;
this.userService = userService;
}
@GetMapping
public List<UserEntity> getAllUsers(Pageable pageable) {
return userService.findAllUsers();
}
@GetMapping("/{id}")
public UserEntity getUserById(
@PathVariable Long id
) {
return userRepository.findById(id).orElse(null);
}
@PostMapping
public UserEntity createUser(
@RequestBody UserEntity user
) {
return userRepository.save(user);
}
@PutMapping("/{id}")
public UserEntity updateUser(
@PathVariable Long id,
@RequestBody UserEntity user
) {
Optional<UserEntity> userEntityOptional = userRepository.findById(id);
if(userEntityOptional.isPresent()) {
// found
user.setId(id);
return userRepository.save(user);
} else {
return null;
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteUser(
@PathVariable Long id
) {
Optional<UserEntity> userEntityOptional = userRepository.findById(id);
if (userEntityOptional.isPresent()) {
userRepository.delete(userEntityOptional.get());
return ResponseEntity.status(HttpStatus.OK).body("user deleted");
} else {
return ResponseEntity.badRequest().body("not found");
}
}
}

View File

@@ -0,0 +1,11 @@
package com.cubetiqs.demo.service;
import com.cubetiqs.demo.domain.UserEntity;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface UserService {
List<UserEntity> findAllUsers();
}

View File

@@ -0,0 +1,23 @@
package com.cubetiqs.demo.service;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<UserEntity> findAllUsers() {
return userRepository.findAll();
}
}

View File

@@ -0,0 +1,12 @@
server:
port: 8081
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: cubetiq
password: Root$
url: jdbc:mysql://192.168.0.150:3306/demo

View File

@@ -0,0 +1,12 @@
server:
port: 8081
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
driver-class-name: org.postgresql.Driver
username: cubetiq
password: Root$
url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo

View File

@@ -1,12 +1,3 @@
server:
port: 8080
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
driver-class-name: org.postgresql.Driver
username: cubetiq
password: Root$
url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo
profiles:
active: postgres