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

Commit da2044c

Browse files
author
Evgenii Kanivets
committed
[30m]. Add BaseController and refactor other controllers.
1 parent 5a088e8 commit da2044c

File tree

5 files changed

+108
-41
lines changed

5 files changed

+108
-41
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/AccountController.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
*
1414
* @author Evgenii Kanivets
1515
*/
16-
public class AccountController {
16+
public class AccountController extends BaseController<Account> {
1717
@SuppressWarnings("unused")
1818
private static final String TAG = "AccountController";
1919

20-
private IRepo<Account> accountRepo;
21-
2220
public AccountController(IRepo<Account> accountRepo) {
23-
this.accountRepo = accountRepo;
21+
super(accountRepo);
2422
}
2523

2624
public boolean recordAdded(@Nullable Record record) {
2725
if (record == null) return false;
2826

29-
Account account = accountRepo.read(record.getAccountId());
27+
Account account = repo.read(record.getAccountId());
3028
if (account == null) return false;
3129

3230
switch (record.getType()) {
@@ -42,15 +40,15 @@ public boolean recordAdded(@Nullable Record record) {
4240
break;
4341
}
4442

45-
accountRepo.update(account);
43+
repo.update(account);
4644

4745
return true;
4846
}
4947

5048
public boolean recordDeleted(@Nullable Record record) {
5149
if (record == null) return false;
5250

53-
Account account = accountRepo.read(record.getAccountId());
51+
Account account = repo.read(record.getAccountId());
5452
if (account == null) return false;
5553

5654
switch (record.getType()) {
@@ -66,7 +64,7 @@ public boolean recordDeleted(@Nullable Record record) {
6664
break;
6765
}
6866

69-
accountRepo.update(account);
67+
repo.update(account);
7068

7169
return true;
7270
}
@@ -81,16 +79,16 @@ public boolean recordUpdated(@Nullable Record oldRecord, @Nullable Record newRec
8179
public boolean transferDone(@Nullable Transfer transfer) {
8280
if(transfer == null) return false;
8381

84-
Account fromAccount = accountRepo.read(transfer.getFromAccountId());
85-
Account toAccount = accountRepo.read(transfer.getToAccountId());
82+
Account fromAccount = repo.read(transfer.getFromAccountId());
83+
Account toAccount = repo.read(transfer.getToAccountId());
8684

8785
if (fromAccount == null || toAccount == null) return false;
8886

8987
fromAccount.take(transfer.getFromAmount());
9088
toAccount.put(transfer.getToAmount());
9189

92-
accountRepo.update(fromAccount);
93-
accountRepo.update(toAccount);
90+
repo.update(fromAccount);
91+
repo.update(toAccount);
9492

9593
return true;
9694
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.blogspot.e_kanivets.moneytracker.controller;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.blogspot.e_kanivets.moneytracker.repo.IRepo;
7+
8+
import java.util.List;
9+
10+
/**
11+
* This is just a wrapper for {@link com.blogspot.e_kanivets.moneytracker.repo.IRepo} interface.
12+
* Don't use Repo classes outside of controllers.
13+
* Created on 2/17/16.
14+
*
15+
* @author Evgenii Kanivets
16+
*/
17+
public abstract class BaseController<T> implements IRepo<T> {
18+
protected IRepo<T> repo;
19+
20+
public BaseController(IRepo<T> repo) {
21+
this.repo = repo;
22+
}
23+
24+
@Nullable
25+
@Override
26+
public T create(T instance) {
27+
return repo.create(instance);
28+
}
29+
30+
@Nullable
31+
@Override
32+
public T read(long id) {
33+
return repo.read(id);
34+
}
35+
36+
@Nullable
37+
@Override
38+
public T update(T instance) {
39+
return repo.update(instance);
40+
}
41+
42+
@Override
43+
public boolean delete(T instance) {
44+
return repo.delete(instance);
45+
}
46+
47+
@NonNull
48+
@Override
49+
public List<T> readAll() {
50+
return repo.readAll();
51+
}
52+
53+
@NonNull
54+
@Override
55+
public List<T> readWithCondition(String condition, String[] args) {
56+
return repo.readWithCondition(condition, args);
57+
}
58+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/CategoryController.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
*
1313
* @author Evgenii Kanivets
1414
*/
15-
public class CategoryController {
15+
public class CategoryController extends BaseController<Category> {
1616
@SuppressWarnings("unused")
1717
private static final String TAG = "CategoryController";
1818

19-
private IRepo<Category> categoryRepo;
20-
2119
public CategoryController(IRepo<Category> categoryRepo) {
22-
this.categoryRepo = categoryRepo;
20+
super(categoryRepo);
2321
}
2422

2523
public Category readOrCreate(String categoryName) {
2624
String condition = DbHelper.NAME_COLUMN + "=?";
2725
String[] args = {categoryName};
28-
List<Category> categoryList = categoryRepo.readWithCondition(condition, args);
26+
List<Category> categoryList = repo.readWithCondition(condition, args);
2927

3028
if (categoryList.size() >= 1) return categoryList.get(0);
31-
else return categoryRepo.create(new Category(categoryName));
29+
else return repo.create(new Category(categoryName));
3230
}
3331
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/RecordController.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,60 @@
1515
*
1616
* @author Evgenii Kanivets
1717
*/
18-
public class RecordController {
18+
public class RecordController extends BaseController<Record> {
1919
private final IRepo<Category> categoryRepo;
20-
private final IRepo<Record> recordRepo;
2120
private final CategoryController categoryController;
2221
private final AccountController accountController;
2322

2423
public RecordController(IRepo<Record> recordRepo, IRepo<Category> categoryRepo,
2524
CategoryController categoryController, AccountController accountController) {
26-
this.recordRepo = recordRepo;
25+
super(recordRepo);
2726
this.categoryRepo = categoryRepo;
2827
this.categoryController = categoryController;
2928
this.accountController = accountController;
3029
}
3130

31+
@Override
3232
@SuppressWarnings("SimplifiableIfStatement")
33-
public boolean create(Record record) {
33+
public Record create(Record record) {
3434
record.setCategoryId(categoryController.readOrCreate(record.getCategory()).getId());
3535

36-
Record createdRecord = recordRepo.create(record);
37-
if (createdRecord == null) return false;
38-
else return accountController.recordAdded(createdRecord);
36+
Record createdRecord = repo.create(record);
37+
if (createdRecord == null) return null;
38+
else {
39+
accountController.recordAdded(createdRecord);
40+
return createdRecord;
41+
}
3942
}
4043

44+
@Override
4145
@SuppressWarnings("SimplifiableIfStatement")
42-
public boolean update(Record record) {
46+
public Record update(Record record) {
4347
record.setCategoryId(categoryController.readOrCreate(record.getCategory()).getId());
4448

45-
Record oldRecord = recordRepo.read(record.getId());
49+
Record oldRecord = repo.read(record.getId());
4650

47-
Record updatedRecord = recordRepo.update(record);
48-
if (updatedRecord == null) return false;
49-
else return accountController.recordUpdated(oldRecord, updatedRecord);
51+
Record updatedRecord = repo.update(record);
52+
if (updatedRecord == null) return null;
53+
else {
54+
accountController.recordUpdated(oldRecord, updatedRecord);
55+
return updatedRecord;
56+
}
5057
}
5158

59+
@Override
5260
@SuppressWarnings("SimplifiableIfStatement")
5361
public boolean delete(Record record) {
54-
if (recordRepo.delete(record)) return accountController.recordDeleted(record);
62+
if (repo.delete(record)) return accountController.recordDeleted(record);
5563
else return false;
5664
}
5765

5866
public List<Record> getRecordsForPeriod(Period period) {
59-
String condition = DbHelper.TIME_COLUMN + " BETWEEN ? AND ?";
67+
String condition = DbHelper.TIME_COLUMN + " BETWEEN ? AND ?";
6068
String[] args = new String[]{Long.toString(period.getFirst().getTime()),
6169
Long.toString(period.getLast().getTime())};
6270

63-
return recordRepo.readWithCondition(condition, args);
71+
return repo.readWithCondition(condition, args);
6472
}
6573

6674
public List<String> getRecordsForExport(long fromDate, long toDate) {
@@ -78,10 +86,10 @@ public List<String> getRecordsForExport(long fromDate, long toDate) {
7886

7987
result.add(sb.toString());
8088

81-
String condition = DbHelper.TIME_COLUMN + " BETWEEN ? AND ?";
89+
String condition = DbHelper.TIME_COLUMN + " BETWEEN ? AND ?";
8290
String[] args = new String[]{Long.toString(fromDate), Long.toString(toDate)};
8391

84-
List<Record> recordList = recordRepo.readWithCondition(condition, args);
92+
List<Record> recordList = repo.readWithCondition(condition, args);
8593

8694
for (Record record : recordList) {
8795
sb = new StringBuilder();

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/TransferController.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,26 @@
99
*
1010
* @author Evgenii Kanivets
1111
*/
12-
public class TransferController {
12+
public class TransferController extends BaseController<Transfer> {
1313
@SuppressWarnings("unused")
1414
private static final String TAG = "TransferController";
1515

16-
private IRepo<Transfer> transferRepo;
1716
private AccountController accountController;
1817

1918
public TransferController(IRepo<Transfer> transferRepo, AccountController accountController) {
20-
this.transferRepo = transferRepo;
19+
super(transferRepo);
2120
this.accountController = accountController;
2221
}
2322

23+
@Override
2424
@SuppressWarnings("SimplifiableIfStatement")
25-
public boolean create(Transfer transfer) {
26-
if (transferRepo.create(transfer) == null) return false;
27-
else return accountController.transferDone(transfer);
25+
public Transfer create(Transfer transfer) {
26+
Transfer createdTransfer = repo.create(transfer);
27+
28+
if (createdTransfer == null) return null;
29+
else {
30+
accountController.transferDone(createdTransfer);
31+
return createdTransfer;
32+
}
2833
}
2934
}

0 commit comments

Comments
 (0)