Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ee0199b50 | |||
| 017e4c1643 | |||
| 232662adea |
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-swagger-ui:2.9.2'
|
||||
|
||||
runtimeOnly 'mysql:mysql-connector-java'
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.io.File;
|
||||
public class DemoApplication implements CommandLineRunner {
|
||||
|
||||
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)
|
||||
private ID id;
|
||||
|
||||
@Column(name = "created_date")
|
||||
@Column(name = "created_date", updatable = false)
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date createdDate;
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
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
|
||||
|
||||
@@ -2,26 +2,39 @@ 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) {
|
||||
public UserController(UserRepository userRepository, UserService userService) {
|
||||
this.userRepository = userRepository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Page<UserEntity> getAllUsers(Pageable pageable) {
|
||||
return userRepository.findAll(pageable);
|
||||
public List<UserEntity> getAllUsers(Pageable pageable) {
|
||||
return userService.findAllUsers();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UserEntity getUserById(
|
||||
@PathVariable Long id
|
||||
) {
|
||||
return userRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@@ -30,4 +43,32 @@ public class UserController {
|
||||
) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal file
11
src/main/java/com/cubetiqs/demo/service/UserService.java
Normal 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();
|
||||
}
|
||||
23
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal file
23
src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java
Normal 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();
|
||||
}
|
||||
}
|
||||
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:
|
||||
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
|
||||
Reference in New Issue
Block a user