7 Commits
day7 ... master

Author SHA1 Message Date
0bc0217bac Remove configs 2020-06-24 10:35:02 +07:00
eb9ac2cd79 Merge branch 'master' of https://github.com/CUBETIQ/backend-demo-training 2020-06-24 10:32:56 +07:00
178ae865df Updated and remove files 2020-06-24 10:32:39 +07:00
Sambo Chea
889fdb2459 Update application-postgres.yml 2020-06-24 10:27:29 +07:00
Sambo Chea
d761ce30f1 Update application-mysql.yml 2020-06-24 10:27:12 +07:00
7e29dca769 Add thymleaf view and testing it 2020-06-23 20:45:36 +07:00
d79ad3eab8 Add query and native query 2020-06-22 20:04:51 +07:00
15 changed files with 164 additions and 6 deletions

3
.gitignore vendored
View File

@@ -5,6 +5,9 @@ build/
!**/src/main/** !**/src/main/**
!**/src/test/** !**/src/test/**
application-mysql.yml
application-postgres.yml
### STS ### ### STS ###
.apt_generated .apt_generated
.classpath .classpath

View File

@@ -13,6 +13,8 @@ repositories {
} }
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
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'

View File

@@ -0,0 +1,20 @@
package com.cubetiqs.demo.domain.view;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonPropertyOrder(value = {
"email",
"id"
})
public class UserView implements Serializable {
private Long id;
private String email;
}

View File

@@ -1,9 +1,23 @@
package com.cubetiqs.demo.repository; package com.cubetiqs.demo.repository;
import com.cubetiqs.demo.domain.UserEntity; import com.cubetiqs.demo.domain.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface UserRepository extends BaseRepository<UserEntity, Long> { public interface UserRepository extends BaseRepository<UserEntity, Long> {
Optional<UserEntity> findFirstByEmail(String email);
@Query(value = "select * from users u where u.email = ?1", nativeQuery = true)
Optional<UserEntity> fetchFirstByEmail(String email);
@Query(value = "select * from users u", nativeQuery = true)
Page<UserEntity> fetchAllUsers(Pageable pageable);
@Query(value = "select * from users u where lower(u.email) like ?1", nativeQuery = true)
Page<UserEntity> searchByEmail(String likeEmail, Pageable pageable);
} }

View File

@@ -1,9 +1,13 @@
package com.cubetiqs.demo.rest; package com.cubetiqs.demo.rest;
import com.cubetiqs.demo.domain.UserEntity; import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.domain.view.UserView;
import com.cubetiqs.demo.repository.UserRepository; import com.cubetiqs.demo.repository.UserRepository;
import com.cubetiqs.demo.service.UserNotFoundException;
import com.cubetiqs.demo.service.UserService; import com.cubetiqs.demo.service.UserService;
import com.cubetiqs.demo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -12,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
@RequestMapping(path = {"/users"}) @RequestMapping(path = {"/users"})
public class UserController { public class UserController {
@@ -29,6 +34,30 @@ public class UserController {
return userService.findAllActives(); return userService.findAllActives();
} }
@GetMapping("/searchByEmail")
public Page<UserView> searchByEmail(
Pageable pageable,
@RequestParam(value = "q", defaultValue = "") String q
) {
return userService.searchByEmailAsView(StringUtils.stringQuery(q), pageable);
}
@GetMapping("/allUsers")
public Page<UserEntity> fetchAllUsers(Pageable pageable) {
return userRepository.fetchAllUsers(Pageable.unpaged());
}
@GetMapping("/findByEmail/{email}")
public ResponseEntity<Object> getUserById(
@PathVariable String email
) {
try {
return ResponseEntity.ok(userService.findByEmail(email));
} catch (UserNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}
}
@GetMapping("/{id}") @GetMapping("/{id}")
public UserEntity getUserById( public UserEntity getUserById(
@PathVariable Long id @PathVariable Long id

View File

@@ -0,0 +1,7 @@
package com.cubetiqs.demo.service;
public class UserNotFoundException extends Exception {
public UserNotFoundException(String message) {
super(message);
}
}

View File

@@ -1,9 +1,14 @@
package com.cubetiqs.demo.service; package com.cubetiqs.demo.service;
import com.cubetiqs.demo.domain.UserEntity; import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.domain.view.UserView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public interface UserService extends BaseService<UserEntity, Long> { public interface UserService extends BaseService<UserEntity, Long> {
UserEntity findByEmail(String email) throws UserNotFoundException;
Page<UserEntity> searchByEmail(String email, Pageable pageable);
Page<UserView> searchByEmailAsView(String email, Pageable pageable);
} }

View File

@@ -1,6 +1,7 @@
package com.cubetiqs.demo.service; package com.cubetiqs.demo.service;
import com.cubetiqs.demo.domain.UserEntity; import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.domain.view.UserView;
import com.cubetiqs.demo.repository.BaseRepository; import com.cubetiqs.demo.repository.BaseRepository;
import com.cubetiqs.demo.repository.UserRepository; import com.cubetiqs.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -9,6 +10,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService { public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implements UserService {
@@ -33,4 +35,25 @@ public class UserServiceImpl extends BaseServiceImpl<UserEntity, Long> implement
public List<UserEntity> findAllActives() { public List<UserEntity> findAllActives() {
return userRepository.findAllActives(); return userRepository.findAllActives();
} }
@Override
public UserEntity findByEmail(String email) throws UserNotFoundException {
Optional<UserEntity> opt = userRepository.fetchFirstByEmail(email);
if (opt.isPresent()) {
return opt.get();
}
throw new UserNotFoundException("user does not exists");
}
@Override
public Page<UserEntity> searchByEmail(String email, Pageable pageable) {
return userRepository.searchByEmail(email, pageable);
}
@Override
public Page<UserView> searchByEmailAsView(String email, Pageable pageable) {
Page<UserEntity> users = userRepository.searchByEmail(email, pageable);
return users.map(s -> new UserView(s.getId(), s.getEmail()));
}
} }

View File

@@ -0,0 +1,20 @@
package com.cubetiqs.demo.ui;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping(value = {"/index", "", "/", "/index.php"})
public String index(Model model) {
model.addAttribute("myname", "Sambo");
return "index";
}
@GetMapping(value = {"/admin/users"})
public String user(Model model) {
model.addAttribute("myname", "Sambo");
return "admin/users/index";
}
}

View File

@@ -0,0 +1,8 @@
package com.cubetiqs.demo.util;
public final class StringUtils {
public static String stringQuery(String q) {
return "%" + q.toLowerCase() + "%";
}
}

View File

@@ -7,6 +7,6 @@ spring:
ddl-auto: update ddl-auto: update
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
username: cubetiq username: root
password: Root$ password: root
url: jdbc:mysql://192.168.0.150:3306/demo url: jdbc:mysql://192.168.0.150:3306/demo

View File

@@ -7,6 +7,6 @@ spring:
ddl-auto: update ddl-auto: update
datasource: datasource:
driver-class-name: org.postgresql.Driver driver-class-name: org.postgresql.Driver
username: cubetiq username: root
password: Root$ password: root
url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo url: jdbc:postgresql://${POSTGRES_HOST:192.168.0.150}:5432/demo

View File

@@ -0,0 +1 @@
console.log("hello")

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Users</h1>
</body>
</html>

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Index</title>
</head>
<body>
<h1 th:text="${myname}"></h1>
</body>
</html>