Skip to content
Snippets Groups Projects
Commit 098090fe authored by Pavel Tetauer's avatar Pavel Tetauer
Browse files

Fix

parent b03a05f8
No related branches found
No related tags found
1 merge request!30Analytics persistance
Showing with 39 additions and 56 deletions
......@@ -2,10 +2,10 @@ package cz.muni.fi.obs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
public class AnalyticsManagement {
public static void main(String[] args) {
......
package cz.muni.fi.obs.data;
import cz.muni.fi.obs.data.dbo.DailyTransaction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface AnalyticsRepository extends JpaRepository<DailyTransaction, String>{
@Query("SELECT dt FROM DailyTransaction dt WHERE dt.account.accountNumber = ?1 AND dt.date.year = ?2 AND dt.date.month = ?3")
default List<DailyTransaction> getDailyTransactions(String accountNumber, int year, int month){
return findAll();
}
public interface AnalyticsRepository extends JpaRepository<DailyTransaction, String> {
@Query("SELECT dt FROM DailyTransaction dt WHERE dt.account.accountNumber = ?1 AND dt.date.yearNumber = ?2 AND dt.date.monthNumber = ?3")
List<DailyTransaction> getDailyTransactions(String accountNumber, int year, int month);
}
......@@ -17,21 +17,21 @@ import java.time.LocalDate;
public class Date extends Dbo {
@Column(nullable = false)
int year;
int yearNumber;
@Column(nullable = false)
int month;
int monthNumber;
@Column(nullable = false)
int day;
int dayNumber;
@Column(nullable = false)
LocalDate fullDate;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
this.yearNumber = year;
this.monthNumber = month;
this.dayNumber = day;
this.fullDate = LocalDate.of(year, month, day);
}
}
......@@ -61,7 +61,7 @@ public class AnalyticsService {
}
private MonthlySummary createMonthlySummary(List<DailyTransaction> transactions) {
String month = Month.of(transactions.getFirst().getDate().getMonth()).name();
String month = Month.of(transactions.getFirst().getDate().getMonthNumber()).name();
Integer totalWithdrawalTransactions = 0;
Integer totalDepositTransactions = 0;
BigDecimal totalWithdrawalAmount = new BigDecimal(0);
......
DROP TABLE IF EXISTS daily_transaction;
DROP TABLE IF EXISTS account_dim;
DROP TABLE IF EXISTS date_dim;
CREATE TABLE account_dim
(
id varchar(40) PRIMARY KEY,
......@@ -10,11 +6,11 @@ CREATE TABLE account_dim
CREATE TABLE date_dim
(
id varchar(40) PRIMARY KEY,
year INTEGER NOT NULL,
month INTEGER NOT NULL,
day INTEGER NOT NULL,
full_date DATE NOT NULL
id varchar(40) PRIMARY KEY,
year_number INTEGER NOT NULL,
month_number INTEGER NOT NULL,
day_number INTEGER NOT NULL,
full_date DATE NOT NULL
);
......@@ -33,4 +29,6 @@ CREATE TABLE daily_transaction
constraint fk_account_id foreign key (account_id) references account_dim (id),
constraint fk_date_id foreign key (date_id) references date_dim (id)
);
\ No newline at end of file
);
CREATE INDEX account_id_index ON daily_transaction (account_id);
\ No newline at end of file
package cz.muni.fi.obs.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaRepositoryConfig {
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package cz.muni.fi.obs.service;
import cz.muni.fi.obs.Application;
import cz.muni.fi.obs.api.DailySummaryResult;
import cz.muni.fi.obs.api.MonthlySummaryResult;
import cz.muni.fi.obs.data.AnalyticsRepository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -28,9 +27,6 @@ import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TE
@SpringBootTest
class AnalyticsServiceTest {
@Autowired
private AnalyticsRepository analyticsRepository;
@Autowired
private AnalyticsService analyticsService;
......@@ -45,12 +41,12 @@ class AnalyticsServiceTest {
void getDailySummary_withFactsPresent_createsCorrectSummary() {
DailySummaryResult dailySummaryResult = analyticsService.getDailySummary("1234567890", 2021, 1);
assertThat(dailySummaryResult.summaries().size()).isEqualTo(1);
assertThat(dailySummaryResult.summaries().size()).isEqualTo(3);
}
@Test
void getMonthlySummary_noFactsPresentForAccount_createsEmptySummary() {
MonthlySummaryResult monthlySummary = analyticsService.getMonthlySummary("1234567890", 2021, 1);
MonthlySummaryResult monthlySummary = analyticsService.getMonthlySummary("12345678900", 2021, 1);
assertThat(monthlySummary.summary()).isNotNull();
assertThat(monthlySummary.summary().month()).isEqualTo(Month.of(1).toString());
......
DELETE
FROM account_dim ac
where ac.id = ac.id;
DELETE
FROM daily_transaction dt
where dt.id = dt.id;
DELETE
FROM date_dim d
where d.id = d.id;
DROP TABLE account_dim CASCADE;
DROP TABLE date_dim CASCADE;
DROP TABLE daily_transaction CASCADE;
\ No newline at end of file
INSERT INTO account_dim (id, account_number)
VALUES ('1', '1234567890');
INSERT INTO date_dim (id, year, month, day, full_date)
INSERT INTO date_dim (id, year_number, month_number, day_number, full_date)
VALUES ('1', 2021, 1, 1, '2021-01-01');
INSERT INTO date_dim (id, year, month, day, full_date)
INSERT INTO date_dim (id, year_number, month_number, day_number, full_date)
VALUES ('2', 2021, 1, 2, '2021-01-02');
INSERT INTO date_dim (id, year_number, month_number, day_number, full_date)
VALUES ('3', 2021, 1, 3, '2021-01-03');
INSERT INTO daily_transaction (id, total_withdrawal_transactions, total_deposit_transactions, total_transaction_amount,
total_withdrawal_amount, total_deposit_amount, average_withdrawal_amount,
average_deposit_amount, account_id, date_id)
VALUES ('1', 1, 1, 100.00, 50.00, 50.00, 50.00, 50.00, '1', '1');
VALUES ('1', 5, 5, 30000, 20000, 10000, 4000, 2000, '1', '1');
INSERT INTO daily_transaction (id, total_withdrawal_transactions, total_deposit_transactions, total_transaction_amount,
total_withdrawal_amount, total_deposit_amount, average_withdrawal_amount,
average_deposit_amount, account_id, date_id)
VALUES ('2', 2, 2, 200.00, 100.00, 100.00, 50.00, 50.00, '1', '2');
VALUES ('2', 10, 10, 40000, 20000, 20000, 2000, 2000, '1', '2');
INSERT INTO daily_transaction (id, total_withdrawal_transactions, total_deposit_transactions, total_transaction_amount,
total_withdrawal_amount, total_deposit_amount, average_withdrawal_amount,
average_deposit_amount, account_id, date_id)
VALUES ('3', 4, 2, 10000, 8000, 2000, 2000, 1000, '1', '3');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment