first working version where transactions can be stored
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.constants.Field;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class IdOrString implements Mappable {
|
||||
private final Long id;
|
||||
private final String value;
|
||||
|
||||
public IdOrString(String val){
|
||||
this.value = val;
|
||||
this.id = parseOrNull(val);
|
||||
}
|
||||
|
||||
public IdOrString(long id){
|
||||
this.value = ""+id;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean isId(){
|
||||
return id != null;
|
||||
}
|
||||
|
||||
public static IdOrString of(String val){
|
||||
return new IdOrString(val);
|
||||
}
|
||||
|
||||
public static IdOrString of(UmbrellaUser user) {
|
||||
return new IdOrString(user.id());
|
||||
}
|
||||
|
||||
private static Long parseOrNull(String val) {
|
||||
try {
|
||||
return Long.parseLong(val);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public long id(){
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
var map = new HashMap<String,Object>();
|
||||
map.put(Field.VALUE,value);
|
||||
if (isId()) map.put(Field.ID, id);
|
||||
return map;
|
||||
}
|
||||
|
||||
public String value(){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -5,20 +5,20 @@ import de.srsoftware.umbrella.core.constants.Field;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
public record Transaction(long accountId, LocalDateTime date, IdOrString source, IdOrString destination, double amount, String purpose) implements Mappable {
|
||||
|
||||
|
||||
public record Transaction(long accountId, LocalDateTime date, String source, String destination, double amount, String purpose) implements Mappable {
|
||||
public static Transaction of(ResultSet rs) throws SQLException {
|
||||
var accountId = rs.getLong(Field.ACCOUNT);
|
||||
var timestamp = rs.getLong(Field.TIMESTAMP);
|
||||
var date = LocalDateTime.ofEpochSecond(timestamp,0, ZoneOffset.UTC);
|
||||
var source = rs.getString(Field.SOURCE);
|
||||
var destination = rs.getString(Field.DESTINATION);
|
||||
var source = IdOrString.of(rs.getString(Field.SOURCE));
|
||||
var destination = IdOrString.of(rs.getString(Field.DESTINATION));
|
||||
var amount = rs.getDouble(Field.AMOUNT);
|
||||
var purpose = rs.getString(Field.DESCRIPTION);
|
||||
return new Transaction(accountId,date,source,destination,amount,purpose);
|
||||
@@ -26,25 +26,11 @@ public record Transaction(long accountId, LocalDateTime date, String source, Str
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
var source = this.source;
|
||||
try {
|
||||
var userId = Long.parseLong(source);
|
||||
var user = userService().loadUser(userId);
|
||||
source = user.name();
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
var destination = this.destination;
|
||||
try {
|
||||
var userId = Long.parseLong(destination);
|
||||
var user = userService().loadUser(userId);
|
||||
destination = user.name();
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return Map.of(
|
||||
Field.ACCOUNT, accountId,
|
||||
Field.DATE, date.toLocalDate(),
|
||||
Field.SOURCE, source,
|
||||
Field.DESTINATION, destination,
|
||||
Field.SOURCE, source.toMap(),
|
||||
Field.DESTINATION, destination.toMap(),
|
||||
Field.AMOUNT, amount,
|
||||
Field.PURPOSE, purpose
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ package de.srsoftware.umbrella.core.model;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.api.NamedThing;
|
||||
import de.srsoftware.umbrella.core.api.Owner;
|
||||
import de.srsoftware.umbrella.core.constants.Module;
|
||||
import java.util.HashMap;
|
||||
|
||||
Reference in New Issue
Block a user