Skip to content
Permalink
Browse files
service find by tags and status
  • Loading branch information
fiammara committed Oct 13, 2019
1 parent 7d0b9ed commit 30759e60ba76fad511538483e2b202a72be7396e
Showing 2 changed files with 24 additions and 29 deletions.
@@ -1,10 +1,19 @@
package org.apache.juneau.petstore.repository;

import java.util.List;

import org.apache.juneau.petstore.dto.Pet;
import org.apache.juneau.petstore.dto.PetStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface PetRepository extends JpaRepository <Pet, Long> {

@Query("select X from PetstorePet X where X.tags in :tags")
List<Pet> findByTags(@Param("tags") String[] tags);

@Query("select X from PetstorePet X where X.status in :status")
List<Pet> findByStatus(@Param("status") PetStatus[] status);
}
@@ -17,7 +17,6 @@
import java.io.*;
import java.util.*;

import javax.persistence.*;

import org.apache.juneau.json.*;
import org.apache.juneau.parser.*;
@@ -42,7 +41,7 @@
* <li class='extlink'>{@source}
* </ul>
*/
public class PetStoreService extends AbstractPersistenceService {
public class PetStoreService {


@Autowired
@@ -67,43 +66,36 @@ public class PetStoreService extends AbstractPersistenceService {
*/
public PetStoreService initDirect(PrintWriter w) throws ParseException, IOException {

EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();

JsonParser parser = JsonParser.create().build();

et.begin();

for (Pet x : em.createQuery("select X from PetstorePet X", Pet.class).getResultList()) {
em.remove(x);
for (Pet x : petRepository.findAll()) {
petRepository.delete(x);
w.println(format("Deleted pet: id={0}", x.getId()));
}
for (Order x : em.createQuery("select X from PetstoreOrder X", Order.class).getResultList()) {
em.remove(x);
for (Order x : orderRepository.findAll()) {
orderRepository.delete(x);
w.println(format("Deleted order: id={0}", x.getId()));
}
for (User x : em.createQuery("select X from PetstoreUser X", User.class).getResultList()) {
em.remove(x);
for (User x : userRepository.findAll()) {
userRepository.delete(x);
w.println(format("Deleted user: username={0}", x.getUsername()));
}

et.commit();
et.begin();


for (Pet x : parser.parse(getStream("init/Pets.json"), Pet[].class)) {
x = em.merge(x);
petRepository.save(x);
w.println(format("Created pet: id={0}, name={1}", x.getId(), x.getName()));
}
for (Order x : parser.parse(getStream("init/Orders.json"), Order[].class)) {
x = em.merge(x);
orderRepository.save(x);
w.println(format("Created order: id={0}", x.getId()));
}
for (User x: parser.parse(getStream("init/Users.json"), User[].class)) {
x = em.merge(x);
userRepository.save(x);
w.println(format("Created user: username={0}", x.getUsername()));
}

et.commit();

return this;
}

@@ -278,10 +270,7 @@ public void removeUser(String username) throws IdNotFound {
* @return Pets with the specified statuses.
*/
public Collection<Pet> getPetsByStatus(PetStatus[] status) {
return getEntityManager()
.createQuery("select X from PetstorePet X where X.status in :status", Pet.class)
.setParameter("status", status)
.getResultList();
return petRepository.findByStatus(status);
}

/**
@@ -292,10 +281,7 @@ public Collection<Pet> getPetsByStatus(PetStatus[] status) {
* @throws InvalidTag Tag name was invalid.
*/
public Collection<Pet> getPetsByTags(String[] tags) throws InvalidTag {
return getEntityManager()
.createQuery("select X from PetstorePet X where X.tags in :tags", Pet.class)
.setParameter("tags", tags)
.getResultList();
return petRepository.findByTags(tags);
}

/**

0 comments on commit 30759e6

Please sign in to comment.