diff --git a/src/de/steamwar/sql/Token.java b/src/de/steamwar/sql/Token.java new file mode 100644 index 0000000..db34967 --- /dev/null +++ b/src/de/steamwar/sql/Token.java @@ -0,0 +1,83 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.sql; + +import de.steamwar.sql.internal.Field; +import de.steamwar.sql.internal.SelectStatement; +import de.steamwar.sql.internal.Statement; +import de.steamwar.sql.internal.Table; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; + +import java.sql.Timestamp; +import java.util.List; + +@AllArgsConstructor +@Getter +@ToString +public class Token { + private static final Table table = new Table<>(Token.class); + private static final Statement insert = table.insertFields(true, "Name", "Owner", "Hash"); + private static final SelectStatement get = table.select(Table.PRIMARY); + private static final SelectStatement listUser = table.selectFields("owner"); + private static final SelectStatement getHash = table.selectFields("hash"); + private static final Statement delete = table.delete(Table.PRIMARY); + + public static Token create(String name, SteamwarUser owner, String hash) { + int id = insert.insertGetKey(name, owner, hash); + return get(id); + } + + public static Token get(int id) { + return get.select(id); + } + + public static List listUser(SteamwarUser owner) { + return listUser.listSelect(owner); + } + + public static Token get(String hash) { + return getHash.select(hash); + } + + public static void delete(Token id) { + delete.update(id.getId()); + } + + @Field(keys = Table.PRIMARY, autoincrement = true) + private final int id; + @Field(keys = "NameOwner") + private final String name; + @Field(keys = "NameOwner") + private final int owner; + @Field + private final Timestamp created; + @Field + private final String hash; + + public void delete() { + delete(this); + } + + public SteamwarUser getOwner() { + return SteamwarUser.get(owner); + } +}