Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 23240e8

Browse files
author
Evgenii Kanivets
committed
#21[1h]. Add Rates table to DB. Add currency column to Records table.
1 parent c440b20 commit 23240e8

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/DbHelper.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,26 @@ public class DbHelper extends SQLiteOpenHelper {
4343
public static final String TO_AMOUNT_COLUMN = "to_amount";
4444
public static final String CREATED_AT_COLUMN = "created_at";
4545

46+
/* DB_VERSION = 3 */
47+
public static final String TABLE_RATES = "rates";
48+
public static final String FROM_CURRENCY_COLUMN = "from_currency";
49+
public static final String TO_CURRENCY_COLUMN = "to_currency";
50+
public static final String AMOUNT_COLUMN = "amount";
51+
4652
public DbHelper(Context context) {
4753
super(context, DB_NAME, null, DB_VERSION);
4854
}
4955

5056
@Override
5157
public void onCreate(SQLiteDatabase db) {
5258
//createDbVersion1(db);
53-
createDbVersion2(db);
59+
//createDbVersion2(db);
60+
createDbVersion3(db);
5461
}
5562

5663
@Override
5764
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
58-
if (oldVersion == 1 && newVersion == 2) {
65+
if (oldVersion < 2) {
5966
db.beginTransaction();
6067

6168
createAccountsTable(db);
@@ -77,6 +84,19 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
7784

7885
db.endTransaction();
7986
}
87+
88+
if (oldVersion < 3) {
89+
db.beginTransaction();
90+
91+
createRatesTable(db);
92+
93+
/* Add account_id column into the records table */
94+
db.execSQL("ALTER TABLE " + TABLE_RECORDS + " ADD COLUMN " + CURRENCY_COLUMN + " INTEGER;");
95+
96+
db.setTransactionSuccessful();
97+
98+
db.endTransaction();
99+
}
80100
}
81101

82102
@SuppressWarnings("unused")
@@ -94,6 +114,7 @@ private void createDbVersion1(SQLiteDatabase db) {
94114
+ NAME_COLUMN + " TEXT" + ");");
95115
}
96116

117+
@SuppressWarnings("unused")
97118
private void createDbVersion2(SQLiteDatabase db) {
98119
db.execSQL("CREATE TABLE " + TABLE_RECORDS + "("
99120
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
@@ -115,6 +136,30 @@ private void createDbVersion2(SQLiteDatabase db) {
115136
insertDefaultAccount(db);
116137
}
117138

139+
private void createDbVersion3(SQLiteDatabase db) {
140+
db.execSQL("CREATE TABLE " + TABLE_RECORDS + "("
141+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
142+
+ TIME_COLUMN + " INTEGER,"
143+
+ TYPE_COLUMN + " INTEGER,"
144+
+ TITLE_COLUMN + " TEXT,"
145+
+ CATEGORY_ID_COLUMN + " INTEGER,"
146+
+ PRICE_COLUMN + " INTEGER,"
147+
+ ACCOUNT_ID_COLUMN + " INTEGER,"
148+
+ CURRENCY_COLUMN + " TEXT);");
149+
150+
db.execSQL("CREATE TABLE " + TABLE_CATEGORIES + "("
151+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
152+
+ NAME_COLUMN + " TEXT" + ");");
153+
154+
createAccountsTable(db);
155+
156+
createTransfersTable(db);
157+
158+
createRatesTable(db);
159+
160+
insertDefaultAccount(db);
161+
}
162+
118163
private void createAccountsTable(SQLiteDatabase db) {
119164
db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + "("
120165
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
@@ -134,6 +179,15 @@ private void createTransfersTable(SQLiteDatabase db) {
134179
+ TO_AMOUNT_COLUMN + " INTEGER);");
135180
}
136181

182+
private void createRatesTable(SQLiteDatabase db) {
183+
db.execSQL("CREATE TABLE " + TABLE_RATES + "("
184+
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
185+
+ CREATED_AT_COLUMN + " INTEGER,"
186+
+ FROM_CURRENCY_COLUMN + " INTEGER,"
187+
+ TO_CURRENCY_COLUMN + " INTEGER,"
188+
+ AMOUNT_COLUMN + " REAL);");
189+
}
190+
137191
private long insertDefaultAccount(SQLiteDatabase db) {
138192
/* Insert default account for all records from DB_VERSION = 1 */
139193
ContentValues contentValues = new ContentValues();

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Record.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.blogspot.e_kanivets.moneytracker.DbHelper;
44
import com.blogspot.e_kanivets.moneytracker.MtApp;
5+
import com.blogspot.e_kanivets.moneytracker.controller.CategoryController;
56
import com.blogspot.e_kanivets.moneytracker.repo.CategoryRepo;
67

78
import java.io.Serializable;
@@ -33,7 +34,8 @@ public Record(long id, long time, int type, String title, long categoryId, int p
3334
this.accountId = accountId;
3435

3536
// TODO: Refactor this shit.
36-
Category categoryActual = new CategoryRepo(new DbHelper(MtApp.get())).read(categoryId);
37+
CategoryRepo categoryRepo = new CategoryRepo(new DbHelper(MtApp.get()));
38+
Category categoryActual = new CategoryController(categoryRepo).read(categoryId);
3739
if (categoryActual != null) category = categoryActual.getName();
3840
}
3941

0 commit comments

Comments
 (0)