From d79ad3eab86c49b26785c1c999b8a7dca3eb2d18 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Mon, 22 Jun 2020 20:04:51 +0700 Subject: [PATCH] Add query and native query --- .../cubetiqs/demo/domain/view/UserView.java | 20 +++++++++++++ .../demo/repository/UserRepository.java | 17 ++++++++++- .../cubetiqs/demo/rest/UserController.java | 29 +++++++++++++++++++ .../demo/service/UserNotFoundException.java | 7 +++++ .../cubetiqs/demo/service/UserService.java | 7 ++++- .../demo/service/UserServiceImpl.java | 23 +++++++++++++++ .../com/cubetiqs/demo/util/StringUtils.java | 8 +++++ 7 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/cubetiqs/demo/domain/view/UserView.java create mode 100644 src/main/java/com/cubetiqs/demo/service/UserNotFoundException.java create mode 100644 src/main/java/com/cubetiqs/demo/util/StringUtils.java diff --git a/src/main/java/com/cubetiqs/demo/domain/view/UserView.java b/src/main/java/com/cubetiqs/demo/domain/view/UserView.java new file mode 100644 index 0000000..9dce50f --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/domain/view/UserView.java @@ -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; +} diff --git a/src/main/java/com/cubetiqs/demo/repository/UserRepository.java b/src/main/java/com/cubetiqs/demo/repository/UserRepository.java index e44b8c8..5b22941 100644 --- a/src/main/java/com/cubetiqs/demo/repository/UserRepository.java +++ b/src/main/java/com/cubetiqs/demo/repository/UserRepository.java @@ -1,9 +1,24 @@ 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; + +@SuppressWarnings("ALL") @Repository public interface UserRepository extends BaseRepository { - + Optional findFirstByEmail(String email); + + @Query(value = "select * from users u where u.email = ?1", nativeQuery = true) + Optional fetchFirstByEmail(String email); + + @Query(value = "select * from users u", nativeQuery = true) + Page fetchAllUsers(Pageable pageable); + + @Query(value = "select * from users u where lower(u.email) like ?1", nativeQuery = true) + Page searchByEmail(String likeEmail, Pageable pageable); } diff --git a/src/main/java/com/cubetiqs/demo/rest/UserController.java b/src/main/java/com/cubetiqs/demo/rest/UserController.java index 052d944..d02291a 100644 --- a/src/main/java/com/cubetiqs/demo/rest/UserController.java +++ b/src/main/java/com/cubetiqs/demo/rest/UserController.java @@ -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 searchByEmail( + Pageable pageable, + @RequestParam(value = "q", defaultValue = "") String q + ) { + return userService.searchByEmailAsView(StringUtils.stringQuery(q), pageable); + } + + @GetMapping("/allUsers") + public Page fetchAllUsers(Pageable pageable) { + return userRepository.fetchAllUsers(Pageable.unpaged()); + } + + @GetMapping("/findByEmail/{email}") + public ResponseEntity 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 diff --git a/src/main/java/com/cubetiqs/demo/service/UserNotFoundException.java b/src/main/java/com/cubetiqs/demo/service/UserNotFoundException.java new file mode 100644 index 0000000..5ada905 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/service/UserNotFoundException.java @@ -0,0 +1,7 @@ +package com.cubetiqs.demo.service; + +public class UserNotFoundException extends Exception { + public UserNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/cubetiqs/demo/service/UserService.java b/src/main/java/com/cubetiqs/demo/service/UserService.java index a5b3220..38fd580 100644 --- a/src/main/java/com/cubetiqs/demo/service/UserService.java +++ b/src/main/java/com/cubetiqs/demo/service/UserService.java @@ -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 findByEmail(String email) throws UserNotFoundException; + Page searchByEmail(String email, Pageable pageable); + Page searchByEmailAsView(String email, Pageable pageable); } diff --git a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java index af723e0..2936f8d 100644 --- a/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java +++ b/src/main/java/com/cubetiqs/demo/service/UserServiceImpl.java @@ -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 implements UserService { @@ -33,4 +35,25 @@ public class UserServiceImpl extends BaseServiceImpl implement public List findAllActives() { return userRepository.findAllActives(); } + + @Override + public UserEntity findByEmail(String email) throws UserNotFoundException { + Optional opt = userRepository.fetchFirstByEmail(email); + if (opt.isPresent()) { + return opt.get(); + } + + throw new UserNotFoundException("user does not exists"); + } + + @Override + public Page searchByEmail(String email, Pageable pageable) { + return userRepository.searchByEmail(email, pageable); + } + + @Override + public Page searchByEmailAsView(String email, Pageable pageable) { + Page users = userRepository.searchByEmail(email, pageable); + return users.map(s -> new UserView(s.getId(), s.getEmail())); + } } diff --git a/src/main/java/com/cubetiqs/demo/util/StringUtils.java b/src/main/java/com/cubetiqs/demo/util/StringUtils.java new file mode 100644 index 0000000..2f821f2 --- /dev/null +++ b/src/main/java/com/cubetiqs/demo/util/StringUtils.java @@ -0,0 +1,8 @@ +package com.cubetiqs.demo.util; + +public final class StringUtils { + public static String stringQuery(String q) { + return "%" + q.toLowerCase() + "%"; + } +} +