|
| 1 | +import { Api } from '@gbfm/api/api' |
| 2 | +import { AuthSession } from '@gbfm/api/middleware/auth' |
| 3 | +import { Effect } from 'effect' |
| 4 | +import { HttpApiBuilder, HttpApiError } from 'effect/unstable/httpapi' |
| 5 | +import { dieOnDatabaseError as makeDieOnDatabaseError } from '@/http/handler-utils' |
| 6 | +import { FavoriteService } from '@/services/favorite.service' |
| 7 | + |
| 8 | +const dieOnDatabaseError = makeDieOnDatabaseError('favorites') |
| 9 | + |
| 10 | +const toFavoriteResponse = (favorite: { |
| 11 | + id: string |
| 12 | + userId: string |
| 13 | + audioId: string | null |
| 14 | + showId: string | null |
| 15 | + createdAt: Date |
| 16 | + audio: { |
| 17 | + id: string |
| 18 | + title: string |
| 19 | + slug: string |
| 20 | + thumbnailUrl: string | null |
| 21 | + type: 'mix' | 'track' | 'misc' |
| 22 | + url: string |
| 23 | + } | null |
| 24 | + show: { id: string; title: string; slug: string; thumbnailUrl: string | null } | null |
| 25 | +}) => ({ |
| 26 | + ...favorite, |
| 27 | + createdAt: favorite.createdAt.toISOString() |
| 28 | +}) |
| 29 | + |
| 30 | +export const FavoritesHandlersLive = HttpApiBuilder.group(Api, 'favorites', (handlers) => |
| 31 | + handlers |
| 32 | + .handle('addFavorite', ({ payload }) => |
| 33 | + Effect.gen(function* () { |
| 34 | + const { user } = yield* AuthSession |
| 35 | + const svc = yield* FavoriteService |
| 36 | + |
| 37 | + if (payload.audioId) { |
| 38 | + yield* dieOnDatabaseError( |
| 39 | + svc.addFavorite(user.id, payload.audioId).pipe( |
| 40 | + Effect.catchTag('NotFoundError', () => new HttpApiError.NotFound()), |
| 41 | + Effect.catchTag('ConflictError', () => new HttpApiError.Conflict()) |
| 42 | + ) |
| 43 | + ) |
| 44 | + } else if (payload.showId) { |
| 45 | + yield* dieOnDatabaseError( |
| 46 | + svc.addShowFavorite(user.id, payload.showId).pipe( |
| 47 | + Effect.catchTag('NotFoundError', () => new HttpApiError.NotFound()), |
| 48 | + Effect.catchTag('ConflictError', () => new HttpApiError.Conflict()) |
| 49 | + ) |
| 50 | + ) |
| 51 | + } else { |
| 52 | + return yield* new HttpApiError.BadRequest() |
| 53 | + } |
| 54 | + |
| 55 | + return { success: true, message: 'Added to favorites' } |
| 56 | + }) |
| 57 | + ) |
| 58 | + .handle('removeFavorite', ({ params }) => |
| 59 | + Effect.gen(function* () { |
| 60 | + const { user } = yield* AuthSession |
| 61 | + const svc = yield* FavoriteService |
| 62 | + |
| 63 | + yield* dieOnDatabaseError( |
| 64 | + svc |
| 65 | + .removeFavorite(user.id, params.audioId) |
| 66 | + .pipe(Effect.catchTag('NotFoundError', () => new HttpApiError.NotFound())) |
| 67 | + ) |
| 68 | + |
| 69 | + return { success: true, message: 'Removed from favorites' } |
| 70 | + }) |
| 71 | + ) |
| 72 | + .handle('removeShowFavorite', ({ params }) => |
| 73 | + Effect.gen(function* () { |
| 74 | + const { user } = yield* AuthSession |
| 75 | + const svc = yield* FavoriteService |
| 76 | + |
| 77 | + yield* dieOnDatabaseError( |
| 78 | + svc |
| 79 | + .removeShowFavorite(user.id, params.showId) |
| 80 | + .pipe(Effect.catchTag('NotFoundError', () => new HttpApiError.NotFound())) |
| 81 | + ) |
| 82 | + |
| 83 | + return { success: true, message: 'Removed from favorites' } |
| 84 | + }) |
| 85 | + ) |
| 86 | + .handle('getFavorites', ({ query }) => |
| 87 | + Effect.gen(function* () { |
| 88 | + const { user } = yield* AuthSession |
| 89 | + const svc = yield* FavoriteService |
| 90 | + |
| 91 | + const favorites = yield* dieOnDatabaseError( |
| 92 | + svc.getFavorites(user.id, query.limit, query.offset) |
| 93 | + ) |
| 94 | + |
| 95 | + return { |
| 96 | + success: true, |
| 97 | + favorites: favorites.map(toFavoriteResponse), |
| 98 | + total: favorites.length |
| 99 | + } |
| 100 | + }) |
| 101 | + ) |
| 102 | +) |
0 commit comments