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

Commit f74c607

Browse files
author
Evgenii Kanivets
committed
[30m]. Refactor Repos to move a lot of code to BaseRepo.
1 parent de41a2d commit f74c607

File tree

5 files changed

+60
-158
lines changed

5 files changed

+60
-158
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/AccountRepo.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import android.content.ContentValues;
44
import android.database.Cursor;
5-
import android.database.sqlite.SQLiteDatabase;
6-
import android.support.annotation.Nullable;
7-
import android.util.Log;
5+
import android.support.annotation.NonNull;
86

97
import com.blogspot.e_kanivets.moneytracker.DbHelper;
108
import com.blogspot.e_kanivets.moneytracker.model.Account;
@@ -31,53 +29,15 @@ protected String getTable() {
3129
return DbHelper.TABLE_ACCOUNTS;
3230
}
3331

34-
@Nullable
32+
@NonNull
3533
@Override
36-
public Account create(Account account) {
37-
SQLiteDatabase db = dbHelper.getWritableDatabase();
38-
34+
protected ContentValues contentValues(Account account) {
3935
ContentValues contentValues = new ContentValues();
4036
contentValues.put(DbHelper.TITLE_COLUMN, account.getTitle());
4137
contentValues.put(DbHelper.CUR_SUM_COLUMN, account.getCurSum());
4238
contentValues.put(DbHelper.CURRENCY_COLUMN, account.getCurrency());
4339

44-
long id = db.insert(getTable(), null, contentValues);
45-
46-
db.close();
47-
48-
if (id == -1) {
49-
Log.d(TAG, "Couldn't create account : " + account);
50-
return null;
51-
} else {
52-
Account createdAccount = read(id);
53-
Log.d(TAG, "Created account : " + createdAccount);
54-
return createdAccount;
55-
}
56-
}
57-
58-
@Nullable
59-
@Override
60-
public Account update(Account account) {
61-
SQLiteDatabase db = dbHelper.getWritableDatabase();
62-
63-
ContentValues contentValues = new ContentValues();
64-
contentValues.put(DbHelper.CUR_SUM_COLUMN, account.getCurSum());
65-
contentValues.put(DbHelper.TITLE_COLUMN, account.getTitle());
66-
contentValues.put(DbHelper.CURRENCY_COLUMN, account.getCurrency());
67-
68-
String[] args = new String[]{Long.valueOf(account.getId()).toString()};
69-
long rowsAffected = db.update(getTable(), contentValues, "id=?", args);
70-
71-
db.close();
72-
73-
if (rowsAffected == 0) {
74-
Log.d(TAG, "Couldn't update account : " + account);
75-
return null;
76-
} else {
77-
Account updatedAccount = read(account.getId());
78-
Log.d(TAG, "Updated account : " + updatedAccount);
79-
return updatedAccount;
80-
}
40+
return contentValues;
8141
}
8242

8343
@Override

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/BaseRepo.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.blogspot.e_kanivets.moneytracker.repo;
22

3+
import android.content.ContentValues;
34
import android.database.Cursor;
45
import android.database.sqlite.SQLiteDatabase;
56
import android.support.annotation.NonNull;
@@ -26,9 +27,31 @@ public BaseRepo(DbHelper dbHelper) {
2627
this.dbHelper = dbHelper;
2728
}
2829

29-
abstract String getTable();
30+
abstract protected String getTable();
3031

31-
abstract List<T> getListFromCursor(Cursor cursor);
32+
@NonNull
33+
abstract protected ContentValues contentValues(T instance);
34+
35+
abstract protected List<T> getListFromCursor(Cursor cursor);
36+
37+
@Nullable
38+
@Override
39+
public T create(T instance) {
40+
SQLiteDatabase db = dbHelper.getWritableDatabase();
41+
42+
long id = db.insert(getTable(), null, contentValues(instance));
43+
44+
db.close();
45+
46+
if (id == -1) {
47+
Log.d(TAG, "Couldn't create record : " + instance);
48+
return null;
49+
} else {
50+
T createdInstance = read(id);
51+
Log.d(TAG, "Created record : " + createdInstance);
52+
return createdInstance;
53+
}
54+
}
3255

3356
@Nullable
3457
@Override
@@ -39,6 +62,26 @@ public T read(long id) {
3962
else return null;
4063
}
4164

65+
@Nullable
66+
@Override
67+
public T update(T instance) {
68+
SQLiteDatabase db = dbHelper.getWritableDatabase();
69+
70+
String[] args = new String[]{Long.valueOf(instance.getId()).toString()};
71+
long rowsAffected = db.update(getTable(), contentValues(instance), "id=?", args);
72+
73+
db.close();
74+
75+
if (rowsAffected == 0) {
76+
Log.d(TAG, "Couldn't update record : " + instance);
77+
return null;
78+
} else {
79+
T updatedInstance = read(instance.getId());
80+
Log.d(TAG, "Updated record : " + updatedInstance);
81+
return updatedInstance;
82+
}
83+
}
84+
4285
@NonNull
4386
@Override
4487
public List<T> readAll() {

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/CategoryRepo.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import android.content.ContentValues;
44
import android.database.Cursor;
5-
import android.database.sqlite.SQLiteDatabase;
6-
import android.support.annotation.Nullable;
7-
import android.util.Log;
5+
import android.support.annotation.NonNull;
86

97
import com.blogspot.e_kanivets.moneytracker.DbHelper;
108
import com.blogspot.e_kanivets.moneytracker.model.Category;
@@ -31,49 +29,13 @@ protected String getTable() {
3129
return DbHelper.TABLE_CATEGORIES;
3230
}
3331

34-
@Nullable
32+
@NonNull
3533
@Override
36-
public Category create(Category category) {
37-
SQLiteDatabase db = dbHelper.getWritableDatabase();
38-
39-
ContentValues contentValues = new ContentValues();
40-
contentValues.put(DbHelper.NAME_COLUMN, category.getName());
41-
42-
long id = db.insert(getTable(), null, contentValues);
43-
44-
db.close();
45-
46-
if (id == -1) {
47-
Log.d(TAG, "Couldn't create category : " + category);
48-
return null;
49-
} else {
50-
Category createdCategory = read(id);
51-
Log.d(TAG, "Created account : " + createdCategory);
52-
return createdCategory;
53-
}
54-
}
55-
56-
@Nullable
57-
@Override
58-
public Category update(Category category) {
59-
SQLiteDatabase db = dbHelper.getWritableDatabase();
60-
34+
protected ContentValues contentValues(Category category) {
6135
ContentValues contentValues = new ContentValues();
6236
contentValues.put(DbHelper.NAME_COLUMN, category.getName());
6337

64-
String[] args = new String[]{Long.valueOf(category.getId()).toString()};
65-
long rowsAffected = db.update(getTable(), contentValues, "id=?", args);
66-
67-
db.close();
68-
69-
if (rowsAffected == 0) {
70-
Log.d(TAG, "Couldn't update category : " + category);
71-
return null;
72-
} else {
73-
Category updatedCategory = read(category.getId());
74-
Log.d(TAG, "Updated category : " + updatedCategory);
75-
return updatedCategory;
76-
}
38+
return contentValues;
7739
}
7840

7941
@Override

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/RecordRepo.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.ContentValues;
44
import android.database.Cursor;
55
import android.database.sqlite.SQLiteDatabase;
6+
import android.support.annotation.NonNull;
67
import android.support.annotation.Nullable;
78
import android.util.Log;
89

@@ -33,11 +34,9 @@ protected String getTable() {
3334
return DbHelper.TABLE_RECORDS;
3435
}
3536

36-
@Nullable
37+
@NonNull
3738
@Override
38-
public Record create(Record record) {
39-
SQLiteDatabase db = dbHelper.getWritableDatabase();
40-
39+
protected ContentValues contentValues(Record record) {
4140
ContentValues contentValues = new ContentValues();
4241
contentValues.put(DbHelper.TIME_COLUMN, record.getTime());
4342
contentValues.put(DbHelper.TYPE_COLUMN, record.getType());
@@ -46,44 +45,7 @@ public Record create(Record record) {
4645
contentValues.put(DbHelper.PRICE_COLUMN, record.getPrice());
4746
contentValues.put(DbHelper.ACCOUNT_ID_COLUMN, record.getAccountId());
4847

49-
long id = db.insert(getTable(), null, contentValues);
50-
51-
db.close();
52-
53-
if (id == -1) {
54-
Log.d(TAG, "Couldn't create record : " + record);
55-
return null;
56-
} else {
57-
Record createdRecord = read(id);
58-
Log.d(TAG, "Created record : " + createdRecord);
59-
return createdRecord;
60-
}
61-
}
62-
63-
@Nullable
64-
@Override
65-
public Record update(Record record) {
66-
SQLiteDatabase db = dbHelper.getWritableDatabase();
67-
68-
ContentValues contentValues = new ContentValues();
69-
contentValues.put(DbHelper.TITLE_COLUMN, record.getTitle());
70-
contentValues.put(DbHelper.CATEGORY_ID_COLUMN, record.getCategoryId());
71-
contentValues.put(DbHelper.PRICE_COLUMN, record.getPrice());
72-
contentValues.put(DbHelper.ACCOUNT_ID_COLUMN, record.getAccountId());
73-
74-
String[] args = {Long.valueOf(record.getId()).toString()};
75-
long rowsAffected = db.update(getTable(), contentValues, "id=?", args);
76-
77-
db.close();
78-
79-
if (rowsAffected == 0) {
80-
Log.d(TAG, "Couldn't update record : " + record);
81-
return null;
82-
} else {
83-
Record updatedRecord = read(record.getId());
84-
Log.d(TAG, "Updated record : " + updatedRecord);
85-
return updatedRecord;
86-
}
48+
return contentValues;
8749
}
8850

8951
@Override

app/src/main/java/com/blogspot/e_kanivets/moneytracker/repo/TransferRepo.java

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import android.content.ContentValues;
44
import android.database.Cursor;
5-
import android.database.sqlite.SQLiteDatabase;
65
import android.support.annotation.NonNull;
7-
import android.support.annotation.Nullable;
8-
import android.util.Log;
96

107
import com.blogspot.e_kanivets.moneytracker.DbHelper;
11-
import com.blogspot.e_kanivets.moneytracker.controller.AccountController;
128
import com.blogspot.e_kanivets.moneytracker.model.Transfer;
139

1410
import java.util.ArrayList;
@@ -33,38 +29,17 @@ protected String getTable() {
3329
return DbHelper.TABLE_TRANSFERS;
3430
}
3531

36-
@Nullable
32+
@NonNull
3733
@Override
38-
public Transfer create(Transfer transfer) {
39-
if (transfer == null) return null;
40-
41-
SQLiteDatabase db = dbHelper.getWritableDatabase();
42-
34+
protected ContentValues contentValues(Transfer transfer) {
4335
ContentValues contentValues = new ContentValues();
4436
contentValues.put(DbHelper.TIME_COLUMN, transfer.getTime());
4537
contentValues.put(DbHelper.FROM_ACCOUNT_ID_COLUMN, transfer.getFromAccountId());
4638
contentValues.put(DbHelper.TO_ACCOUNT_ID_COLUMN, transfer.getToAccountId());
4739
contentValues.put(DbHelper.FROM_AMOUNT_COLUMN, transfer.getFromAmount());
4840
contentValues.put(DbHelper.TO_AMOUNT_COLUMN, transfer.getToAmount());
4941

50-
long id = db.insert(getTable(), null, contentValues);
51-
52-
db.close();
53-
54-
if (id == -1) {
55-
Log.d(TAG, "Couldn't create transfer : " + transfer);
56-
return null;
57-
} else {
58-
Transfer createdTransfer = read(id);
59-
Log.d(TAG, "Created transfer : " + createdTransfer);
60-
return createdTransfer;
61-
}
62-
}
63-
64-
@Nullable
65-
@Override
66-
public Transfer update(Transfer instance) {
67-
throw new IllegalStateException("Not implemented yet");
42+
return contentValues;
6843
}
6944

7045
@Override

0 commit comments

Comments
 (0)