Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 50d6479c8d | |||
| 9ee0199b50 | |||
| 017e4c1643 | |||
| 232662adea | |||
| 73e0c0df14 |
8
README.md
Normal file
8
README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
### Backend Classrom Demo
|
||||||
|
CUBETIQ Backend Training
|
||||||
|
|
||||||
|
### TODO
|
||||||
|
- [x] Entity
|
||||||
|
- [x] Repository
|
||||||
|
- [ ] Service
|
||||||
|
- [x] Controller
|
||||||
@@ -18,6 +18,7 @@ dependencies {
|
|||||||
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 'mysql:mysql-connector-java'
|
||||||
runtimeOnly 'org.postgresql:postgresql'
|
runtimeOnly 'org.postgresql:postgresql'
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
annotationProcessor 'org.projectlombok:lombok'
|
annotationProcessor 'org.projectlombok:lombok'
|
||||||
|
|||||||
5
homework/Home.md
Normal file
5
homework/Home.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
### Homework
|
||||||
|
|
||||||
|
- [ ] Create project all functions from start
|
||||||
|
- [ ] Add functions into BaseService (CRUD)
|
||||||
|
- [ ] Create BaseController
|
||||||
@@ -1,15 +1,18 @@
|
|||||||
package com.cubetiqs.demo;
|
package com.cubetiqs.demo;
|
||||||
|
|
||||||
import com.cubetiqs.demo.domain.UserEntity;
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.rest.UserController;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
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 implements CommandLineRunner {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class BaseEntity<ID extends Serializable> implements Serializable, Persis
|
|||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private ID id;
|
private ID id;
|
||||||
|
|
||||||
@Column(name = "created_date")
|
@Column(name = "created_date", updatable = false)
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date createdDate;
|
private Date createdDate;
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import lombok.NoArgsConstructor;
|
|||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "posts")
|
@Table(name = "posts")
|
||||||
@@ -18,8 +17,10 @@ import java.util.List;
|
|||||||
public class PostEntity extends BaseEntity<Long> {
|
public class PostEntity extends BaseEntity<Long> {
|
||||||
@Column
|
@Column
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@Column(columnDefinition = "TEXT")
|
@Column(columnDefinition = "TEXT")
|
||||||
private String contents;
|
private String contents;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
@JoinColumn(name = "user_id")
|
@JoinColumn(name = "user_id")
|
||||||
private UserEntity user;
|
private UserEntity user;
|
||||||
@@ -27,3 +28,4 @@ public class PostEntity extends BaseEntity<Long> {
|
|||||||
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
|
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
|
||||||
private Collection<CommentEntity> comments;
|
private Collection<CommentEntity> comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||||
|
}
|
||||||
@@ -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,9 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PostRepository extends BaseRepository<PostEntity, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.cubetiqs.demo.repository;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends BaseRepository<UserEntity, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
27
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
27
src/main/java/com/cubetiqs/demo/rest/PostController.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.cubetiqs.demo.rest;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import com.cubetiqs.demo.service.PostService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = {"/posts"})
|
||||||
|
public class PostController {
|
||||||
|
private final PostService postService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostController(PostService postService) {
|
||||||
|
this.postService = postService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<PostEntity> getAllPosts(Pageable pageable) {
|
||||||
|
return postService.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal file
73
src/main/java/com/cubetiqs/demo/rest/UserController.java
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
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.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.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
13
src/main/java/com/cubetiqs/demo/service/BaseService.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public interface BaseService<T, ID extends Serializable> {
|
||||||
|
BaseRepository<T, ID> getRepository();
|
||||||
|
List<T> findAll();
|
||||||
|
}
|
||||||
14
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
14
src/main/java/com/cubetiqs/demo/service/BaseServiceImpl.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@NoRepositoryBean
|
||||||
|
public abstract class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID> {
|
||||||
|
@Override
|
||||||
|
public List<T> findAll() {
|
||||||
|
return getRepository().findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
8
src/main/java/com/cubetiqs/demo/service/PostService.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface PostService extends BaseService<PostEntity, Long> {
|
||||||
|
}
|
||||||
22
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
22
src/main/java/com/cubetiqs/demo/service/PostServiceImpl.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.PostEntity;
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import com.cubetiqs.demo.repository.PostRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostServiceImpl extends BaseServiceImpl<PostEntity, Long> implements PostService {
|
||||||
|
private final PostRepository postRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public PostServiceImpl(PostRepository postRepository) {
|
||||||
|
this.postRepository = postRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseRepository<PostEntity, Long> getRepository() {
|
||||||
|
return postRepository;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
9
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public interface UserService extends BaseService<UserEntity, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
22
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
22
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.cubetiqs.demo.service;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import com.cubetiqs.demo.repository.BaseRepository;
|
||||||
|
import com.cubetiqs.demo.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService {
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserServiceImpl(UserRepository userRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseRepository<UserEntity, Long> getRepository() {
|
||||||
|
return userRepository;
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/main/resources/application-mysql.yml
Normal file
12
src/main/resources/application-mysql.yml
Normal 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
|
||||||
12
src/main/resources/application-postgres.yml
Normal file
12
src/main/resources/application-postgres.yml
Normal 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
|
||||||
@@ -1,12 +1,3 @@
|
|||||||
server:
|
|
||||||
port: 8080
|
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
jpa:
|
profiles:
|
||||||
hibernate:
|
active: postgres
|
||||||
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
|
|
||||||
Reference in New Issue
Block a user