@Test public void testDefaultStats(){ String username = "foo"; userService.createUser(username, "123"); MatchStats stats = matchStatsService.getMatchStats(username); assertEquals(0, (int) stats.getVictories()); assertEquals(0, (int) stats.getDefeats()); }
public MatchStats getMatchStats(String username) { TypedQuery<MatchStats> query = em.createQuery( "select m from MatchStats m where m.user.username=?1", MatchStats.class); query.setParameter(1, username); List<MatchStats> results = query.getResultList(); if(!results.isEmpty()){ return results.get(0); } User user = em.find(User.class, username); if(user == null){ throw new IllegalArgumentException("No existing user: " + username); } MatchStats match = new MatchStats(); match.setUser(user); em.persist(match); return match; }
MatchStatsService { public MatchStats getMatchStats(String username) { TypedQuery<MatchStats> query = em.createQuery( "select m from MatchStats m where m.user.username=?1", MatchStats.class); query.setParameter(1, username); List<MatchStats> results = query.getResultList(); if(!results.isEmpty()){ return results.get(0); } User user = em.find(User.class, username); if(user == null){ throw new IllegalArgumentException("No existing user: " + username); } MatchStats match = new MatchStats(); match.setUser(user); em.persist(match); return match; } }
MatchStatsService { public MatchStats getMatchStats(String username) { TypedQuery<MatchStats> query = em.createQuery( "select m from MatchStats m where m.user.username=?1", MatchStats.class); query.setParameter(1, username); List<MatchStats> results = query.getResultList(); if(!results.isEmpty()){ return results.get(0); } User user = em.find(User.class, username); if(user == null){ throw new IllegalArgumentException("No existing user: " + username); } MatchStats match = new MatchStats(); match.setUser(user); em.persist(match); return match; }  }
MatchStatsService { public MatchStats getMatchStats(String username) { TypedQuery<MatchStats> query = em.createQuery( "select m from MatchStats m where m.user.username=?1", MatchStats.class); query.setParameter(1, username); List<MatchStats> results = query.getResultList(); if(!results.isEmpty()){ return results.get(0); } User user = em.find(User.class, username); if(user == null){ throw new IllegalArgumentException("No existing user: " + username); } MatchStats match = new MatchStats(); match.setUser(user); em.persist(match); return match; }  void reportVictory(String username); void reportDefeat(String username); MatchStats getMatchStats(String username); }
MatchStatsService { public MatchStats getMatchStats(String username) { TypedQuery<MatchStats> query = em.createQuery( "select m from MatchStats m where m.user.username=?1", MatchStats.class); query.setParameter(1, username); List<MatchStats> results = query.getResultList(); if(!results.isEmpty()){ return results.get(0); } User user = em.find(User.class, username); if(user == null){ throw new IllegalArgumentException("No existing user: " + username); } MatchStats match = new MatchStats(); match.setUser(user); em.persist(match); return match; }  void reportVictory(String username); void reportDefeat(String username); MatchStats getMatchStats(String username);  }