-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added trash api in backend and updated template
- Loading branch information
1 parent
9a6148d
commit 67325e7
Showing
6 changed files
with
166 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
72 changes: 72 additions & 0 deletions
72
apps/cra-template-mockbee-notes-app/template/src/backend/controllers/TrashController.js
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,72 @@ | ||
import { Response } from "miragejs"; | ||
import { requiresAuth } from "../utils/authUtils"; | ||
|
||
/** | ||
* All the routes related to Trash are present here. | ||
* These are Privately accessible routes. | ||
* */ | ||
|
||
/** | ||
* This handler handles gets all trashed notes in the db. | ||
* send GET Request at /api/trash | ||
* */ | ||
|
||
export const getAllTrashNotesHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
return new Response(200, {}, { trash: user.trash }); | ||
}; | ||
|
||
/** | ||
* This handler handles deletes note from trash. | ||
* send DELETE Request at /api/trash/delete/:noteId | ||
* */ | ||
|
||
export const deleteFromTrashHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
const { noteId } = request.params; | ||
user.trash = user.trash.filter((note) => note._id !== noteId); | ||
this.db.users.update({ _id: user._id }, user); | ||
return new Response(200, {}, { trash: user.trash }); | ||
}; | ||
|
||
/** | ||
* This handler handles restoring the trashed notes to user notes. | ||
* send POST Request at /api/trash/restore/:noteId | ||
* */ | ||
|
||
export const restoreFromTrashHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
const { noteId } = request.params; | ||
const restoredNote = user.trash.filter((note) => note._id === noteId)[0]; | ||
user.trash = user.trash.filter((note) => note._id !== noteId); | ||
user.notes.push({ ...restoredNote }); | ||
this.db.users.update({ _id: user._id }, user); | ||
return new Response(200, {}, { trash: user.trash, notes: user.notes }); | ||
}; |
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
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
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,72 @@ | ||
import { Response } from "miragejs"; | ||
import { requiresAuth } from "../utils/authUtils"; | ||
|
||
/** | ||
* All the routes related to Trash are present here. | ||
* These are Privately accessible routes. | ||
* */ | ||
|
||
/** | ||
* This handler handles gets all trashed notes in the db. | ||
* send GET Request at /api/trash | ||
* */ | ||
|
||
export const getAllTrashNotesHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
return new Response(200, {}, { trash: user.trash }); | ||
}; | ||
|
||
/** | ||
* This handler handles deletes note from trash. | ||
* send DELETE Request at /api/trash/delete/:noteId | ||
* */ | ||
|
||
export const deleteFromTrashHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
const { noteId } = request.params; | ||
user.trash = user.trash.filter((note) => note._id !== noteId); | ||
this.db.users.update({ _id: user._id }, user); | ||
return new Response(200, {}, { trash: user.trash }); | ||
}; | ||
|
||
/** | ||
* This handler handles restoring the trashed notes to user notes. | ||
* send POST Request at /api/trash/restore/:noteId | ||
* */ | ||
|
||
export const restoreFromTrashHandler = function (schema, request) { | ||
const user = requiresAuth.call(this, request); | ||
if (!user) { | ||
new Response( | ||
404, | ||
{}, | ||
{ | ||
errors: ["The email you entered is not Registered. Not Found error"], | ||
} | ||
); | ||
} | ||
const { noteId } = request.params; | ||
const restoredNote = user.trash.filter((note) => note._id === noteId)[0]; | ||
user.trash = user.trash.filter((note) => note._id !== noteId); | ||
user.notes.push({ ...restoredNote }); | ||
this.db.users.update({ _id: user._id }, user); | ||
return new Response(200, {}, { trash: user.trash, notes: user.notes }); | ||
}; |
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