completed user controller

This commit is contained in:
Sambo Chea 2020-06-15 19:38:02 +07:00
parent 232662adea
commit 017e4c1643
7 changed files with 66 additions and 15 deletions

View File

@ -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'

View File

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

View File

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

View File

@ -5,9 +5,11 @@ 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.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"})
@ -24,10 +26,45 @@ public class UserController {
return userRepository.findAll(pageable);
}
@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,12 @@
server:
port: 8080
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: 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

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