Files
SerpentRace/SerpentRace_Backend/dist/Application/Search/Generalsearch.js
T

114 lines
4.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeneralSearchService = void 0;
const UserMapper_1 = require("../DTOs/Mappers/UserMapper");
const OrganizationMapper_1 = require("../DTOs/Mappers/OrganizationMapper");
const DeckMapper_1 = require("../DTOs/Mappers/DeckMapper");
class GeneralSearchService {
constructor(userRepo, organizationRepo, deckRepo) {
this.userRepo = userRepo;
this.organizationRepo = organizationRepo;
this.deckRepo = deckRepo;
}
static getSearchTypeFromUrl(url) {
if (url.includes('/users/') || url.includes('/api/users/')) {
return 'users';
}
else if (url.includes('/organizations/') || url.includes('/api/organizations/')) {
return 'organizations';
}
else if (url.includes('/decks/') || url.includes('/api/decks/')) {
return 'decks';
}
return 'users';
}
async searchUsers(searchQuery) {
const { query, limit = 20, offset = 0 } = searchQuery;
if (!query || query.trim().length === 0) {
return {
results: [],
totalCount: 0,
hasMore: false,
searchQuery: query,
searchType: 'users'
};
}
try {
const { users, totalCount } = await this.userRepo.search(query.trim(), limit, offset);
const results = users.map(user => UserMapper_1.UserMapper.toShortDto(user));
const hasMore = (offset + limit) < totalCount;
return {
results,
totalCount,
hasMore,
searchQuery: query,
searchType: 'users'
};
}
catch (error) {
throw new Error('Failed to search users');
}
}
async searchOrganizations(searchQuery) {
const { query, limit = 20, offset = 0 } = searchQuery;
if (!query || query.trim().length === 0) {
return {
results: [],
totalCount: 0,
hasMore: false,
searchQuery: query,
searchType: 'organizations'
};
}
const { organizations, totalCount } = await this.organizationRepo.search(query.trim(), limit, offset);
const results = organizations.map(org => OrganizationMapper_1.OrganizationMapper.toShortDto(org));
const hasMore = (offset + limit) < totalCount;
return {
results,
totalCount,
hasMore,
searchQuery: query,
searchType: 'organizations'
};
}
async searchDecks(searchQuery) {
const { query, limit = 20, offset = 0 } = searchQuery;
if (!query || query.trim().length === 0) {
return {
results: [],
totalCount: 0,
hasMore: false,
searchQuery: query,
searchType: 'decks'
};
}
const { decks, totalCount } = await this.deckRepo.search(query.trim(), limit, offset);
const results = decks.map(deck => DeckMapper_1.DeckMapper.toShortDto(deck));
const hasMore = (offset + limit) < totalCount;
return {
results,
totalCount,
hasMore,
searchQuery: query,
searchType: 'decks'
};
}
async searchByType(searchType, searchQuery) {
switch (searchType) {
case 'users':
return await this.searchUsers(searchQuery);
case 'organizations':
return await this.searchOrganizations(searchQuery);
case 'decks':
return await this.searchDecks(searchQuery);
default:
throw new Error(`Unsupported search type: ${searchType}`);
}
}
async searchFromUrl(url, searchQuery) {
const searchType = GeneralSearchService.getSearchTypeFromUrl(url);
return await this.searchByType(searchType, searchQuery);
}
}
exports.GeneralSearchService = GeneralSearchService;
//# sourceMappingURL=Generalsearch.js.map