Skip to content

Commit

Permalink
renames
Browse files Browse the repository at this point in the history
  • Loading branch information
mariovyord committed Dec 22, 2023
1 parent dc5a7fe commit 624f275
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
File renamed without changes.
16 changes: 8 additions & 8 deletions src/features/article/article-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IFullQuery, parseQueryToMongoParams } from "../../utils/parse-query";
import * as articleRepository from "./article-repository";
import * as articleQueries from "./article-queries";
import { ArticleDto, ICreateArticleData, IPatchArticleData } from "./article-types";

export async function getAll(query: IFullQuery): Promise<ArticleDto[] | number> {
const parsedQuery = parseQueryToMongoParams(query);

if (parsedQuery.count) {
return articleRepository.countDocumentsByQuery(parsedQuery);
return articleQueries.countDocumentsByQuery(parsedQuery);
}

const articles = await articleRepository.findArticlesByQuery(parsedQuery);
const articles = await articleQueries.findArticlesByQuery(parsedQuery);

return articles.map((x) => new ArticleDto(x));
}
Expand All @@ -26,7 +26,7 @@ export async function getOne(id: string, query: any): Promise<ArticleDto | null>
}
}

const article = await articleRepository.findArticleById(id, { populate, limitPopulate });
const article = await articleQueries.findArticleById(id, { populate, limitPopulate });

if (!article) {
throw new Error("Not found");
Expand All @@ -36,14 +36,14 @@ export async function getOne(id: string, query: any): Promise<ArticleDto | null>
}

export async function create(data: ICreateArticleData): Promise<ArticleDto> {
const article = await articleRepository.createArticle(data);
const article = await articleQueries.createArticle(data);
return new ArticleDto(article);
}

const ALLOWED_UPDATE_FIELDS = ["title", "content"];

export async function update(id: string, userId: string, data: IPatchArticleData): Promise<ArticleDto> {
const article = await articleRepository.findArticleById(id);
const article = await articleQueries.findArticleById(id);

if (article === null) throw new Error("Article not found");
if (article.owner.toString() !== userId) throw new Error("Only owners can update article");
Expand All @@ -60,10 +60,10 @@ export async function update(id: string, userId: string, data: IPatchArticleData
}

export async function remove(id: string, userId: string): Promise<void> {
const article = await articleRepository.findArticleById(id);
const article = await articleQueries.findArticleById(id);

if (!article) throw new Error("Article does not exist");
if (article.owner.toString() !== userId) throw new Error("Only owners can delete articles");

await articleRepository.deleteArticleById(id);
await articleQueries.deleteArticleById(id);
}
File renamed without changes.
16 changes: 8 additions & 8 deletions src/features/comment/comment-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IFullQuery, parseQueryToMongoParams } from "../../utils/parse-query";
import * as commentRepository from "./comment-repository";
import * as commentQueries from "./comment-queries";
import { CommentDto, ICreateCommentData, IPatchCommentData } from "./comment-types";

export async function getAllComments(query: IFullQuery): Promise<CommentDto[] | number> {
const parsedQuery = parseQueryToMongoParams(query);

if (parsedQuery.count) {
return commentRepository.countDocumentsByQuery(parsedQuery);
return commentQueries.countDocumentsByQuery(parsedQuery);
}

const comments = await commentRepository.findCommentsByQuery(parsedQuery);
const comments = await commentQueries.findCommentsByQuery(parsedQuery);

return comments.map((x) => new CommentDto(x));
}
Expand All @@ -26,7 +26,7 @@ export async function getOneComment(id: string, query: any): Promise<CommentDto
}
}

const comment = await commentRepository.findCommentById(id, { populate, limitPopulate });
const comment = await commentQueries.findCommentById(id, { populate, limitPopulate });

if (!comment) {
throw new Error("Not found");
Expand All @@ -36,14 +36,14 @@ export async function getOneComment(id: string, query: any): Promise<CommentDto
}

export async function createComment(data: ICreateCommentData): Promise<CommentDto> {
const comment = await commentRepository.createComment(data);
const comment = await commentQueries.createComment(data);
return new CommentDto(comment);
}

const ALLOWED_UPDATE_FIELDS = ["content"];

export async function updateComment(id: string, userId: string, data: IPatchCommentData): Promise<CommentDto> {
const comment = await commentRepository.findCommentById(id);
const comment = await commentQueries.findCommentById(id);

if (comment === null) throw new Error("Comment not found");
if (comment.owner.toString() !== userId) throw new Error("Only owners can update commet");
Expand All @@ -60,10 +60,10 @@ export async function updateComment(id: string, userId: string, data: IPatchComm
}

export async function deleteComment(id: string, userId: string): Promise<void> {
const comment = await commentRepository.findCommentById(id);
const comment = await commentQueries.findCommentById(id);

if (!comment) throw new Error("Comment does not exist");
if (comment.owner.toString() !== userId) throw new Error("Only owners can delete comments");

await commentRepository.deleteCommentById(id);
await commentQueries.deleteCommentById(id);
}
File renamed without changes.
14 changes: 7 additions & 7 deletions src/features/user/user-service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import jwt from "jsonwebtoken";
import getConfig from "../../config/get-config";
import { IUserLocal, ISignUpUserData, UserDto, IUpdateUserData } from "./user-types";
import * as userRepository from "./user-repository";
import * as userQueries from "./user-queries";

export async function signUp(userData: ISignUpUserData): Promise<[string, UserDto]> {
const existing = await userRepository.findOneByUsername(userData.username);
const existing = await userQueries.findOneByUsername(userData.username);

if (existing) {
throw new Error("Username already exists");
}

const user = await userRepository.createUser(userData);
const user = await userQueries.createUser(userData);
const token = await createToken(user.id);

return [token, new UserDto(user)];
}

export async function signIn(username: string, password: string): Promise<[string, UserDto]> {
const user = await userRepository.findOneByPassword(username, password);
const user = await userQueries.findOneByPassword(username, password);

if (!user) {
throw new Error("Incorrect username or password");
Expand All @@ -41,7 +41,7 @@ async function createToken(id: string) {
}

export async function getUser(userData: IUserLocal): Promise<UserDto> {
const user = await userRepository.findOneById(userData.id);
const user = await userQueries.findOneById(userData.id);

if (!user) {
throw new Error("Not found");
Expand All @@ -53,7 +53,7 @@ export async function getUser(userData: IUserLocal): Promise<UserDto> {
const ALLOWED_UPDATE_FIELDS = ["firstName", "lastName"];

export async function updateUser(userId: string, userData: Partial<IUpdateUserData>): Promise<UserDto> {
const user = await userRepository.findOneById(userId);
const user = await userQueries.findOneById(userId);

if (user === null || user._id.toString() !== userId) {
throw new Error("Failed to update user");
Expand All @@ -72,7 +72,7 @@ export async function updateUser(userId: string, userData: Partial<IUpdateUserDa
}

export async function updatePassword(userId: string, oldPassword: string, newPassword: string) {
const user = await userRepository.findOneById(userId);
const user = await userQueries.findOneById(userId);

if (!user) {
throw new Error("User does not exist");
Expand Down

0 comments on commit 624f275

Please sign in to comment.