Compare commits

...

7 Commits
day7 ... master

15 changed files with 164 additions and 6 deletions

3
.gitignore vendored
View File

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

View File

@ -13,6 +13,8 @@ repositories {
}
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-web'
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;
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 java.util.Optional;
@Repository
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;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.domain.view.UserView;
import com.cubetiqs.demo.repository.UserRepository;
import com.cubetiqs.demo.service.UserNotFoundException;
import com.cubetiqs.demo.service.UserService;
import com.cubetiqs.demo.util.StringUtils;
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;
@ -12,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping(path = {"/users"})
public class UserController {
@ -29,6 +34,30 @@ public class UserController {
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}")
public UserEntity getUserById(
@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;
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;
@Service
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;
import com.cubetiqs.demo.domain.UserEntity;
import com.cubetiqs.demo.domain.view.UserView;
import com.cubetiqs.demo.repository.BaseRepository;
import com.cubetiqs.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -9,6 +10,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
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() {
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
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: cubetiq
password: Root$
username: root
password: root
url: jdbc:mysql://192.168.0.150:3306/demo

View File

@ -7,6 +7,6 @@ spring:
ddl-auto: update
datasource:
driver-class-name: org.postgresql.Driver
username: cubetiq
password: Root$
username: root
password: root
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>