Skip to content

Commit

Permalink
connection pooling & transaction commits (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
M1chaCH authored Jan 4, 2024
1 parent dcaa5cb commit 87a718a
Show file tree
Hide file tree
Showing 31 changed files with 785 additions and 500 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _should be publicly available at: 01.08.2024_
-

### Framework
- [ ] write tests for complicated data manipulations (assign tag / resolve tag conflicts -> they have a lot of edge cases)
- [x] write tests for complicated data manipulations (assign tag / resolve tag conflicts -> they have a lot of edge cases)
- [ ] db connection pooling
- one db connection per request
- limit max requests handled at the same time (maybe make "getConnection" blocking if no "slot" is open)
Expand All @@ -75,10 +75,10 @@ _should be publicly available at: 01.08.2024_
- [ ] TODOs all over the project for improvements
- [ ] dockerize
- [ ] load test
- [ ] detailed tests that user can't access data he's not allowed to
- [x] detailed tests that user can't access data he's not allowed to
- [ ] add support for more banks
- post, migros. NAB, UBS, ZKB, CS
- [ ] fronted: handle unauthorized response (send to login)
- [x] fronted: handle unauthorized response (send to login)

(GPT finance adviser)
- create a custom GPT that knows how SwissBudget works and knows how the datastructures is built
Expand Down
5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<groupId>org.postgresql</groupId>
<version>${postgres.version}</version>
</dependency>
<dependency>
<artifactId>HikariCP</artifactId>
<groupId>com.zaxxer</groupId>
<version>5.1.0</version>
</dependency>
<dependency>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

import ch.michu.tech.swissbudget.app.dto.keyword.KeywordDto;
import ch.michu.tech.swissbudget.framework.data.BaseRecordProvider;
import ch.michu.tech.swissbudget.framework.data.DataProvider;
import ch.michu.tech.swissbudget.framework.data.LoggedStatement;
import ch.michu.tech.swissbudget.generated.jooq.tables.records.KeywordRecord;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand All @@ -23,23 +21,14 @@
@ApplicationScoped
public class KeywordProvider implements BaseRecordProvider<KeywordRecord, UUID> {

protected final DataProvider data;
protected final DSLContext db;

@Inject
public KeywordProvider(DataProvider data) {
this.data = data;
this.db = data.getContext();
}

@Override
public KeywordRecord newRecord() {
public KeywordRecord newRecord(DSLContext db) {
return db.newRecord(KEYWORD);
}

@Override
public KeywordRecord fromRecord(Record result) {
KeywordRecord keyword = newRecord();
public KeywordRecord fromRecord(DSLContext db, Record result) {
KeywordRecord keyword = newRecord(db);

keyword.setId(result.getValue(KEYWORD.ID));
keyword.setKeyword(result.getValue(KEYWORD.KEYWORD_));
Expand All @@ -51,7 +40,7 @@ public KeywordRecord fromRecord(Record result) {

@Override
@LoggedStatement
public boolean fetchExists(UUID userId, UUID recordId) {
public boolean fetchExists(DSLContext db, UUID userId, UUID recordId) {
Condition userCondition = KEYWORD.USER_ID.eq(userId);
Condition keywordCondition = KEYWORD.ID.eq(recordId);

Expand All @@ -67,7 +56,7 @@ public KeywordDto asDto(Record result) {
}

@LoggedStatement
public KeywordWithTagEntity selectByKeywordWithTagName(UUID userId, String keyword) throws TooManyRowsException {
public KeywordWithTagEntity selectByKeywordWithTagName(DSLContext db, UUID userId, String keyword) throws TooManyRowsException {
keyword = "%" + keyword + "%";

Condition userCondition = KEYWORD.USER_ID.eq(userId);
Expand All @@ -94,20 +83,20 @@ public KeywordWithTagEntity selectByKeywordWithTagName(UUID userId, String keywo
}

@LoggedStatement
public List<KeywordRecord> selectKeywordsByTagId(UUID userId, UUID tagId) {
public List<KeywordRecord> selectKeywordsByTagId(DSLContext db, UUID userId, UUID tagId) {
return db.fetch(KEYWORD, KEYWORD.USER_ID.eq(userId), KEYWORD.TAG_ID.eq(tagId));
}

@LoggedStatement
public void insertKeywordToTag(UUID userId, UUID keywordId, UUID tagId, String keyword) {
public void insertKeywordToTag(DSLContext db, UUID userId, UUID keywordId, UUID tagId, String keyword) {
db
.insertInto(KEYWORD, KEYWORD.ID, KEYWORD.TAG_ID, KEYWORD.KEYWORD_, KEYWORD.USER_ID)
.values(keywordId, tagId, keyword, userId)
.execute();
}

@LoggedStatement()
public void insertKeywordsToTag(UUID userId, UUID tagId, List<String> keywords) {
public void insertKeywordsToTag(DSLContext db, UUID userId, UUID tagId, List<String> keywords) {
List<Query> insertKeywords = keywords
.stream()
.map(keyword -> (Query) db
Expand All @@ -119,7 +108,7 @@ public void insertKeywordsToTag(UUID userId, UUID tagId, List<String> keywords)
}

@LoggedStatement
public void deleteKeywordsByIds(UUID userId, UUID... keywordIds) {
public void deleteKeywordsByIds(DSLContext db, UUID userId, UUID... keywordIds) {
if (keywordIds.length == 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import ch.michu.tech.swissbudget.app.dto.keyword.KeywordDto;
import ch.michu.tech.swissbudget.app.dto.tag.TagDto;
import ch.michu.tech.swissbudget.framework.data.BaseRecordProvider;
import ch.michu.tech.swissbudget.framework.data.DataProvider;
import ch.michu.tech.swissbudget.framework.data.LoggedStatement;
import ch.michu.tech.swissbudget.generated.jooq.tables.records.KeywordRecord;
import ch.michu.tech.swissbudget.generated.jooq.tables.records.TagRecord;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -27,23 +25,14 @@ public class TagProvider implements BaseRecordProvider<TagRecord, UUID> {
public static final String DEFAULT_TAG_COLOR = "#3c3e3c";
public static final String DEFAULT_TAG_ICON = "question_mark";

protected final DataProvider data;
protected final DSLContext db;

@Inject
public TagProvider(DataProvider data) {
this.data = data;
this.db = data.getContext();
}

@Override
public TagRecord newRecord() {
public TagRecord newRecord(DSLContext db) {
return db.newRecord(TAG);
}

@Override
public TagRecord fromRecord(Record result) {
TagRecord tag = newRecord();
public TagRecord fromRecord(DSLContext db, Record result) {
TagRecord tag = newRecord(db);

tag.setId(result.getValue(TAG.ID));
tag.setIcon(result.getValue(TAG.ICON));
Expand All @@ -57,15 +46,15 @@ public TagRecord fromRecord(Record result) {

@Override
@LoggedStatement
public boolean fetchExists(UUID userId, UUID recordId) {
public boolean fetchExists(DSLContext db, UUID userId, UUID recordId) {
Condition userCondition = TAG.USER_ID.eq(userId);
Condition tagCondition = TAG.ID.eq(recordId);

return db.fetchExists(TAG, userCondition, tagCondition);
}

@LoggedStatement
public boolean fetchExists(UUID userId, UUID excludedTagId, String name) {
public boolean fetchExists(DSLContext db, UUID userId, UUID excludedTagId, String name) {
return db.fetchExists(TAG, TAG.USER_ID.eq(userId), TAG.NAME.equalIgnoreCase(name), TAG.ID.ne(excludedTagId));
}

Expand All @@ -81,7 +70,7 @@ public TagDto asDto(Record result, List<KeywordDto> keywords) {
}

@LoggedStatement
public UUID selectDefaultTagId(UUID userId) {
public UUID selectDefaultTagId(DSLContext db, UUID userId) {
return db.select(TAG.ID).from(TAG).where(TAG.USER_ID.eq(userId)).and(TAG.DEFAULT_TAG.eq(true)).fetchOne(TAG.ID);
}

Expand All @@ -92,7 +81,7 @@ public UUID selectDefaultTagId(UUID userId) {
* @return a map of Tags with their Keywords
*/
@LoggedStatement
public Map<TagRecord, List<KeywordRecord>> selectTagsWithKeywordsByUserId(UUID userId) {
public Map<TagRecord, List<KeywordRecord>> selectTagsWithKeywordsByUserId(DSLContext db, UUID userId) {
Map<TagRecord, List<KeywordRecord>> entities = new HashMap<>();

List<TagRecord> tags = db.selectFrom(TAG)
Expand All @@ -113,7 +102,7 @@ public Map<TagRecord, List<KeywordRecord>> selectTagsWithKeywordsByUserId(UUID u
}

@LoggedStatement
public List<TagDto> selectTagsWithKeywordsByUserIdAsDto(UUID userId) {
public List<TagDto> selectTagsWithKeywordsByUserIdAsDto(DSLContext db, UUID userId) {
List<TagDto> tags = db
.selectFrom(TAG)
.where(TAG.USER_ID.eq(userId))
Expand All @@ -129,7 +118,7 @@ public List<TagDto> selectTagsWithKeywordsByUserIdAsDto(UUID userId) {
}

@LoggedStatement
public void insertCompleteTag(UUID userId, UUID tagId, String name, String color, String icon, List<String> keywords) {
public void insertCompleteTag(DSLContext db, UUID userId, UUID tagId, String name, String color, String icon, List<String> keywords) {
db.transaction(ctx -> {
DSLContext dsl = ctx.dsl();

Expand All @@ -146,7 +135,7 @@ public void insertCompleteTag(UUID userId, UUID tagId, String name, String color
}

@LoggedStatement
public void updateTag(UUID userId, UUID tagId, String name, String color, String icon) {
public void updateTag(DSLContext db, UUID userId, UUID tagId, String name, String color, String icon) {
db.update(TAG)
.set(TAG.ICON, icon)
.set(TAG.COLOR, color)
Expand All @@ -157,7 +146,7 @@ public void updateTag(UUID userId, UUID tagId, String name, String color, String
}

@LoggedStatement
public void deleteById(UUID userId, UUID tagId) {
public void deleteById(DSLContext db, UUID userId, UUID tagId) {
db.deleteFrom(TAG).where(TAG.USER_ID.eq(userId)).and(TAG.ID.eq(tagId)).execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import static ch.michu.tech.swissbudget.generated.jooq.tables.TransactionMail.TRANSACTION_MAIL;

import ch.michu.tech.swissbudget.framework.data.BaseRecordProvider;
import ch.michu.tech.swissbudget.framework.data.DataProvider;
import ch.michu.tech.swissbudget.framework.data.LoggedStatement;
import ch.michu.tech.swissbudget.generated.jooq.tables.records.TransactionMailRecord;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.UUID;
import org.jooq.Condition;
import org.jooq.DSLContext;
Expand All @@ -16,23 +14,14 @@
@ApplicationScoped
public class TransactionMailProvider implements BaseRecordProvider<TransactionMailRecord, UUID> {

protected final DataProvider data;
protected final DSLContext db;

@Inject
public TransactionMailProvider(DataProvider data) {
this.data = data;
this.db = data.getContext();
}

@Override
public TransactionMailRecord newRecord() {
public TransactionMailRecord newRecord(DSLContext db) {
return db.newRecord(TRANSACTION_MAIL);
}

@Override
public TransactionMailRecord fromRecord(Record result) {
TransactionMailRecord mail = newRecord();
public TransactionMailRecord fromRecord(DSLContext db, Record result) {
TransactionMailRecord mail = newRecord(db);

mail.setId(result.getValue(TRANSACTION_MAIL.ID));
mail.setMessageNumber(result.getValue(TRANSACTION_MAIL.MESSAGE_NUMBER));
Expand All @@ -50,7 +39,7 @@ public TransactionMailRecord fromRecord(Record result) {

@Override
@LoggedStatement
public boolean fetchExists(UUID userId, UUID recordId) {
public boolean fetchExists(DSLContext db, UUID userId, UUID recordId) {
Condition userCondition = TRANSACTION_MAIL.USER_ID.eq(userId);
Condition transactionMailCondition = TRANSACTION_MAIL.ID.eq(recordId);

Expand Down
Loading

0 comments on commit 87a718a

Please sign in to comment.