Erster Aufbau Forum

This commit is contained in:
Torsten Schulz
2024-10-15 16:28:42 +02:00
parent c31be3f879
commit 663564aa96
163 changed files with 9449 additions and 116 deletions

View File

@@ -18,8 +18,18 @@ import FolderImageVisibility from './community/folder_image_visibility.js';
import ImageImageVisibility from './community/image_image_visibility.js';
import FolderVisibilityUser from './community/folder_visibility_user.js';
import GuestbookEntry from './community/guestbook.js';
import Forum from './forum/forum.js';
import Title from './forum/title.js';
import Message from './forum/message.js';
import MessageImage from './forum/message_image.js';
import MessageHistory from './forum/message_history.js';
import TitleHistory from './forum/title_history.js';
import ForumPermission from './forum/forum_permission.js';
import ForumUserPermission from './forum/forum_user_permission.js';
import ForumForumPermission from './forum/forum_forum_permission.js';
export default function setupAssociations() {
// UserParam related associations
SettingsType.hasMany(UserParamType, { foreignKey: 'settingsId', as: 'user_param_types' });
UserParamType.belongsTo(SettingsType, { foreignKey: 'settingsId', as: 'settings_type' });
@@ -29,13 +39,19 @@ export default function setupAssociations() {
User.hasMany(UserParam, { foreignKey: 'userId', as: 'user_params' });
UserParam.belongsTo(User, { foreignKey: 'userId', as: 'user' });
// UserRight related associations
UserRight.belongsTo(User, { foreignKey: 'userId', as: 'user_with_rights' });
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
UserRightType.hasMany(UserRight, { foreignKey: 'rightTypeId', as: 'user_rights' });
UserParamType.hasMany(UserParamValue, { foreignKey: 'userParamTypeId', as: 'user_param_values' });
UserParamValue.belongsTo(UserParamType, { foreignKey: 'userParamTypeId', as: 'user_param_type' });
// UserParamVisibility related associations
UserParam.hasMany(UserParamVisibility, { foreignKey: 'param_id', as: 'param_visibilities' });
UserParamVisibility.belongsTo(UserParam, { foreignKey: 'param_id', as: 'param' });
UserParamVisibility.belongsTo(UserParamVisibilityType, { foreignKey: 'visibility', as: 'visibility_type' });
UserParamVisibilityType.hasMany(UserParamVisibility, { foreignKey: 'visibility', as: 'user_param_visibilities' });
// Interest related associations
InterestType.hasMany(InterestTranslationType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestTranslationType.belongsTo(InterestType, { foreignKey: 'interestsId', as: 'interest_translations' });
@@ -44,14 +60,7 @@ export default function setupAssociations() {
Interest.belongsTo(InterestType, { foreignKey: 'userinterestId', as: 'interest_type' });
Interest.belongsTo(User, { foreignKey: 'userId', as: 'interest_owner' });
InterestTranslationType.belongsTo(UserParamValue, { foreignKey: 'language', targetKey: 'id', as: 'user_param_value' });
UserParam.hasMany(UserParamVisibility, { foreignKey: 'param_id', as: 'param_visibilities' });
UserParamVisibility.belongsTo(UserParam, { foreignKey: 'param_id', as: 'param' });
UserParamVisibility.belongsTo(UserParamVisibilityType, { foreignKey: 'visibility', as: 'visibility_type' });
UserParamVisibilityType.hasMany(UserParamVisibility, { foreignKey: 'visibility', as: 'user_param_visibilities' });
// Folder and Image related associations
Folder.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Folder, { foreignKey: 'userId' });
@@ -62,8 +71,9 @@ export default function setupAssociations() {
Folder.hasMany(Image, { foreignKey: 'folderId' });
Image.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Image, { foreignKey: 'userId' });
User.hasMany(Image, { foreignKey: 'userId' });
// Image visibility associations
Folder.belongsToMany(ImageVisibilityType, {
through: FolderImageVisibility,
foreignKey: 'folderId',
@@ -97,24 +107,53 @@ export default function setupAssociations() {
otherKey: 'folderId'
});
User.hasMany(GuestbookEntry, {
foreignKey: 'recipientId',
as: 'receivedEntries'
// Guestbook related associations
User.hasMany(GuestbookEntry, { foreignKey: 'recipientId', as: 'receivedEntries' });
User.hasMany(GuestbookEntry, { foreignKey: 'senderId', as: 'sentEntries' });
GuestbookEntry.belongsTo(User, { foreignKey: 'recipientId', as: 'recipient' });
GuestbookEntry.belongsTo(User, { foreignKey: 'senderId', as: 'sender' });
// Forum related associations
Forum.hasMany(Title, { foreignKey: 'forumId' });
Title.belongsTo(Forum, { foreignKey: 'forumId' });
Title.belongsTo(User, { foreignKey: 'createdBy', as: 'createdByUser' });
User.hasMany(Title, { foreignKey: 'createdBy', as: 'titles' });
Title.hasMany(Message, { foreignKey: 'titleId', as: 'messages' });
Message.belongsTo(Title, { foreignKey: 'titleId', as: 'title' });
Message.belongsTo(User, { foreignKey: 'createdBy', as: 'lastMessageUser' });
User.hasMany(Message, { foreignKey: 'createdBy', as: 'userMessages' });
Message.hasMany(MessageImage, { foreignKey: 'messageId' });
MessageImage.belongsTo(Message, { foreignKey: 'messageId' });
Message.hasMany(MessageHistory, { foreignKey: 'messageId' });
MessageHistory.belongsTo(Message, { foreignKey: 'messageId' });
Title.hasMany(TitleHistory, { foreignKey: 'titleId' });
TitleHistory.belongsTo(Title, { foreignKey: 'titleId' });
// Forum permissions associations
Forum.hasMany(ForumUserPermission, { foreignKey: 'forumId', as: 'userPermissions' });
ForumUserPermission.belongsTo(Forum, { foreignKey: 'forumId' });
User.hasMany(ForumUserPermission, { foreignKey: 'userId', as: 'userPermissions' });
ForumUserPermission.belongsTo(User, { foreignKey: 'userId' });
Forum.belongsToMany(ForumPermission, {
through: ForumForumPermission,
foreignKey: 'forumId',
as: 'associatedPermissions'
});
User.hasMany(GuestbookEntry, {
foreignKey: 'senderId',
as: 'sentEntries'
});
GuestbookEntry.belongsTo(User, {
foreignKey: 'recipientId',
as: 'recipient'
});
GuestbookEntry.belongsTo(User, {
foreignKey: 'senderId',
as: 'sender'
ForumPermission.belongsToMany(Forum, {
through: ForumForumPermission,
foreignKey: 'permissionId',
as: 'forums'
});
ForumPermission.hasMany(ForumUserPermission, { foreignKey: 'permissionId' });
ForumUserPermission.belongsTo(ForumPermission, { foreignKey: 'permissionId' });
}

View File

@@ -26,6 +26,7 @@ Diary.init({
tableName: 'diary',
schema: 'community',
timestamps: true,
underscored: true,
});
export default Diary;

View File

@@ -15,7 +15,7 @@ DiaryHistory.init({
oldText: {
type: DataTypes.TEXT,
allowNull: false,
},
},
oldCreatedAt: {
type: DataTypes.DATE,
allowNull: false,
@@ -30,6 +30,7 @@ DiaryHistory.init({
tableName: 'diary_history',
schema: 'community',
timestamps: false,
underscored: true,
});
export default DiaryHistory;

View File

@@ -0,0 +1,15 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const Forum = sequelize.define('forum', {
name: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'forum',
schema: 'forum',
underscored: true
});
export default Forum;

View File

@@ -0,0 +1,27 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const ForumForumPermission = sequelize.define('forum_forum_permission', {
forumId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'forum',
key: 'id'
}
},
permissionId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'forum_permission',
key: 'id'
}
}
}, {
tableName: 'forum_forum_permission',
schema: 'forum',
underscored: true
});
export default ForumForumPermission;

View File

@@ -0,0 +1,22 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const ForumPermission = sequelize.define('forum_permission', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
value: {
type: DataTypes.STRING,
allowNull: true,
},
}, {
sequelize,
modelName: 'ForumPermission',
tableName: 'forum_permission',
schema: 'forum',
timestamps: false,
underscored: true
});
export default ForumPermission;

View File

@@ -0,0 +1,23 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const ForumUserPermission = sequelize.define('forum_user_permission', {
userId: {
type: DataTypes.INTEGER,
allowNull: true
},
permissionId: {
type: DataTypes.INTEGER,
allowNull: false
},
forumId: {
type: DataTypes.INTEGER,
allowNull: false
},
}, {
tableName: 'forum_user_permission',
schema: 'forum',
underscored: true
});
export default ForumUserPermission;

View File

@@ -0,0 +1,24 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const Message = sequelize.define('message', {
text: {
type: DataTypes.TEXT,
allowNull: false
},
createdBy: {
type: DataTypes.INTEGER,
allowNull: false
},
titleId: {
type: DataTypes.INTEGER,
allowNull: false
}
}, {
tableName: 'message',
schema: 'forum',
underscored: true,
timestamps: true
});
export default Message;

View File

@@ -0,0 +1,28 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const MessageHistory = sequelize.define('message_history', {
messageId: {
type: DataTypes.INTEGER,
allowNull: false
},
oldText: {
type: DataTypes.TEXT,
allowNull: false
},
changedBy: {
type: DataTypes.INTEGER,
allowNull: false
},
oldUpdatedAt: {
type: DataTypes.DATE,
allowNull: false
}
}, {
tableName: 'message_history',
schema: 'forum',
underscored: true,
timestamps: false
});
export default MessageHistory;

View File

@@ -0,0 +1,20 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const MessageImage = sequelize.define('message_image', {
messageId: {
type: DataTypes.INTEGER,
allowNull: false
},
fileName: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'message_image',
schema: 'forum',
underscored: true,
timestamps: false
});
export default MessageImage;

View File

@@ -0,0 +1,24 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const Title = sequelize.define('title', {
title: {
type: DataTypes.STRING,
allowNull: false
},
createdBy: {
type: DataTypes.INTEGER,
allowNull: false
},
forumId: {
type: DataTypes.INTEGER,
allowNull: false
}
}, {
tableName: 'title',
schema: 'forum',
underscored: true,
timestamps: true,
});
export default Title;

View File

@@ -0,0 +1,28 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
const TitleHistory = sequelize.define('title_history', {
titleId: {
type: DataTypes.INTEGER,
allowNull: false
},
oldTitle: {
type: DataTypes.STRING,
allowNull: false
},
changedBy: {
type: DataTypes.INTEGER,
allowNull: false
},
oldUpdatedAt: {
type: DataTypes.DATE,
allowNull: false
}
}, {
tableName: 'title_history',
schema: 'forum',
underscored: true,
timestamps: false
});
export default TitleHistory;

View File

@@ -22,6 +22,15 @@ import FolderVisibilityUser from './community/folder_visibility_user.js';
import GuestbookEntry from './community/guestbook.js';
import DiaryHistory from './community/diary_history.js';
import Diary from './community/diary.js';
import Forum from './forum/forum.js';
import ForumPermission from './forum/forum_permission.js';
import ForumUserPermission from './forum/forum_user_permission.js';
import Title from './forum/title.js';
import TitleHistory from './forum/title_history.js';
import Message from './forum/message.js';
import MessageHistory from './forum/message_history.js';
import MessageImage from './forum/message_image.js';
import ForumForumPermission from './forum/forum_forum_permission.js';
const models = {
SettingsType,
@@ -48,6 +57,15 @@ const models = {
GuestbookEntry,
DiaryHistory,
Diary,
Forum,
ForumPermission,
ForumForumPermission,
ForumUserPermission,
Title,
TitleHistory,
Message,
MessageHistory,
MessageImage,
};
export default models;

View File

@@ -16,6 +16,12 @@ export async function createTriggers() {
SELECT id FROM type.user_param_visibility WHERE description = 'Invisible'
));
END IF;
-- If NEW is null, then we have nothing to do
IF NEW IS NULL THEN
RETURN NULL;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
@@ -25,6 +31,7 @@ export async function createTriggers() {
CREATE OR REPLACE TRIGGER trigger_create_user_param_visibility
AFTER INSERT ON community.user_param
FOR EACH ROW
WHEN (NEW.id IS NOT NULL)
EXECUTE FUNCTION create_user_param_visibility_trigger();
`;
@@ -32,6 +39,7 @@ export async function createTriggers() {
CREATE OR REPLACE TRIGGER trigger_update_user_param_visibility
AFTER UPDATE ON community.user_param
FOR EACH ROW
WHEN (NEW.id IS NOT NULL)
EXECUTE FUNCTION create_user_param_visibility_trigger();
`;
@@ -39,8 +47,14 @@ export async function createTriggers() {
CREATE OR REPLACE FUNCTION insert_diary_history()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO community.diary_history (diaryId, userId, oldText, oldCreatedAt, oldUpdatedAt)
VALUES (OLD.id, OLD.userId, OLD.text, OLD.createdAt, OLD.updatedAt);
INSERT INTO community.diary_history (diary_id, user_id, old_text, old_created_at, old_updated_at)
VALUES (OLD.id, OLD.user_id, OLD.text, OLD.created_at, OLD.updated_at);
-- If NEW is null, then we have nothing to do
IF NEW IS NULL THEN
RETURN NULL;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
@@ -50,18 +64,41 @@ export async function createTriggers() {
CREATE OR REPLACE TRIGGER diary_update_trigger
BEFORE UPDATE ON community.diary
FOR EACH ROW
WHEN (OLD.id IS NOT NULL)
EXECUTE FUNCTION insert_diary_history();
`;
const createTitleHistoryTriggerFunction = `
CREATE OR REPLACE FUNCTION insert_title_history()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO forum.title_history (title_id, old_title, changed_by, old_updated_at)
VALUES (OLD.id, OLD.title, OLD.created_by, OLD.updated_at);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
`;
const createTitleHistoryTrigger = `
CREATE OR REPLACE TRIGGER title_update_trigger
BEFORE UPDATE ON forum.title
FOR EACH ROW
WHEN (OLD.id IS NOT NULL)
EXECUTE FUNCTION insert_title_history();
`;
try {
await sequelize.query(createTriggerFunction);
await sequelize.query(createInsertTrigger);
await sequelize.query(createUpdateTrigger);
await sequelize.query(createDiaryHistoryTriggerFunction);
await sequelize.query(createDiaryHistoryTrigger);
await sequelize.query(createTitleHistoryTriggerFunction);
await sequelize.query(createTitleHistoryTrigger);
console.log('Triggers created successfully');
} catch (error) {
console.error('Error creating triggers:', error);
}
}