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

@ -8,10 +8,10 @@ import java.time.Instant;
import java.util.Date;
@Entity
@Table(name="ACCOUNTS")
@Table(name = "ACCOUNTS")
public class Account {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "accounts_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accounts_seq")
@SequenceGenerator(name = "accounts_seq", sequenceName = "accounts_seq", allocationSize = 1)
@Column(name = "user_id")
private int userId;
@ -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

@ -3,37 +3,22 @@ package com.baeldung.countrows.entity;
import javax.persistence.*;
@Entity
@Table(name="PERMISSIONS")
@Table(name = "PERMISSIONS")
public class Permission {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "permissions_id_sq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "permissions_id_sq")
@SequenceGenerator(name = "permissions_id_sq", sequenceName = "permissions_id_sq", allocationSize = 1)
private int id;
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;
@ -29,19 +31,18 @@ public class AccountStatsLogic {
@Autowired
private PermissionRepository permissionRepository;
public long getAccountCount(){
public long getAccountCount() {
return accountRepository.count();
}
public long getAccountCountByUsername(String username){
public long getAccountCountByUsername(String username) {
return accountRepository.countByUsername(username);
}
public long getAccountCountByPermission(Permission permission){
public long getAccountCountByPermission(Permission permission) {
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

@ -35,15 +35,15 @@ class AccountStatsUnitTest {
private AccountStatsLogic accountStatsLogic;
@AfterEach
public void afterEach(){
public void afterEach() {
accountRepository.deleteAll();
permissionRepository.deleteAll();
}
@Test
public void givenAccountInTable_whenPerformCount_returnsAppropriateCount(){
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();
@ -109,11 +111,11 @@ class AccountStatsUnitTest {
assertThat(count).isEqualTo(1);
}
private Account saveAccount(){
private Account saveAccount() {
return accountRepository.save(getAccount());
}
private void savePermissions(){
private void savePermissions() {
Permission editor = new Permission();
editor.setType("editor");
permissionRepository.save(editor);
@ -123,18 +125,13 @@ 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();
account.setUsername("username_"+seed);
account.setEmail("username_"+seed+"@gmail.com");
String seed = UUID.randomUUID()
.toString();
account.setUsername("username_" + seed);
account.setEmail("username_" + seed + "@gmail.com");
account.setPermission(permission);
account.setPassword("password_q1234");
account.setCreatedOn(Timestamp.from(Instant.now()));