|
| 1 | +package com.github.commerce.repository.product; |
| 2 | + |
| 3 | +import com.github.commerce.web.dto.product.GetProductDto; |
| 4 | +import org.springframework.data.domain.Pageable; |
| 5 | +import org.springframework.stereotype.Repository; |
| 6 | + |
| 7 | +import javax.persistence.EntityManager; |
| 8 | +import javax.persistence.PersistenceContext; |
| 9 | +import javax.persistence.Query; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +@Repository |
| 13 | +public class ProductRepositoryCustom { |
| 14 | + |
| 15 | + @PersistenceContext |
| 16 | + private EntityManager entityManager; |
| 17 | + |
| 18 | + public List<GetProductDto> findByProductCategorySortByCreatedAt( |
| 19 | + String inputProductCategory, |
| 20 | + String inputAgeCategory, |
| 21 | + String inputGenderCategory, |
| 22 | + Pageable pageable) { |
| 23 | + |
| 24 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 25 | + "p.product_category, p.age_category, p.gender_category, " + |
| 26 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 27 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 28 | + "WHERE p.product_category = :inputProductCategory " + |
| 29 | + "AND (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 30 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 31 | + "AND p.is_deleted = false " + |
| 32 | + "ORDER BY p.created_at DESC"; |
| 33 | + |
| 34 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 35 | + query.setParameter("inputProductCategory", inputProductCategory); |
| 36 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 37 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 38 | + query.setFirstResult((int) pageable.getOffset()); |
| 39 | + query.setMaxResults(pageable.getPageSize()); |
| 40 | + |
| 41 | + //noinspection unchecked |
| 42 | + return query.getResultList(); |
| 43 | + } |
| 44 | + |
| 45 | + public List<GetProductDto> findByProductCategorySortByPrice( |
| 46 | + String inputProductCategory, |
| 47 | + String inputAgeCategory, |
| 48 | + String inputGenderCategory, |
| 49 | + Pageable pageable) { |
| 50 | + |
| 51 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 52 | + "p.product_category, p.age_category, p.gender_category, " + |
| 53 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 54 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 55 | + "WHERE p.product_category = :inputProductCategory " + |
| 56 | + "AND (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 57 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 58 | + "AND p.is_deleted = false " + |
| 59 | + "ORDER BY p.price ASC"; |
| 60 | + |
| 61 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 62 | + query.setParameter("inputProductCategory", inputProductCategory); |
| 63 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 64 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 65 | + query.setFirstResult((int) pageable.getOffset()); |
| 66 | + query.setMaxResults(pageable.getPageSize()); |
| 67 | + |
| 68 | + //noinspection unchecked |
| 69 | + return query.getResultList(); |
| 70 | + } |
| 71 | + |
| 72 | + public List<GetProductDto> findByProductCategorySortById( |
| 73 | + String inputProductCategory, |
| 74 | + String inputAgeCategory, |
| 75 | + String inputGenderCategory, |
| 76 | + Pageable pageable) { |
| 77 | + |
| 78 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 79 | + "p.product_category, p.age_category, p.gender_category, " + |
| 80 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 81 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 82 | + "WHERE p.product_category = :inputProductCategory " + |
| 83 | + "AND (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 84 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 85 | + "AND p.is_deleted = false " + |
| 86 | + "ORDER BY p.id ASC"; |
| 87 | + |
| 88 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 89 | + query.setParameter("inputProductCategory", inputProductCategory); |
| 90 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 91 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 92 | + query.setFirstResult((int) pageable.getOffset()); |
| 93 | + query.setMaxResults(pageable.getPageSize()); |
| 94 | + |
| 95 | + //noinspection unchecked |
| 96 | + return query.getResultList(); |
| 97 | + } |
| 98 | + |
| 99 | + public List<GetProductDto> findAllSortByCreatedAt( |
| 100 | + String inputAgeCategory, |
| 101 | + String inputGenderCategory, |
| 102 | + Pageable pageable) { |
| 103 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 104 | + "p.product_category, p.age_category, p.gender_category, " + |
| 105 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 106 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 107 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 108 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 109 | + "AND p.is_deleted = false " + |
| 110 | + "ORDER BY p.created_at DESC"; |
| 111 | + |
| 112 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 113 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 114 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 115 | + query.setFirstResult((int) pageable.getOffset()); |
| 116 | + query.setMaxResults(pageable.getPageSize()); |
| 117 | + |
| 118 | + //noinspection unchecked |
| 119 | + return query.getResultList(); |
| 120 | + } |
| 121 | + |
| 122 | + public List<GetProductDto> findAllSortByPrice( |
| 123 | + String inputAgeCategory, |
| 124 | + String inputGenderCategory, |
| 125 | + Pageable pageable) { |
| 126 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 127 | + "p.product_category, p.age_category, p.gender_category, " + |
| 128 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 129 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 130 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 131 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 132 | + "AND p.is_deleted = false " + |
| 133 | + "ORDER BY p.price ASC"; |
| 134 | + |
| 135 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 136 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 137 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 138 | + query.setFirstResult((int) pageable.getOffset()); |
| 139 | + query.setMaxResults(pageable.getPageSize()); |
| 140 | + |
| 141 | + //noinspection unchecked |
| 142 | + return query.getResultList(); |
| 143 | + } |
| 144 | + |
| 145 | + public List<GetProductDto> findAllSortById( |
| 146 | + String inputAgeCategory, |
| 147 | + String inputGenderCategory, |
| 148 | + Pageable pageable) { |
| 149 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 150 | + "p.product_category, p.age_category, p.gender_category, " + |
| 151 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 152 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 153 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 154 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 155 | + "AND p.is_deleted = false " + |
| 156 | + "ORDER BY p.id ASC"; |
| 157 | + |
| 158 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 159 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 160 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 161 | + query.setFirstResult((int) pageable.getOffset()); |
| 162 | + query.setMaxResults(pageable.getPageSize()); |
| 163 | + |
| 164 | + //noinspection unchecked |
| 165 | + return query.getResultList(); |
| 166 | + } |
| 167 | + |
| 168 | + public List<GetProductDto> searchProductSortByPrice( |
| 169 | + String searchToken, |
| 170 | + String inputAgeCategory, |
| 171 | + String inputGenderCategory, |
| 172 | + Pageable pageable) { |
| 173 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 174 | + "p.product_category, p.age_category, p.gender_category, " + |
| 175 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 176 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 177 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 178 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 179 | + "AND p.is_deleted = false " + |
| 180 | + "AND p.name LIKE :searchToken " + |
| 181 | + "ORDER BY p.price ASC"; |
| 182 | + |
| 183 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 184 | + query.setParameter("searchToken", searchToken); |
| 185 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 186 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 187 | + query.setFirstResult((int) pageable.getOffset()); |
| 188 | + query.setMaxResults(pageable.getPageSize()); |
| 189 | + |
| 190 | + //noinspection unchecked |
| 191 | + return query.getResultList(); |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + public List<GetProductDto> searchProductSortByCreatedAt(String searchToken, String inputAgeCategory, String inputGenderCategory, Pageable pageable) |
| 196 | + { |
| 197 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 198 | + "p.product_category, p.age_category, p.gender_category, " + |
| 199 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 200 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 201 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 202 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 203 | + "AND p.is_deleted = false " + |
| 204 | + "AND p.name LIKE :searchToken " + |
| 205 | + "ORDER BY p.created_at DESC"; |
| 206 | + |
| 207 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 208 | + query.setParameter("searchToken", searchToken); |
| 209 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 210 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 211 | + query.setFirstResult((int) pageable.getOffset()); |
| 212 | + query.setMaxResults(pageable.getPageSize()); |
| 213 | + |
| 214 | + //noinspection unchecked |
| 215 | + return query.getResultList(); |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + public List<GetProductDto> searchProductSortById(String searchToken, String inputAgeCategory, String inputGenderCategory, Pageable pageable) { |
| 220 | + String sql = "SELECT p.id, p.name, p.price, p.created_at, " + |
| 221 | + "p.product_category, p.age_category, p.gender_category, " + |
| 222 | + "p.left_amount, p.thumbnail_url, s.shop_name " + |
| 223 | + "FROM commerce.products p LEFT JOIN commerce.sellers s ON p.sellers_id = s.id " + |
| 224 | + "WHERE (:inputAgeCategory IS NULL OR p.age_category = :inputAgeCategory) " + |
| 225 | + "AND (:inputGenderCategory IS NULL OR p.gender_category = :inputGenderCategory) " + |
| 226 | + "AND p.is_deleted = false " + |
| 227 | + "AND p.name LIKE :searchToken " + |
| 228 | + "ORDER BY p.id ASC"; |
| 229 | + |
| 230 | + Query query = entityManager.createNativeQuery(sql, "GetProductDtoMapping"); |
| 231 | + query.setParameter("searchToken", searchToken); |
| 232 | + query.setParameter("inputAgeCategory", inputAgeCategory); |
| 233 | + query.setParameter("inputGenderCategory", inputGenderCategory); |
| 234 | + query.setFirstResult((int) pageable.getOffset()); |
| 235 | + query.setMaxResults(pageable.getPageSize()); |
| 236 | + |
| 237 | + //noinspection unchecked |
| 238 | + return query.getResultList(); |
| 239 | + |
| 240 | + } |
| 241 | +} |
0 commit comments