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")
private Permission permission;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
@ -41,18 +33,10 @@ public class Account {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@ -65,10 +49,6 @@ public class Account {
this.createdOn = createdOn;
}
public Timestamp getLastLogin() {
return lastLogin;
}
public void setLastLogin(Timestamp lastLogin) {
this.lastLogin = lastLogin;
}
@ -83,14 +63,6 @@ public class Account {
@Override
public String toString() {
return "Account{" +
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", createdOn=" + createdOn +
", lastLogin=" + lastLogin +
", permission=" + permission +
'}';
return "Account{" + "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;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Permission{" +
"id=" + id +
", type='" + type + '\'' +
'}';
return "Permission{" + "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.Permission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

View File

@ -11,8 +11,10 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.countrows.entity.Account;
import com.baeldung.countrows.entity.Permission;
import com.baeldung.countrows.repository.AccountRepository;
@ -41,7 +43,6 @@ public class AccountStatsLogic {
return accountRepository.countByPermission(permission);
}
public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException {
return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime()));
}
@ -53,11 +54,11 @@ public class AccountStatsLogic {
Root<Account> accountRoot = criteriaQuery.from(Account.class);
// select query
criteriaQuery
.select(builder.count(accountRoot));
criteriaQuery.select(builder.count(accountRoot));
// execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult();
return entityManager.createQuery(criteriaQuery)
.getSingleResult();
}
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
predicateList.add(builder.equal(accountRoot.get("permission"), permission));
criteriaQuery
.select(builder.count(accountRoot))
criteriaQuery.select(builder.count(accountRoot))
.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 {
@ -87,12 +88,12 @@ public class AccountStatsLogic {
predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime())));
// select query
criteriaQuery
.select(builder.count(accountRoot))
criteriaQuery.select(builder.count(accountRoot))
.where(builder.and(predicateList.toArray(new Predicate[0])));
// execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult();
return entityManager.createQuery(criteriaQuery)
.getSingleResult();
}
public long getAccountsUsingJPQL() throws ParseException {
@ -112,12 +113,4 @@ public class AccountStatsLogic {
query.setParameter(2, new Timestamp(date.getTime()));
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
public void givenAccountInTable_whenPerformCount_returnsAppropriateCount() {
savePermissions();
Account account = saveAccount();
saveAccount();
assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1);
}
@ -66,7 +66,7 @@ class AccountStatsUnitTest {
@Test
public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException {
savePermissions();
Account account = saveAccount();
saveAccount();
long count = accountStatsLogic.getAccountsUsingCQ();
assertThat(count).isEqualTo(1);
}
@ -90,10 +90,11 @@ class AccountStatsUnitTest {
@Test
public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions();
Account account = saveAccount();
saveAccount();
long count = accountStatsLogic.getAccountsUsingJPQL();
assertThat(count).isEqualTo(1);
}
@Test
public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions();
@ -101,6 +102,7 @@ class AccountStatsUnitTest {
long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission());
assertThat(count).isEqualTo(1);
}
@Test
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions();
@ -123,16 +125,11 @@ class AccountStatsUnitTest {
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() {
Permission permission = permissionRepository.findByType("admin");
Account account = new Account();
String seed = UUID.randomUUID().toString();
String seed = UUID.randomUUID()
.toString();
account.setUsername("username_" + seed);
account.setEmail("username_" + seed + "@gmail.com");
account.setPermission(permission);