1
0

PackageController+Repository with name search option

This commit is contained in:
Michael Hoennig
2022-08-04 09:26:01 +02:00
parent 212b1077c8
commit 8605d4c4f1
8 changed files with 53 additions and 31 deletions

View File

@ -29,11 +29,10 @@ public class CustomerController {
if (assumedRoles != null && !assumedRoles.isBlank()) {
context.assumeRoles(assumedRoles);
}
return customerRepository.findCustomerByOptionalPrefix(prefix);
return customerRepository.findCustomerByOptionalPrefixLike(prefix);
}
@PostMapping(value = "/api/customers")
@ResponseStatus
@Transactional
public CustomerEntity addCustomer(
@RequestHeader(value = "current-user") String userName,

View File

@ -14,7 +14,7 @@ public interface CustomerRepository extends Repository<CustomerEntity, UUID> {
Optional<CustomerEntity> findByUuid(UUID id);
@Query("SELECT c FROM CustomerEntity c WHERE :prefix is null or c.prefix like concat(:prefix, '%')")
List<CustomerEntity> findCustomerByOptionalPrefix(@Param("prefix") String prefix);
List<CustomerEntity> findCustomerByOptionalPrefixLike(@Param("prefix") String prefix);
CustomerEntity save(final CustomerEntity entity);

View File

@ -2,16 +2,12 @@ package net.hostsharing.hsadminng.hs.hspackage;
import net.hostsharing.hsadminng.context.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import javax.transaction.Transactional;
import java.util.List;
@Controller
@RestController
public class PackageController {
@Autowired
@ -20,18 +16,18 @@ public class PackageController {
@Autowired
private PackageRepository packageRepository;
@ResponseBody
@RequestMapping(value = "/api/packages", method = RequestMethod.GET)
@Transactional
public List<PackageEntity> listPackages(
@RequestHeader(value = "current-user") String userName,
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles
@RequestHeader(value = "assumed-roles", required = false) String assumedRoles,
@RequestParam(required = false) String name
) {
context.setCurrentUser(userName);
if (assumedRoles != null && !assumedRoles.isBlank()) {
context.assumeRoles(assumedRoles);
}
return packageRepository.findAll();
return packageRepository.findAllByOptionalNameLike(name);
}
}

View File

@ -1,9 +1,13 @@
package net.hostsharing.hsadminng.hs.hspackage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import java.util.List;
import java.util.UUID;
public interface PackageRepository extends JpaRepository<PackageEntity, UUID> {
public interface PackageRepository extends Repository<PackageEntity, UUID> {
@Query("SELECT p FROM PackageEntity p WHERE :name is null or p.name like concat(:name, '%')")
List<PackageEntity> findAllByOptionalNameLike(final String name);
}