Addressing PR feedback

This commit is contained in:
technoddy 2023-05-25 23:40:16 -04:00
parent ebbd80e6c7
commit 01e8fa41fd
5 changed files with 34 additions and 86 deletions

View File

@ -25,14 +25,6 @@ public class Account {
@JoinColumn(name = "permissions_id") @JoinColumn(name = "permissions_id")
private Permission permission; private Permission permission;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() { public String getUsername() {
return username; return username;
} }
@ -41,18 +33,10 @@ public class Account {
this.username = username; this.username = username;
} }
public String getPassword() {
return password;
}
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public String getEmail() {
return email;
}
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
@ -65,10 +49,6 @@ public class Account {
this.createdOn = createdOn; this.createdOn = createdOn;
} }
public Timestamp getLastLogin() {
return lastLogin;
}
public void setLastLogin(Timestamp lastLogin) { public void setLastLogin(Timestamp lastLogin) {
this.lastLogin = lastLogin; this.lastLogin = lastLogin;
} }
@ -83,14 +63,6 @@ public class Account {
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{" + "userId=" + userId + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", createdOn=" + createdOn + ", lastLogin=" + lastLogin + ", permission=" + permission + '}';
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", createdOn=" + createdOn +
", lastLogin=" + lastLogin +
", permission=" + permission +
'}';
} }
} }

View File

@ -13,27 +13,12 @@ public class Permission {
private String type; private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
@Override @Override
public String toString() { public String toString() {
return "Permission{" + return "Permission{" + "id=" + id + ", type='" + type + '\'' + '}';
"id=" + id +
", type='" + type + '\'' +
'}';
} }
} }

View File

@ -2,6 +2,7 @@ package com.baeldung.countrows.repository;
import com.baeldung.countrows.entity.Account; import com.baeldung.countrows.entity.Account;
import com.baeldung.countrows.entity.Permission; import com.baeldung.countrows.entity.Permission;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;

View File

@ -11,8 +11,10 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.criteria.*; import javax.persistence.criteria.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baeldung.countrows.entity.Account; import com.baeldung.countrows.entity.Account;
import com.baeldung.countrows.entity.Permission; import com.baeldung.countrows.entity.Permission;
import com.baeldung.countrows.repository.AccountRepository; import com.baeldung.countrows.repository.AccountRepository;
@ -41,7 +43,6 @@ public class AccountStatsLogic {
return accountRepository.countByPermission(permission); return accountRepository.countByPermission(permission);
} }
public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException { public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException {
return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime())); return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime()));
} }
@ -53,11 +54,11 @@ public class AccountStatsLogic {
Root<Account> accountRoot = criteriaQuery.from(Account.class); Root<Account> accountRoot = criteriaQuery.from(Account.class);
// select query // select query
criteriaQuery criteriaQuery.select(builder.count(accountRoot));
.select(builder.count(accountRoot));
// execute and get the result // execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsByPermissionUsingCQ(Permission permission) throws ParseException { public long getAccountsByPermissionUsingCQ(Permission permission) throws ParseException {
@ -68,11 +69,11 @@ public class AccountStatsLogic {
List<Predicate> predicateList = new ArrayList<>(); // list of predicates that will go in where clause List<Predicate> predicateList = new ArrayList<>(); // list of predicates that will go in where clause
predicateList.add(builder.equal(accountRoot.get("permission"), permission)); predicateList.add(builder.equal(accountRoot.get("permission"), permission));
criteriaQuery criteriaQuery.select(builder.count(accountRoot))
.select(builder.count(accountRoot))
.where(builder.and(predicateList.toArray(new Predicate[0]))); .where(builder.and(predicateList.toArray(new Predicate[0])));
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsByPermissionAndCreateOnUsingCQ(Permission permission, Date date) throws ParseException { public long getAccountsByPermissionAndCreateOnUsingCQ(Permission permission, Date date) throws ParseException {
@ -87,12 +88,12 @@ public class AccountStatsLogic {
predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime()))); predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime())));
// select query // select query
criteriaQuery criteriaQuery.select(builder.count(accountRoot))
.select(builder.count(accountRoot))
.where(builder.and(predicateList.toArray(new Predicate[0]))); .where(builder.and(predicateList.toArray(new Predicate[0])));
// execute and get the result // execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsUsingJPQL() throws ParseException { public long getAccountsUsingJPQL() throws ParseException {
@ -112,12 +113,4 @@ public class AccountStatsLogic {
query.setParameter(2, new Timestamp(date.getTime())); query.setParameter(2, new Timestamp(date.getTime()));
return (long) query.getSingleResult(); return (long) query.getSingleResult();
} }
private static Date getDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse("2023-04-29");
System.out.println("parseDate: "+parsedDate);
return parsedDate;
}
} }

View File

@ -43,7 +43,7 @@ class AccountStatsUnitTest {
@Test @Test
public void givenAccountInTable_whenPerformCount_returnsAppropriateCount() { public void givenAccountInTable_whenPerformCount_returnsAppropriateCount() {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1); assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1);
} }
@ -66,7 +66,7 @@ class AccountStatsUnitTest {
@Test @Test
public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
long count = accountStatsLogic.getAccountsUsingCQ(); long count = accountStatsLogic.getAccountsUsingCQ();
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@ -90,10 +90,11 @@ class AccountStatsUnitTest {
@Test @Test
public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
long count = accountStatsLogic.getAccountsUsingJPQL(); long count = accountStatsLogic.getAccountsUsingJPQL();
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@Test @Test
public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
@ -101,6 +102,7 @@ class AccountStatsUnitTest {
long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission()); long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission());
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@Test @Test
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
@ -123,16 +125,11 @@ class AccountStatsUnitTest {
permissionRepository.save(admin); permissionRepository.save(admin);
} }
private static Date getDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse("2023-04-29");
return parsedDate;
}
private Account getAccount() { private Account getAccount() {
Permission permission = permissionRepository.findByType("admin"); Permission permission = permissionRepository.findByType("admin");
Account account = new Account(); Account account = new Account();
String seed = UUID.randomUUID().toString(); String seed = UUID.randomUUID()
.toString();
account.setUsername("username_" + seed); account.setUsername("username_" + seed);
account.setEmail("username_" + seed + "@gmail.com"); account.setEmail("username_" + seed + "@gmail.com");
account.setPermission(permission); account.setPermission(permission);