Skip to content

Commit

Permalink
Merge pull request #36 from pol-dev-shinroo/main
Browse files Browse the repository at this point in the history
pr
  • Loading branch information
pol-dev-shinroo authored May 14, 2023
2 parents 75cc235 + 0c3e6ae commit ed09b38
Show file tree
Hide file tree
Showing 25 changed files with 1,434 additions and 32 deletions.
15 changes: 11 additions & 4 deletions src/common/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,35 @@ export const dbException = (error: any) => {

export const notFoundAccountException = (id?: number) => {
throw {
code: 404,
code: StatusCodes.INTERNAL_SERVER_ERROR,
message: `user does not exist with id ${id}`,
};
};

export const notFoundNews = (news_id?: number) => {
throw {
code: 404,
code: StatusCodes.INTERNAL_SERVER_ERROR,
message: `news does not exist with id ${news_id}`,
};
};

export const notFoundError = (message: string) => {
throw {
code: StatusCodes.INTERNAL_SERVER_ERROR,
message,
};
};

export const DuplicateError = (msg: string) => {
throw {
code: StatusCodes.CONFLICT,
code: StatusCodes.INTERNAL_SERVER_ERROR,
message: msg,
};
};

export const LimitError = (msg: string) => {
throw {
code: StatusCodes.CONFLICT,
code: StatusCodes.INTERNAL_SERVER_ERROR,
message: msg,
};
};
Expand Down
108 changes: 107 additions & 1 deletion src/controllers/bookmark/bookmark.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ class BookmarkController implements Controller {
this.bookmark,
this.listAllBookmarks,
this.createFolder,
this.listAllFolders
this.listAllFolders,
this.allocate,
this.updateFolderName,
this.listBookmarksFromFolder,
this.removeBookmarkFromFolder,
this.deleteBookmark,
this.deleteBookmarkFolder
);
createRoutes(newsRoutes, this.router);
}
Expand Down Expand Up @@ -74,6 +80,106 @@ class BookmarkController implements Controller {
response.error(err as ErrorData);
}
});

private allocate = asyncWrapper(async (req: CustomRequest, res) => {
const response = customResponse(res);
const profile_id = req.profile_id;
const { folder_id, bookmark_id } = req.body;
try {
const data = await bookmarkService.allocate(
profile_id!,
folder_id,
bookmark_id
);
response.success({ code: StatusCodes.CREATED, data });
} catch (err) {
response.error(err as ErrorData);
}
});

private updateFolderName = asyncWrapper(async (req: CustomRequest, res) => {
const response = customResponse(res);
const profile_id = req.profile_id;
const { name: new_name, folder_id } = req.body;
try {
const data = await bookmarkService.updateFolderName(
profile_id!,
folder_id,
new_name
);
response.success({ code: StatusCodes.CREATED, data });
} catch (err) {
response.error(err as ErrorData);
}
});

private listBookmarksFromFolder = asyncWrapper(
async (req: CustomRequest, res) => {
const response = customResponse(res);
const profile_id = req.profile_id;
const { folder_id } = req.body;
try {
const data = await bookmarkService.listBookmarksFromFolder(
folder_id,
profile_id!
);
response.success({ code: StatusCodes.CREATED, data });
} catch (err) {
response.error(err as ErrorData);
}
}
);

private removeBookmarkFromFolder = asyncWrapper(
async (req: CustomRequest, res) => {
const response = customResponse(res);
const { bookmark_id } = req.body;

try {
await bookmarkService.removeBookmarkFromFolder(bookmark_id);
response.success({
code: StatusCodes.CREATED,
});
} catch (err) {
response.error(err as ErrorData);
}
}
);

private deleteBookmark = asyncWrapper(async (req: CustomRequest, res) => {
const response = customResponse(res);
const { bookmark_id } = req.body;

try {
await bookmarkService.deleteBookmark(bookmark_id);
response.success({
code: StatusCodes.CREATED,
});
} catch (err) {
response.error(err as ErrorData);
}
});

private deleteBookmarkFolder = asyncWrapper(
async (req: CustomRequest, res) => {
const response = customResponse(res);
const profile_id = req.profile_id;
const { folder_id } = req.body;

try {
await bookmarkService.deleteBookmarkFolder(
profile_id!,
folder_id
);
response.success({
code: StatusCodes.CREATED,
data: `folder : ${folder_id} is deleted`,
});
} catch (err) {
response.error(err as ErrorData);
}
}
);
}

export default BookmarkController;
80 changes: 79 additions & 1 deletion src/controllers/bookmark/bookmark.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@ import {
tokenValidationProfile,
bookmarkValidation,
bookmarkFolderNameValidation,
bookmarkAllocationValidation,
} from '@/middlewares/index';
import {
bookmark_validation,
createFolder_validation,
allocate_validation,
updateFolderName_validation,
listBookmarksFromFolder_validation,
removeBookmarkFromFolder_validation,
deleteBookmark_validation,
deleteBookmarkFolder_validation,
} from './bookmark.validation';

