Skip to content

Commit e585344

Browse files
AlmielkaAlmielka
Almielka
authored and
Almielka
committed
added Queries findByCompanyNameContaining, findByCompanyType
1 parent 72f6244 commit e585344

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/main/java/com/almielka/restapicrud/domain/Company.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
@Table(name = "company", indexes = {@Index(columnList = "name", unique = true)})
3838
public class Company extends AbstractNameEntity {
3939

40-
@Column(name = "imagery_type")
40+
@Column(name = "company_type")
4141
@Enumerated(EnumType.STRING)
4242
private CompanyType companyType;
4343

src/main/java/com/almielka/restapicrud/repository/ProductRepository.java

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.data.repository.query.Param;
66
import org.springframework.stereotype.Repository;
77
import org.springframework.transaction.annotation.Transactional;
8+
import com.almielka.restapicrud.domain.Company;
89

910
import java.util.Set;
1011

@@ -18,6 +19,38 @@
1819
@Repository
1920
public interface ProductRepository extends CommonRepository<Product> {
2021

22+
/**
23+
* Retrieve {@link Product}s from the data store by {@code name} of {@link Company}, returning all products
24+
* whose Company name <i>containing</i> with the given name.
25+
*
26+
* @param name Value to search for
27+
* @return a Collection of matching products (or an empty Collection if none found)
28+
*/
29+
@Query(value = "SELECT * " +
30+
"FROM product p " +
31+
"INNER JOIN company c " +
32+
"ON p.company_id = c.id " +
33+
"WHERE c.name LIKE %:name%",
34+
nativeQuery = true)
35+
@Transactional(readOnly = true)
36+
Set<Product> findByCompanyNameContaining(@Param("name") String name);
37+
38+
/**
39+
* Retrieve {@link Product}s from the data store by {@code name} of {@link Company}, returning all products
40+
* whose Company type <i>equals</i> with the given type.
41+
*
42+
* @param type Value to search for
43+
* @return a Collection of matching products (or an empty Collection if none found)
44+
*/
45+
@Query(value = "SELECT * " +
46+
"FROM product p " +
47+
"INNER JOIN company c " +
48+
"ON p.company_id = c.id " +
49+
"WHERE c.company_type = :type",
50+
nativeQuery = true)
51+
@Transactional(readOnly = true)
52+
Set<Product> findByCompanyType(@Param("type") String type);
53+
2154
@Query(value = "SELECT p, " +
2255
"COUNT(*)" +
2356
"FROM Product p " +

src/main/java/com/almielka/restapicrud/service/impl/ProductServiceImpl.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ public ProductServiceImpl(ProductRepository repository) {
2626

2727
@Override
2828
public Set<Product> findByCompanyNameContaining(String name) {
29-
return repository.findAll().stream()
30-
.filter(item -> item.getCompany().getName().contains(name))
31-
.collect(Collectors.toSet());
29+
// return repository.findAll().stream()
30+
// .filter(item -> item.getCompany().getName().contains(name))
31+
// .collect(Collectors.toSet());
32+
return repository.findByCompanyNameContaining(name);
3233
}
3334

3435
@Override
3536
public Set<Product> findByCompanyType(String type) {
36-
return repository.findAll().stream()
37-
.filter(item -> item.getCompany().getCompanyType().toString().equals(type.toUpperCase()))
38-
.collect(Collectors.toSet());
37+
// return repository.findAll().stream()
38+
// .filter(item -> item.getCompany().getCompanyType().toString().equals(type.toUpperCase()))
39+
// .collect(Collectors.toSet());
40+
return repository.findByCompanyType(type.toUpperCase());
3941
}
4042

4143
@Override

0 commit comments

Comments
 (0)