generated from include-davis/Next.js-App-Router-Starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* events crud * with auth * event crud changes * _types event & return body update --------- Co-authored-by: Anjali Jay Jain <116294517+JayJ104@users.noreply.github.com>
- Loading branch information
1 parent
a589602
commit b996e1b
Showing
12 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
import parseAndReplace from '@utils/request/parseAndReplace'; | ||
import { | ||
DuplicateError, | ||
HttpError, | ||
NoContentError, | ||
} from '@utils/response/Errors'; | ||
import isBodyEmpty from '@utils/request/isBodyEmpty'; | ||
|
||
export async function createEvent(body: object) { | ||
try { | ||
if (isBodyEmpty(body)) { | ||
throw new NoContentError(); | ||
} | ||
const parsedBody = await parseAndReplace(body); | ||
const db = await getDatabase(); | ||
|
||
//duplicate event | ||
const existingEvent = await db.collection('events').findOne({ | ||
name: parsedBody.name, | ||
}); | ||
if (existingEvent) { | ||
throw new DuplicateError('Duplicate: event already exists.'); | ||
} | ||
|
||
const creationStatus = await db.collection('events').insertOne(parsedBody); | ||
|
||
const createdEvent = await db.collection('events').findOne({ | ||
_id: creationStatus.insertedId, | ||
}); | ||
|
||
if (!createdEvent) { | ||
throw new HttpError('Failed to fetch the created item'); | ||
} | ||
|
||
return { ok: true, body: createdEvent, error: null }; | ||
} catch (e) { | ||
const error = e as HttpError; | ||
return { | ||
ok: false, | ||
body: null, | ||
error: error.message || 'Internal Server Error', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
import { HttpError, NotFoundError } from '@utils/response/Errors'; | ||
import { ObjectId } from 'mongodb'; | ||
|
||
export async function deleteEvent(id: string) { | ||
try { | ||
const db = await getDatabase(); | ||
const objectId = ObjectId.createFromHexString(id); | ||
|
||
const deletion = await db.collection('events').deleteOne({ | ||
_id: objectId, | ||
}); | ||
|
||
if (deletion.deletedCount === 0) { | ||
throw new NotFoundError( | ||
`Could not delete event with ID: '${id}'. Event does not exist or ID is incorrect.` | ||
); | ||
} | ||
|
||
return { | ||
ok: true, | ||
body: 'Event deleted.', | ||
error: null, | ||
}; | ||
} catch (e) { | ||
const error = e as HttpError; | ||
return { | ||
ok: false, | ||
body: null, | ||
error: error.message || 'Internal Server Error', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
import { HttpError, NotFoundError } from '@utils/response/Errors'; | ||
import { ObjectId } from 'mongodb'; | ||
|
||
/** | ||
* Retrieves an event from the database by id | ||
* @param query - ID of Event | ||
* @returns: { | ||
* ok: boolean, | ||
* body: object | null, | ||
* error: number | null | ||
* } | ||
*/ | ||
export async function getEvent(id: string) { | ||
try { | ||
const db = await getDatabase(); | ||
const objectId = ObjectId.createFromHexString(id); | ||
const event = await db.collection('events').findOne({ _id: objectId }); | ||
|
||
if (!event) { | ||
throw new NotFoundError(`Event with id: ${id} not found.`); | ||
} | ||
|
||
return { | ||
ok: true, | ||
body: event, | ||
error: null, | ||
}; | ||
} catch (e) { | ||
const error = e as HttpError; | ||
return { | ||
ok: false, | ||
body: null, | ||
error: error.message, | ||
}; | ||
} | ||
} | ||
|
||
export async function getEvents(query: object) { | ||
try { | ||
const db = await getDatabase(); | ||
const events = await db.collection('events').find(query).toArray(); | ||
|
||
return { | ||
ok: true, | ||
body: events, | ||
error: null, | ||
}; | ||
} catch (e) { | ||
const error = e as HttpError; | ||
return { | ||
ok: false, | ||
body: null, | ||
error: error.message || 'Internal Server Error', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
import { ObjectId } from 'mongodb'; | ||
import isBodyEmpty from '@utils/request/isBodyEmpty'; | ||
import parseAndReplace from '@utils/request/parseAndReplace'; | ||
import { | ||
HttpError, | ||
NoContentError, | ||
NotFoundError, | ||
} from '@utils/response/Errors'; | ||
|
||
export async function updateEvent(id: string, body: object) { | ||
try { | ||
if (isBodyEmpty(body)) { | ||
throw new NoContentError(); | ||
} | ||
|
||
const db = await getDatabase(); | ||
const objectId = ObjectId.createFromHexString(id); | ||
const parsedBody = await parseAndReplace(body); | ||
|
||
const event = await db | ||
.collection('users') | ||
.updateOne({ _id: objectId }, parsedBody); | ||
|
||
if (event.matchedCount === 0) { | ||
throw new NotFoundError( | ||
`Could not update event with ID: '${id}'. Event does not exist or ID is incorrect.` | ||
); | ||
} | ||
|
||
return { | ||
ok: true, | ||
body: event, | ||
error: null, | ||
}; | ||
} catch (e) { | ||
const error = e as HttpError; | ||
return { | ||
ok: false, | ||
body: null, | ||
error: error.message || 'Internal Server Error', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { deleteEvent } from '@datalib/events/deleteEvent'; | ||
|
||
export async function DELETE( | ||
_: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
const res = await deleteEvent(params.id); | ||
return NextResponse.json({ ...res }, { status: res.ok ? 200 : 500 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { getEvent } from '@datalib/events/getEvent'; | ||
|
||
export async function GET( | ||
_: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
const res = await getEvent(params.id); | ||
return NextResponse.json({ ...res }, { status: res.ok ? 200 : 500 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { updateEvent } from '@datalib/events/updateEvent'; | ||
|
||
export async function PUT( | ||
request: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
const body = await request.json(); | ||
const res = await updateEvent(params.id, body); | ||
return NextResponse.json({ ...res }, { status: res.ok ? 200 : 500 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import authenticated from '@utils/authentication/authenticated'; | ||
import { PUT as put } from './put'; | ||
import { GET as get } from './get'; | ||
import { DELETE as del } from './delete'; | ||
|
||
const PUT = authenticated(put); | ||
const GET = authenticated(get); | ||
const DELETE = authenticated(del); | ||
|
||
export { PUT, GET, DELETE }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { getEvents } from '@datalib/events/getEvent'; | ||
import getQueries from '@utils/request/getQueries'; | ||
|
||
export async function GET(request: NextRequest) { | ||
const queries = await getQueries(request, 'events'); | ||
const res = await getEvents(queries); | ||
return NextResponse.json({ ...res }, { status: res.ok ? 200 : 500 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { createEvent } from '@datalib/events/createEvent'; | ||
|
||
export async function POST(request: NextRequest) { | ||
const body = await request.json(); | ||
const res = await createEvent(body); | ||
return NextResponse.json({ ...res }, { status: res.ok ? 200 : 500 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import authenticated from '@utils/authentication/authenticated'; | ||
import { POST as post } from './post'; | ||
import { GET as get } from './get'; | ||
|
||
const POST = authenticated(post); | ||
const GET = authenticated(get); | ||
|
||
export { POST, GET }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
interface EventInt { | ||
_id: string; | ||
name: string; | ||
host?: string; | ||
type: string; | ||
location: string; | ||
star_time: Date; | ||
end_time: Date; | ||
tags?: string[]; | ||
} | ||
|
||
export default EventInt; |