export function createSearchRoutes(
path: string,
bookmark: any,
listAllBookmarks: any,
createFolder: any,
listAllFolders: any
listAllFolders: any,
allocate: any,
updateFolderName: any,
listBookmarksFromFolder: any,
removeBookmarkFromFolder: any,
deleteBookmark: any,
deleteBookmarkFolder: any
): AuthRoutes {
return {
bookmark: {
Expand All @@ -31,12 +44,14 @@ export function createSearchRoutes(
],
handler: bookmark,
},

listAllBookmarks: {
method: 'get',
path: `${path}/listAllBookmarks`,
middleware: [tokenValidationProfile()],
handler: listAllBookmarks,
},

createFolder: {
method: 'post',
path: `${path}/createFolder`,
Expand All @@ -47,11 +62,74 @@ export function createSearchRoutes(
],
handler: createFolder,
},

listAllFolders: {
method: 'get',
path: `${path}/listAllFolders`,
middleware: [tokenValidationProfile()],
handler: listAllFolders,
},

allocate: {
method: 'post',
path: `${path}/allocate`,
middleware: [
bodyValidation(allocate_validation),
tokenValidationProfile(),
bookmarkAllocationValidation(),
],
handler: allocate,
},

updateFolderName: {
method: 'put',
path: `${path}/updateFolderName`,
middleware: [
bodyValidation(updateFolderName_validation),
tokenValidationProfile(),
bookmarkFolderNameValidation(),
],
handler: updateFolderName,
},

listBookmarksFromFolder: {
method: 'get',
path: `${path}/listBookmarksFromFolder`,
middleware: [
bodyValidation(listBookmarksFromFolder_validation),
tokenValidationProfile(),
],
handler: listBookmarksFromFolder,
},

removeBookmarkFromFolder: {
method: 'delete',
path: `${path}/removeBookmarkFromFolder`,
middleware: [
bodyValidation(removeBookmarkFromFolder_validation),
tokenValidationProfile(),
],
handler: removeBookmarkFromFolder,
},

deleteBookmark: {
method: 'delete',
path: `${path}/deleteBookmark`,
middleware: [
bodyValidation(deleteBookmark_validation),
tokenValidationProfile(),
],
handler: deleteBookmark,
},

deleteBookmarkFolder: {
method: 'delete',
path: `${path}/deleteBookmarkFolder`,
middleware: [
bodyValidation(deleteBookmarkFolder_validation),
tokenValidationProfile(),
],
handler: deleteBookmarkFolder,
},
};
}
69 changes: 57 additions & 12 deletions src/controllers/bookmark/bookmark.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bookmarkRepository } from '@/database/repositories/bookmark.repository';
import { bookmark_folderRepository } from '@/database/repositories/bookmark_folder.repository';
import { LimitError } from '@/common/exceptions';
import { dbException } from '@/common/exceptions';

export const bookmarkService = {
repository: bookmarkRepository,
Expand All @@ -16,17 +16,8 @@ export const bookmarkService = {
return res;
},

async createFolder(
profile_id: number,
name: string,
maxFolders: number = 7
) {
const folderCount = await this.folder_repository.countFolders(
profile_id
);
if (folderCount >= maxFolders) {
return LimitError(`max folderNo. exceeded: ${maxFolders}`);
}
async createFolder(profile_id: number, name: string) {
await this.folder_repository.countFolders(profile_id);
const folder = await this.folder_repository.createFolder(
profile_id,
name
Expand All @@ -37,4 +28,58 @@ export const bookmarkService = {
async listAllFolders(profile_id: number) {
return await this.folder_repository.listAllFolders(profile_id);
},

async allocate(profile_id: number, folder_id: number, bookmark_id: number) {
await this.repository.findBookMark(bookmark_id, profile_id);
await this.folder_repository.findFolder(profile_id, folder_id);
await this.repository.allocateBookmarkToFolder(bookmark_id, folder_id);
},

async updateFolderName(
profile_id: number,
folder_id: number,
new_name: string
) {
return await this.folder_repository.updateFolderName(
profile_id,
folder_id,
new_name
);
},

async listBookmarksFromFolder(folder_id: number, profile_id: number) {
console.log('hi');
await this.folder_repository.findFolder(profile_id, folder_id);
return await this.repository.listBookmarksFromFolder(folder_id);
},

async removeBookmarkFromFolder(bookmark_id: number) {
return await this.repository.removeBookmarkFromFolder(bookmark_id);
},

async deleteBookmark(bookmark_id: number) {
const bookmark = await this.repository.findBookmarkSimple(bookmark_id);
await bookmark.destroy({ force: true });
},

async deleteBookmarkFolder(profile_id: number, folder_id: number) {
try {
const folder = await this.folder_repository.findFolder(
profile_id,
folder_id
);
const bookmarks = await this.repository.findAllBookmarksWithFolder(
folder_id,
profile_id
);
for (let bookmark of bookmarks) {
bookmark.folder_id = null;
await bookmark.save();
}

await folder.destroy({ force: true });
} catch (err) {
return dbException(err);
}
},
};
Loading

0 comments on commit ed09b38

Please sign in to comment.