first initialization gallery

This commit is contained in:
Torsten Schulz
2024-09-22 01:26:59 +02:00
parent f1b6dd74f7
commit 7ab6939863
23 changed files with 792 additions and 34 deletions

View File

@@ -8,6 +8,8 @@ import UserParamVisibility from '../models/community/user_param_visibility.js';
import UserParamVisibilityType from '../models/type/user_param_visibility.js';
import Folder from '../models/community/folder.js';
import Image from '../models/community/image.js';
import ImageVisibilityType from '../models/type/image_visibility.js';
import FolderImageVisibility from '../models/community/folder_image_visibility.js';
class SocialNetworkService extends BaseService {
async searchUsers({ username, ageFrom, ageTo, genders }) {
@@ -23,20 +25,84 @@ class SocialNetworkService extends BaseService {
return this.constructUserProfile(user, requestingUserId);
}
async createFolder(data) {
this.validateFolderData(data);
await this.checkUserAccess(data.userId);
return await Folder.create(data);
async createFolder(hashedUserId, data) {
await this.checkUserAccess(hashedUserId);
const user = await User.findOne({
hashedId: hashedUserId
});
const parentFolder = Folder.findOne({
id: data.parentId,
userId: user.id
});
if (!parentFolder) {
throw new Error('foldernotfound');
}
const newFolder = await Folder.create({
parentId: data.parentId,
userId: user.id,
name: data.name
});
for (const visibilityId of data.visibilities) {
await FolderImageVisibility.create({
folderId: newFolder.id,
visibilityTypeId: visibilityId
});
}
return newFolder;
}
async getFolders(userId) {
await this.checkUserAccess(userId);
return await Folder.findAll({ where: { userId } });
async getFolders(hashedId) {
const userId = await this.checkUserAccess(hashedId);
let rootFolder = await Folder.findOne({ where: { parentId: null, userId } });
if (!rootFolder) {
const user = await User.findOne({ where: { id: userId } });
const visibility = await ImageVisibilityType.findOne({
where: {
description: 'everyone'
}
});
rootFolder = await Folder.create({
name: user.username,
parentId: null,
userId,
visibilityTypeId: visibility.id
});
}
const children = await this.getSubFolders(rootFolder.id, userId);
rootFolder = rootFolder.get();
rootFolder.children = children;
return rootFolder;
}
async uploadImage(imageData) {
async getSubFolders(parentId, userId) {
const folders = await Folder.findAll({ where: { parentId, userId } });
for (const folder of folders) {
const children = await this.getSubFolders(folder.id, userId);
folder.setDataValue('children', children);
}
return folders.map(folder => folder.get());
}
async getFolderImageList(hashedId, folderId) {
const userId = await this.checkUserAccess(hashedId);
const folder = await Folder.findOne({
where: {
id: folderId,
userId
}
});
if (!folder) throw new Error('Folder not found');
return await Image.findAll({
where: {
folderId: folder.id
}
});
}
async uploadImage(hashedId, imageData) {
this.validateImageData(imageData);
await this.checkUserAccess(imageData.userId);
const userId = await this.checkUserAccess(hashedId);
imageData.id = userId;
return await Image.create(imageData);
}
@@ -47,9 +113,10 @@ class SocialNetworkService extends BaseService {
return image;
}
async checkUserAccess(userId) {
const user = await User.findByPk(userId);
async checkUserAccess(hashedId) {
const user = await User.findOne({ hashedId: hashedId });
if (!user || !user.active) throw new Error('Access denied: User not found or inactive');
return user.id;
}
validateFolderData(data) {
@@ -184,6 +251,10 @@ class SocialNetworkService extends BaseService {
});
return userParamValue ? userParamValue.value : value;
}
async getPossibleImageVisibilities() {
return await ImageVisibilityType.findAll();
}
}
export default SocialNetworkService;