Add query and native query

This commit is contained in:
Sambo Chea 2020-06-22 20:04:51 +07:00
parent f14bede40b
commit d79ad3eab8
7 changed files with 109 additions and 2 deletions

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,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<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,8 @@
package com.cubetiqs.demo.util;
public final class StringUtils {
public static String stringQuery(String q) {
return "%" + q.toLowerCase() + "%";
}
}