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.
- Loading branch information
1 parent
c1424c9
commit f8e36f1
Showing
11 changed files
with
232 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,37 @@ | ||
import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; | ||
import parseAndReplace from '@utils/request/parseAndReplace'; | ||
import { 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(); | ||
const currentDate = new Date().toISOString(); | ||
const creationStatus = await db.collection('events').insertOne({ | ||
...parsedBody, | ||
_last_modified: currentDate, | ||
_created_at: currentDate, | ||
}); | ||
|
||
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: null, | ||
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 a user from the database by id | ||
* @param query - ID of User | ||
* @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,9 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { deleteEvent } from '@datalib/events/deleteEvent'; | ||
|
||
export async function DELETE( | ||
_: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
return NextResponse.json(await deleteEvent(params.id)); | ||
} |
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 { getEvent } from '@datalib/events/getEvent'; | ||
|
||
export async function GET( | ||
_: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
return NextResponse.json(await getEvent(params.id)); | ||
} |
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 { updateEvent } from '@datalib/events/updateEvent'; | ||
|
||
export async function PUT( | ||
request: NextRequest, | ||
{ params }: { params: { id: string } } | ||
) { | ||
const body = await request.json(); | ||
return NextResponse.json(await updateEvent(params.id, body)); | ||
} |
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,8 @@ | ||
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'); | ||
return NextResponse.json(await getEvents(queries)); | ||
} |
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,7 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { createEvent } from '@datalib/events/createEvent'; | ||
|
||
export async function POST(request: NextRequest) { | ||
const body = await request.json(); | ||
return NextResponse.json(await createEvent(body)); | ||
} |
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 }; |