Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pages/api/v2/clubs/[uuid]/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { ClubService } from 'server/service/club.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { z, ZodIssue } from 'zod'
import { ClubUuidParamsSchema } from 'src/lib/schemas/clubs'
import { NotFoundError } from 'server/domain/error'
import { BadRequestError, NotFoundError, UnauthorizedError } from 'server/domain/error'
import { resolveOptionalAuth } from 'server/util/optional-auth'

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Club | string | ZodIssue[]>,
) {
try {
const clubService = Provider.getService(ClubService)
const userService = Provider.getService(UserService)
if (req.method == 'GET') {
const { uuid: ClubUuid } = ClubUuidParamsSchema.parse(req.query)
const club = await clubService.findPublicByUuid(ClubUuid)
const auth = await resolveOptionalAuth(req)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const club = await clubService.findPublicByUuid(ClubUuid, serviceUserId)
return res.status(200).json(club)
}
} catch (err) {
if (err instanceof NotFoundError) {
return res.status(404).send('club not found')
}
if (err instanceof UnauthorizedError) {
return res.status(401).send('unauthorized')
}
if (err instanceof BadRequestError) {
return res.status(400).send(err.message)
}
if (err instanceof z.ZodError) {
return res.status(400).json(err.errors)
}
Expand Down
20 changes: 19 additions & 1 deletion pages/api/v2/clubs/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { ClubService } from 'server/service/club.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { resolveOptionalAuth } from 'server/util/optional-auth'
import { BadRequestError, UnauthorizedError } from 'server/domain/error'

type ResponseData = {
clubs: Club[]
Expand All @@ -14,19 +17,34 @@ export default async function handler(
) {
try {
const clubService = Provider.getService(ClubService)
const userService = Provider.getService(UserService)

if (req.method == 'GET') {
const category = req.query.category as string
if (!category) {
return res.status(400).send('category is required')
}
const clubs = await clubService.findByCategory(category)
const auth = await resolveOptionalAuth(req)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const clubs = await clubService.findByCategory(category, serviceUserId)
return res.status(200).json({
clubs: clubs,
totalSize: clubs.length,
})
}
} catch (err) {
if (err instanceof UnauthorizedError) {
return res.status(401).send('unauthorized')
}
if (err instanceof BadRequestError) {
return res.status(400).send(err.message)
}
console.error('listClubs error: ', err)
return res.status(500).send('Internal Server Error')
}
Expand Down
20 changes: 19 additions & 1 deletion pages/api/v2/clubs/latest/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { ClubService } from 'server/service/club.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { resolveOptionalAuth } from 'server/util/optional-auth'
import { BadRequestError, UnauthorizedError } from 'server/domain/error'

type ResponseData = {
clubs: Club[]
Expand All @@ -14,15 +17,30 @@ export default async function handler(
) {
try {
const clubService = Provider.getService(ClubService)
const userService = Provider.getService(UserService)

if (req.method == 'GET') {
const clubs = await clubService.findLatestUploaded()
const auth = await resolveOptionalAuth(req)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const clubs = await clubService.findLatestUploaded(undefined, serviceUserId)
return res.status(200).json({
clubs: clubs,
totalSize: clubs.length,
})
}
} catch (err) {
if (err instanceof UnauthorizedError) {
return res.status(401).send('unauthorized')
}
if (err instanceof BadRequestError) {
return res.status(400).send(err.message)
}
console.error('listLatestClubs error: ', err)
return res.status(500).send('Internal Server Error')
}
Expand Down
20 changes: 19 additions & 1 deletion pages/api/v2/clubs/popular/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { ClubService } from 'server/service/club.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { resolveOptionalAuth } from 'server/util/optional-auth'
import { BadRequestError, UnauthorizedError } from 'server/domain/error'

type ResponseData = {
clubs: Club[]
Expand All @@ -14,15 +17,30 @@ export default async function handler(
) {
try {
const clubService = Provider.getService(ClubService)
const userService = Provider.getService(UserService)

if (req.method == 'GET') {
const clubs = await clubService.findPopular()
const auth = await resolveOptionalAuth(req)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const clubs = await clubService.findPopular(serviceUserId)
return res.status(200).json({
clubs: clubs,
totalSize: clubs.length,
})
}
} catch (err) {
if (err instanceof UnauthorizedError) {
return res.status(401).send('unauthorized')
}
if (err instanceof BadRequestError) {
return res.status(400).send(err.message)
}
console.error('listPopularClubs error: ', err)
return res.status(500).send('Internal Server Error')
}
Expand Down
20 changes: 19 additions & 1 deletion pages/api/v2/clubs/recommendations/random/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { ClubService } from 'server/service/club.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { resolveOptionalAuth } from 'server/util/optional-auth'
import { BadRequestError, UnauthorizedError } from 'server/domain/error'

type ResponseData = {
clubs: Club[]
Expand All @@ -14,15 +17,30 @@ export default async function handler(
) {
try {
const clubService = Provider.getService(ClubService)
const userService = Provider.getService(UserService)

if (req.method == 'GET') {
const clubs = await clubService.findRandomRecommendations(5)
const auth = await resolveOptionalAuth(req)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const clubs = await clubService.findRandomRecommendations(5, serviceUserId)
return res.status(200).json({
clubs: clubs,
totalSize: clubs.length,
})
}
} catch (err) {
if (err instanceof UnauthorizedError) {
return res.status(401).send('unauthorized')
}
if (err instanceof BadRequestError) {
return res.status(400).send(err.message)
}
console.error('listRandomRecommendedClubs error: ', err)
return res.status(500).send('Internal Server Error')
}
Expand Down
11 changes: 10 additions & 1 deletion pages/api/v2/clubs/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Provider } from 'server/provider'
import { SearchService } from 'server/service/search.service'
import { UserService } from 'server/service/user.service'
import { Club } from 'server/domain/model/Club'
import { MinActivityPeriodFilter, SearchFilters } from 'server/service/search/search.types'
import { BadRequestError, UnauthorizedError } from 'server/domain/error'
Expand Down Expand Up @@ -30,6 +31,14 @@ export default async function handler(

if (req.method == 'GET') {
const auth = await resolveOptionalAuth(req)
const userService = Provider.getService(UserService)
const serviceUserId =
auth.type === 'member'
? await userService
.getUserByAccountId(auth.accountId)
.then((u) => u.serviceUserId)
.catch(() => null)
: null
const query = req.query.query as string
if (!query) {
return res.status(400).send('query is required')
Expand All @@ -46,7 +55,7 @@ export default async function handler(
}

const { clubs, correctedQuery, isTypoCorrected } =
await searchService.searchWithTypoCorrection(query, { filters })
await searchService.searchWithTypoCorrection(query, { filters }, serviceUserId)
await saveRecentSearchBestEffort(auth, query)
return res.status(200).json({
clubs: clubs,
Expand Down
3 changes: 3 additions & 0 deletions server/domain/model/Club.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type Club = {
totalReviews: number
reviewKeywords: ReviewKeyword[]
latestComment: string
isSaved: boolean
}

export const toClubDomain = (
Expand All @@ -70,6 +71,7 @@ export const toClubDomain = (
reviewKeywords: ReviewKeyword[]
latestComment: string
},
isSaved?: boolean,
): Club => ({
id: it.uuid,
uuid: it.uuid,
Expand Down Expand Up @@ -112,6 +114,7 @@ export const toClubDomain = (
totalReviews: review?.totalReviews ?? 0,
reviewKeywords: review?.reviewKeywords ?? [],
latestComment: review?.latestComment ?? '',
isSaved: isSaved ?? false,
})

function encode(imageUri: string | undefined): string {
Expand Down
Loading
Loading