Skip to content

Commit

Permalink
Added trash api in backend and updated template
Browse files Browse the repository at this point in the history
  • Loading branch information
NadaaFarook committed Apr 4, 2022
1 parent 9a6148d commit 67325e7
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const signupHandler = function (schema, request) {
...rest,
notes: [],
archives: [],
trash: [],
};
const createdUser = schema.users.create(newUser);
const encodedToken = sign({ _id, email }, process.env.REACT_APP_JWT_SECRET);
Expand Down
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 });
};
10 changes: 10 additions & 0 deletions apps/cra-template-mockbee-notes-app/template/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
getAllNotesHandler,
updateNoteHandler,
} from "./backend/controllers/NotesController";
import {
deleteFromTrashHandler,
getAllTrashNotesHandler,
restoreFromTrashHandler,
} from "./backend/controllers/TrashController";
import { users } from "./backend/db/users";

export function makeServer({ environment = "development" } = {}) {
Expand All @@ -36,6 +41,7 @@ export function makeServer({ environment = "development" } = {}) {
...item,
notes: [],
archives: [],
trash: [],
})
);
},
Expand Down Expand Up @@ -63,6 +69,10 @@ export function makeServer({ environment = "development" } = {}) {
"/archives/delete/:noteId",
deleteFromArchivesHandler.bind(this)
);
// trash routes (private)
this.get("/trash", getAllTrashNotesHandler.bind(this));
this.post("/trash/restore/:noteId", restoreFromTrashHandler.bind(this));
this.delete("/trash/delete/:noteId", deleteFromTrashHandler.bind(this));
},
});
return server;
Expand Down
1 change: 1 addition & 0 deletions apps/notes-app/src/backend/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const signupHandler = function (schema, request) {
...rest,
notes: [],
archives: [],
trash: [],
};
const createdUser = schema.users.create(newUser);
const encodedToken = sign({ _id, email }, process.env.REACT_APP_JWT_SECRET);
Expand Down
72 changes: 72 additions & 0 deletions apps/notes-app/src/backend/controllers/TrashController.js
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 });
};
10 changes: 10 additions & 0 deletions apps/notes-app/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
getAllNotesHandler,
updateNoteHandler,
} from "./backend/controllers/NotesController";
import {
deleteFromTrashHandler,
getAllTrashNotesHandler,
restoreFromTrashHandler,
} from "./backend/controllers/TrashController";
import { users } from "./backend/db/users";

export function makeServer({ environment = "development" } = {}) {
Expand All @@ -36,6 +41,7 @@ export function makeServer({ environment = "development" } = {}) {
...item,
notes: [],
archives: [],
trash: [],
})
);
},
Expand Down Expand Up @@ -63,6 +69,10 @@ export function makeServer({ environment = "development" } = {}) {
"/archives/delete/:noteId",
deleteFromArchivesHandler.bind(this)
);
// trash routes (private)
this.get("/trash", getAllTrashNotesHandler.bind(this));
this.post("/trash/restore/:noteId", restoreFromTrashHandler.bind(this));
this.delete("/trash/delete/:noteId", deleteFromTrashHandler.bind(this));
},
});
return server;
Expand Down

0 comments on commit 67325e7

Please sign in to comment.