From 859d959a2bf257824bb122a3584fd6cd013081e6 Mon Sep 17 00:00:00 2001 From: Bernd Ritter Date: Fri, 17 Nov 2023 15:46:26 +0100 Subject: [PATCH] Attachment-Backend --- doc/db2/01_schema/09_attachments.sql | 26 +++++-- doc/db2/01_schema/10_search.sql | 13 ++-- doc/db2/02_data/01_roles_taggroups_forums.sql | 37 +++++++++ .../java/de/holarse/backend/db/Article.java | 3 - .../de/holarse/backend/db/Attachment.java | 75 +++++++++++++++++++ .../holarse/backend/db/AttachmentGroup.java | 34 +++++++++ .../de/holarse/backend/db/AttachmentType.java | 67 +++++++++++++++++ .../backend/types/AttachmentDataType.java | 8 ++ 8 files changed, 248 insertions(+), 15 deletions(-) create mode 100644 src/main/java/de/holarse/backend/db/Attachment.java create mode 100644 src/main/java/de/holarse/backend/db/AttachmentGroup.java create mode 100644 src/main/java/de/holarse/backend/db/AttachmentType.java create mode 100644 src/main/java/de/holarse/backend/types/AttachmentDataType.java diff --git a/doc/db2/01_schema/09_attachments.sql b/doc/db2/01_schema/09_attachments.sql index 42dd53b1..36f30a41 100644 --- a/doc/db2/01_schema/09_attachments.sql +++ b/doc/db2/01_schema/09_attachments.sql @@ -1,18 +1,32 @@ +create type attachment_data_type as enum ('url', 'storage'); + +create table if not exists attachment_groups( + id integer primary key default nextval('hibernate_sequence'), + code varchar(255) not null unique, + label varchar(255) not null +); + +create table if not exists attachment_types( + id integer primary key default nextval('hibernate_sequence'), + code varchar(255) not null, + label varchar(255) not null, + attachment_group_id integer references attachment_groups(id), + datatype attachment_data_type, + unique(code, attachment_group_id) +); + create table if not exists attachments( id integer primary key default nextval('hibernate_sequence'), nodeid integer not null, - ordering integer default 0, + weight integer default 0, description varchar(255), - attachment_group varchar(255), - attachment_type varchar(255), - attachment_datatype varchar(255), + attachment_type_id integer references attachment_types(id), attachment_data varchar(4096), created timestamptz not null default CURRENT_TIMESTAMP, - updated timestamptz not null default CURRENT_TIMESTAMP, - update_userid integer references users(id) + updated timestamptz not null default CURRENT_TIMESTAMP ); \ No newline at end of file diff --git a/doc/db2/01_schema/10_search.sql b/doc/db2/01_schema/10_search.sql index 6b602ec8..df6cd502 100644 --- a/doc/db2/01_schema/10_search.sql +++ b/doc/db2/01_schema/10_search.sql @@ -25,9 +25,9 @@ SELECT LEFT JOIN node_tags ON node_tags.nodeid = ar.nodeid LEFT JOIN tags ON tags.id = node_tags.tagid LEFT JOIN attachments att ON att.id = ( - SElECT id - FROM attachments - WHERE nodeid = ar.id AND attachment_type = 'SCREENSHOT' AND attachment_group = 'IMAGE' ORDER BY id LIMIT 1 + select a2.id from attachments a2 + inner join attachment_types at2 on at2.id = a2.attachment_type_id + where at2.code = 'screenshot' order by a2.weight, a2.id limit 1 ) left join node_slugs nsl on nsl.id = ( select id @@ -59,9 +59,9 @@ SELECT LEFT JOIN node_tags ON node_tags.nodeid = nr.nodeid LEFT JOIN tags ON tags.id = node_tags.tagid LEFT JOIN attachments att ON att.id = ( - SElECT id - FROM attachments - WHERE nodeid = nr.id AND attachment_type = 'SCREENSHOT' AND attachment_group = 'IMAGE' ORDER BY id LIMIT 1 + select a2.id from attachments a2 + inner join attachment_types at2 on at2.id = a2.attachment_type_id + where at2.code = 'screenshot' order by a2.weight, a2.id limit 1 ) left join node_slugs nsl on nsl.id = ( select id @@ -103,6 +103,7 @@ ALTER MATERIALIZED VIEW mv_searchindex OWNER TO holarse; CREATE INDEX IF NOT EXISTS idx_fts_search ON mv_searchindex USING gin(document); CREATE INDEX IF NOT EXISTS idx_fts_tags ON mv_searchindex (tags, doctype); + --- --- Suggestions --- diff --git a/doc/db2/02_data/01_roles_taggroups_forums.sql b/doc/db2/02_data/01_roles_taggroups_forums.sql index 687e54d7..138027e4 100644 --- a/doc/db2/02_data/01_roles_taggroups_forums.sql +++ b/doc/db2/02_data/01_roles_taggroups_forums.sql @@ -44,3 +44,40 @@ INSERT INTO apiusers (id, login, rolename, token, active) VALUES (nextval('hibernate_sequence'), 'dbl', 'API_DRÜCKBLICK', 'ADDB0F5E7826C857D7376D1BD9BC33C0C544790A2EAC96144A8AF22B1298C940', true); -- u: dbl, p: geheim +-- 6. attachments +insert into attachment_groups (code, label) +values +('website', 'Webseiten'), +('wine', 'wine'), +('shop', 'Shops'), +('video', 'Videos'), +('image', 'Bilder/Screenshots'), +('file', 'Dateien/Anhänge'), +('repo', 'Repos'); + +insert into attachment_types (code, label, attachment_group_id, datatype) values +('link', 'Link', (select id from attachment_groups where code = 'website'), 'url'), + +('winehq', 'WineHQ', (select id from attachment_groups where code = 'wine'), 'url'), +('protondb', 'ProtonDB', (select id from attachment_groups where code = 'wine'), 'url'), +('protonofficial', 'Proton Official', (select id from attachment_groups where code = 'wine'), 'url'), +('crossoverdb', 'CrossOver', (select id from attachment_groups where code = 'wine'), 'url'), + +('steam', 'Steam', (select id from attachment_groups where code = 'shop'), 'url'), +('humble', 'HumbleStore', (select id from attachment_groups where code = 'shop'), 'url'), +('gog', 'GOG', (select id from attachment_groups where code = 'shop'), 'url'), +('ownshop', 'Hersteller', (select id from attachment_groups where code = 'shop'), 'url'), +('itch', 'Itch', (select id from attachment_groups where code = 'shop'), 'url'), + +('youtube', 'YouTube', (select id from attachment_groups where code = 'video'), 'url'), +('youtube-channel', 'YouTube-Kanal', (select id from attachment_groups where code = 'video'), 'url'), +('twitch', 'Twitch', (select id from attachment_groups where code = 'video'), 'url'), + +('screenshot', 'Screenshot', (select id from attachment_groups where code = 'image'), 'storage'), + +('file', 'Dateianhang', (select id from attachment_groups where code = 'file'), 'storage'), + +('appimage', 'AppImage', (select id from attachment_groups where code = 'repo'), 'url'), +('flatpak', 'Flatpak', (select id from attachment_groups where code = 'repo'), 'url'), +('Snap', 'Snap', (select id from attachment_groups where code = 'repo'), 'url'); + diff --git a/src/main/java/de/holarse/backend/db/Article.java b/src/main/java/de/holarse/backend/db/Article.java index 9e756fad..2a5fd747 100644 --- a/src/main/java/de/holarse/backend/db/Article.java +++ b/src/main/java/de/holarse/backend/db/Article.java @@ -8,11 +8,8 @@ import jakarta.persistence.ManyToMany; import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; -import jakarta.persistence.OrderBy; import jakarta.persistence.Table; -import java.util.ArrayList; import java.util.HashSet; -import java.util.List; import java.util.Set; @Table(name = "articles") diff --git a/src/main/java/de/holarse/backend/db/Attachment.java b/src/main/java/de/holarse/backend/db/Attachment.java new file mode 100644 index 00000000..8db17ace --- /dev/null +++ b/src/main/java/de/holarse/backend/db/Attachment.java @@ -0,0 +1,75 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package de.holarse.backend.db; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; + +@Table(name = "attachments") +@Entity +public class Attachment extends TimestampedBase { + + private static final long serialVersionUID = 1L; + + @Column(name = "nodeid") + private Integer nodeId; + + @Column + private int weight; + + @Column(length = 255) + private String description; + + @ManyToOne + @JoinColumn(name="attachment_type_id", nullable=false, referencedColumnName = "id") + private AttachmentGroup group; + + @Column(name = "attachment_data", length = 4096) + private String data; + + public Integer getNodeId() { + return nodeId; + } + + public void setNodeId(Integer nodeId) { + this.nodeId = nodeId; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public AttachmentGroup getGroup() { + return group; + } + + public void setGroup(AttachmentGroup group) { + this.group = group; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + +} diff --git a/src/main/java/de/holarse/backend/db/AttachmentGroup.java b/src/main/java/de/holarse/backend/db/AttachmentGroup.java new file mode 100644 index 00000000..9cde8e0a --- /dev/null +++ b/src/main/java/de/holarse/backend/db/AttachmentGroup.java @@ -0,0 +1,34 @@ +package de.holarse.backend.db; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Table(name = "attachment_groups") +@Entity +public class AttachmentGroup extends Base { + + private static final long serialVersionUID = 1L; + + @Column(length = 255) + private String code; + @Column(length = 255) + private String label; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + +} diff --git a/src/main/java/de/holarse/backend/db/AttachmentType.java b/src/main/java/de/holarse/backend/db/AttachmentType.java new file mode 100644 index 00000000..0ac84af2 --- /dev/null +++ b/src/main/java/de/holarse/backend/db/AttachmentType.java @@ -0,0 +1,67 @@ +package de.holarse.backend.db; + +import de.holarse.backend.types.AttachmentDataType; +import io.hypersistence.utils.hibernate.type.basic.PostgreSQLEnumType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import org.hibernate.annotations.Type; + +@Entity +@Table(name = "attachment_types") +public class AttachmentType extends Base { + + private static final long serialVersionUID = 1L; + + @Column(length = 255) + private String code; + + @Column(length = 255) + private String label; + + @ManyToOne + @JoinColumn(name="attachment_group_id", nullable=false, referencedColumnName = "id") + private AttachmentGroup attachmentGroup; + + @Enumerated(EnumType.STRING) + @Type(PostgreSQLEnumType.class) + @Column(columnDefinition = "attachment_data_type", name = "datatype") + private AttachmentDataType dataType; + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public AttachmentGroup getAttachmentGroup() { + return attachmentGroup; + } + + public void setAttachmentGroup(AttachmentGroup attachmentGroup) { + this.attachmentGroup = attachmentGroup; + } + + public AttachmentDataType getDataType() { + return dataType; + } + + public void setDataType(AttachmentDataType dataType) { + this.dataType = dataType; + } + +} diff --git a/src/main/java/de/holarse/backend/types/AttachmentDataType.java b/src/main/java/de/holarse/backend/types/AttachmentDataType.java new file mode 100644 index 00000000..2b18a756 --- /dev/null +++ b/src/main/java/de/holarse/backend/types/AttachmentDataType.java @@ -0,0 +1,8 @@ +package de.holarse.backend.types; + +public enum AttachmentDataType { + + url, + storage + +}