💵 payments database methods

Took 16 seconds
This commit is contained in:
Kiran Hart 2023-02-28 23:12:30 -05:00
parent 2cb0a5961c
commit 1fafa40cc9
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,35 @@
/*
* Auction House
* Copyright 2023 Kiran Hart
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ca.tweetzy.auctionhouse.auction;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
@AllArgsConstructor
@Getter
public final class AuctionPayment {
private final UUID id;
private final UUID to;
private final double amount;
private final long time;
}

View File

@ -600,6 +600,69 @@ public class DataManager extends DataManagerAbstract {
}));
}
public void getAuctionPayments(Callback<ArrayList<AuctionPayment>> callback) {
ArrayList<AuctionPayment> payments = new ArrayList<>();
this.async(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "payments")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
payments.add(extractAuctionPayment(resultSet));
}
callback.accept(null, payments);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
public void insertAuctionPayment(AuctionPayment auctionPayment, Callback<AuctionPayment> callback) {
this.thread.execute(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + getTablePrefix() + "payments (uuid, payment_for, amount, time) VALUES (?, ?, ?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "payments WHERE uuid = ?");
fetch.setString(1, auctionPayment.getId().toString());
statement.setString(1, auctionPayment.getId().toString());
statement.setString(2, auctionPayment.getTo().toString());
statement.setDouble(3, auctionPayment.getAmount());
statement.setLong(4, auctionPayment.getTime());
statement.executeUpdate();
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractAuctionPayment(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void deletePayments(Collection<UUID> payments) {
this.async(() -> this.databaseConnector.connect(connection -> {
PreparedStatement statement = connection.prepareStatement("DELETE FROM " + this.getTablePrefix() + "payments WHERE uuid = ?");
for (UUID id : payments) {
statement.setString(1, id.toString());
statement.addBatch();
}
statement.executeBatch();
}));
}
private AuctionPayment extractAuctionPayment(ResultSet resultSet) throws SQLException {
return new AuctionPayment(
UUID.fromString(resultSet.getString("uuid")),
UUID.fromString(resultSet.getString("payment_for")),
resultSet.getDouble("amount"),
resultSet.getLong("time")
);
}
private AuctionStatistic extractAuctionStatistic(ResultSet resultSet) throws SQLException {
return new AuctionStatistic(
UUID.fromString(resultSet.getString("uuid")),