Skip to content
Permalink
Browse files
Bug fixes discovered through testing.
  • Loading branch information
jamesbognar committed Dec 20, 2019
1 parent 4e0152d commit f3accd40cf17c5799c3d883e16d992d20e7ed90a
Showing 4 changed files with 24 additions and 13 deletions.
@@ -54,7 +54,7 @@ public class Pet {
private Species species;

@ElementCollection(fetch=FetchType.EAGER) @OrderColumn
@Schema(description="Pet attributes.", example="friendly,smart")
@Schema(description="Pet attributes.", example="friendly,smart")
private List<String> tags;

@Column @Enumerated(STRING)
@@ -86,7 +86,7 @@ public Pet apply(UpdatePet x) {
this.name = x.getName();
this.price = x.getPrice();
this.species = x.getSpecies();
this.tags = Arrays.asList(x.getTags());
this.tags = x.getTags() == null ? null : Arrays.asList(x.getTags());
this.status = x.getStatus();
return this;
}
@@ -2,6 +2,8 @@



import java.util.Optional;

import org.apache.juneau.petstore.dto.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@@ -13,7 +15,7 @@
public interface UserRepository extends JpaRepository <User, Long>{

@SuppressWarnings("javadoc")
User findByUsername(String username);
Optional<User> findByUsername(String username);

@SuppressWarnings("javadoc")
Long deleteByUsername(String username);
@@ -96,6 +96,7 @@
)
}
),
debug="true",
staticFiles={"htdocs:/htdocs"} // Expose static files in htdocs subpackage.
)
@HtmlDocConfig(
@@ -25,6 +25,7 @@
import org.apache.juneau.petstore.repository.PetRepository;
import org.apache.juneau.petstore.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;



@@ -42,13 +43,13 @@
*/
public class PetStoreService {


@Autowired
private PetRepository petRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private OrderRepository orderRepository;
//-----------------------------------------------------------------------------------------------------------------
@@ -65,7 +66,7 @@ public class PetStoreService {
*/
public PetStoreService initDirect(PrintWriter w) throws ParseException, IOException {


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

for (Pet x : petRepository.findAll()) {
@@ -80,7 +81,7 @@ public PetStoreService initDirect(PrintWriter w) throws ParseException, IOExcept
userRepository.delete(x);
w.println(format("Deleted user: username={0}", x.getUsername()));
}


for (Pet x : parser.parse(getStream("init/Pets.json"), Pet[].class)) {
petRepository.save(x);
@@ -135,7 +136,7 @@ public Order getOrder(long id) throws IdNotFound {
*/
public User getUser(String username) throws InvalidUsername, IdNotFound {
assertValidUsername(username);
return userRepository.findByUsername(username);
return userRepository.findByUsername(username).orElseThrow(() -> new IdNotFound(username, User.class));
}

/**
@@ -171,7 +172,7 @@ public List<User> getUsers() {
* @param c The pet input data.
* @return a new {@link Pet} object.
*/
public Pet create(CreatePet c) {
public Pet create(CreatePet c) {
return petRepository.save((new Pet().status(PetStatus.AVAILABLE).apply(c)));
}

@@ -192,6 +193,7 @@ public Order create(CreateOrder c) {
* @return a new {@link User} object.
*/
public User create(User c) {
assertValidUsername(c.getUsername());
return userRepository.save((new User().apply(c)));
}

@@ -202,8 +204,9 @@ public User create(User c) {
* @return The updated {@link Pet} object.
* @throws IdNotFound Pet was not found.
*/
@Transactional(rollbackFor=Exception.class)
public Pet update(UpdatePet u) throws IdNotFound {
Pet pet = petRepository.findById(u.getId()).get();
Pet pet = petRepository.findById(u.getId()).orElseThrow(() -> new IdNotFound(u.getId(), Pet.class));
return petRepository.save(pet.apply(u));
}

@@ -214,8 +217,9 @@ public Pet update(UpdatePet u) throws IdNotFound {
* @return The updated {@link Order} object.
* @throws IdNotFound Order was not found.
*/
@Transactional(rollbackFor=Exception.class)
public Order update(Order o) throws IdNotFound {
Order order = orderRepository.findById(o.getId()).get();
Order order = orderRepository.findById(o.getId()).orElseThrow(() -> new IdNotFound(o.getId(), Order.class));
return orderRepository.save(order.apply(o));
}

@@ -227,8 +231,9 @@ public Order update(Order o) throws IdNotFound {
* @throws IdNotFound User was not found.
* @throws InvalidUsername The username was not valid.
*/
@Transactional(rollbackFor=Exception.class)
public User update(User u) throws IdNotFound, InvalidUsername {
User user = userRepository.findByUsername(u.getUsername());
User user = userRepository.findByUsername(u.getUsername()).orElseThrow(() -> new IdNotFound(u.getUsername(), Order.class));
return userRepository.save(user.apply(u));
}

@@ -238,6 +243,7 @@ public User update(User u) throws IdNotFound, InvalidUsername {
* @param id The pet ID.
* @throws IdNotFound Pet was not found.
*/
@Transactional(rollbackFor=Exception.class)
public void removePet(long id) throws IdNotFound {
petRepository.deleteById(id);
}
@@ -248,6 +254,7 @@ public void removePet(long id) throws IdNotFound {
* @param id The order ID.
* @throws IdNotFound Order was not found.
*/
@Transactional(rollbackFor=Exception.class)
public void removeOrder(long id) throws IdNotFound {
orderRepository.deleteById(id);
}
@@ -258,6 +265,7 @@ public void removeOrder(long id) throws IdNotFound {
* @param username The username.
* @throws IdNotFound User was not found.
*/
@Transactional(rollbackFor=Exception.class)
public void removeUser(String username) throws IdNotFound {
userRepository.deleteByUsername(username);
}

0 comments on commit f3accd4

Please sign in to comment.