want events
This commit is contained in:
1 parent
f5e75f4fe6
commit
30bf7426e4
19 files changed
+293
-108
No files matched your search
+29
-11
@@ -14,8 +14,9 @@ import type { Listing, User, Want, Prisma } from '@prisma/client';
|
||||
|
||||
import { Pg } from './pg';
|
||||
import routerAuth from './routers/router.auth';
|
||||
import { Events } from './events';
|
||||
|
||||
export default function createApi(log: Log, pg: Pg, discogs) {
|
||||
export default function createApi(log: Log, pg: Pg, discogs, events) {
|
||||
const PORT = 41337;
|
||||
const SECRET = 'ohja4Eif9eidah3aj5ta2haa4iemooth';
|
||||
|
||||
@@ -147,23 +148,40 @@ export default function createApi(log: Log, pg: Pg, discogs) {
|
||||
ctx.response.body = listings;
|
||||
});
|
||||
|
||||
router.get('/wants', async function wantPost(ctx, next) {
|
||||
const { userId, releaseId } = ctx.request.body;
|
||||
const user = await pg.userFindId(userId);
|
||||
router.post('/want', async function wantPost(ctx, next) {
|
||||
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
|
||||
const { releaseId } = ctx.request.body;
|
||||
const user = await pg.userFindId(ctx.state.user.id);
|
||||
const release = await discogs.release(releaseId);
|
||||
const want = await pg.wantUpsert(release, user);
|
||||
ctx.response.body = want;
|
||||
const want = await pg.wantCreate([release], user);
|
||||
|
||||
// fetch listings
|
||||
const listings = await discogs.listings(want);
|
||||
|
||||
ctx.response.body = { want, listings };
|
||||
});
|
||||
|
||||
router.get('/wants', async function wantGet(ctx, next) {
|
||||
router.post('/want/discogs', async function wantListPost(ctx, next) {
|
||||
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
|
||||
log.warn(ctx.state.user);
|
||||
const user = ctx.state.user;
|
||||
const wants = await pg.wantFind(user);
|
||||
const { username } = ctx.request.body;
|
||||
const user = await pg.userFindId(ctx.state.user.id);
|
||||
|
||||
const wants = await discogs.wantListSync(username, user);
|
||||
|
||||
events.emit(Events.WantsAdded, wants);
|
||||
|
||||
ctx.response.body = wants;
|
||||
});
|
||||
|
||||
router.post('/wants/search', async function wantPost(ctx, next) {
|
||||
router.get('/want/all', async function wantGet(ctx, next) {
|
||||
ctx.assert(ctx.isAuthenticated(), 401, 'log in required');
|
||||
const user = ctx.state.user;
|
||||
const wants = await pg.wantList(user);
|
||||
|
||||
ctx.response.body = wants;
|
||||
});
|
||||
|
||||
router.post('/want/search', async function wantPost(ctx, next) {
|
||||
const { artist, title } = ctx.request.body;
|
||||
const results = await discogs.search(artist, title);
|
||||
ctx.response.body = results;
|
||||
|
||||
Reference in new issue
Block a user