Skip to content

Commit

Permalink
✨ unlike mechanism & smol refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brano-hozza committed Mar 24, 2024
1 parent 613eb6f commit c4829cd
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 37 deletions.
6 changes: 3 additions & 3 deletions server/api/auth/login.post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { authService } from '~/server/services/auth.service';
import { useAuthService } from '~/server/services/auth.service';
import { VerifyNonce } from '~/types/auth';

export default defineEventHandler(async (event) => {
const { verifyNonce } = authService();
const { signature, address } = await readBody<VerifyNonce>(event);
return await verifyNonce(signature, address);
const service = useAuthService();
return await service.verifyNonce(signature, address);
});
6 changes: 3 additions & 3 deletions server/api/auth/nonce.post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { authService } from '~/server/services/auth.service';
import { useAuthService } from '~/server/services/auth.service';
import { GetNonceDTO } from '~/types/auth';

export default defineEventHandler(async (event) => {
const { address } = await readBody<GetNonceDTO>(event);
const { getNonce } = authService();
return await getNonce(address);
const service = useAuthService();
return await service.getNonce(address);
});
6 changes: 3 additions & 3 deletions server/api/auth/refresh.post.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { authService } from '~/server/services/auth.service';
import { useAuthService } from '~/server/services/auth.service';
import { AuthenticatedUser, RefreshTokenRequestDTO } from '~/types/auth';

type Body = RefreshTokenRequestDTO & AuthenticatedUser;

export default defineEventHandler(async (event) => {
const { refreshToken: refToken, jwt } = await readBody<Body>(event);
const { refreshToken } = authService(jwt);
return await refreshToken(refToken);
const service = useAuthService(jwt);
return await service.refreshToken(refToken);
});
4 changes: 2 additions & 2 deletions server/api/comment/[id]/index.delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commentService } from '~/server/services/comment.service';
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';

type Body = AuthenticatedUser;
Expand All @@ -11,6 +11,6 @@ export default defineEventHandler(async (event) => {
message: 'Invalid comment id',
});
}
const service = commentService(jwt);
const service = useCommentService(jwt);
return await service.remove(Number(commentId));
});
4 changes: 2 additions & 2 deletions server/api/comment/[id]/index.put.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commentService } from '~/server/services/comment.service';
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';
import { UpdateCommentDTO } from '~/types/dtos';

Expand All @@ -12,6 +12,6 @@ export default defineEventHandler(async (event) => {
message: 'Invalid comment id',
});
}
const service = commentService(jwt);
const service = useCommentService(jwt);
return await service.update(Number(commentId), content);
});
15 changes: 15 additions & 0 deletions server/api/comment/[id]/like/index.delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
const { jwt } = await readBody<AuthenticatedUser>(event);
const commentId = getRouterParam(event, 'id');
if (Number.isNaN(commentId)) {
return createError({
message: 'Invalid comment id',
});
}

const service = useCommentService(jwt);
return await service.unlike(Number(commentId));
});
6 changes: 3 additions & 3 deletions server/api/comment/[id]/like/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commentService } from '~/server/services/comment.service';
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
Expand All @@ -10,6 +10,6 @@ export default defineEventHandler(async (event) => {
});
}

const { getLikes } = commentService(jwt);
return await getLikes(Number(commentId));
const service = useCommentService(jwt);
return await service.getLikes(Number(commentId));
});
6 changes: 3 additions & 3 deletions server/api/comment/[id]/like/index.post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commentService } from '~/server/services/comment.service';
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
Expand All @@ -10,6 +10,6 @@ export default defineEventHandler(async (event) => {
});
}

const { likeComment } = commentService(jwt);
return await likeComment(Number(commentId));
const service = useCommentService(jwt);
return await service.like(Number(commentId));
});
4 changes: 2 additions & 2 deletions server/api/comment/index.post.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { commentService } from '~/server/services/comment.service';
import { useCommentService } from '~/server/services/comment.service';
import { AuthenticatedUser } from '~/types/auth';
import { AddCommentDTO, NFTAddress } from '~/types/dtos';

type Body = AuthenticatedUser & AddCommentDTO & NFTAddress;

export default defineEventHandler(async (event) => {
const { jwt, content, parentCommentId, address } = await readBody<Body>(event);
const service = commentService(jwt);
const service = useCommentService(jwt);
return await service.add(address, content, parentCommentId);
});
15 changes: 15 additions & 0 deletions server/api/post/[id]/like/index.delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { usePostService } from '~/server/services/post.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
const { jwt } = await readBody<AuthenticatedUser>(event);
const nftAddress = getRouterParam(event, 'id');
if (!nftAddress) {
return createError({
message: 'Invalid NFT address',
});
}

const service = usePostService(jwt);
return await service.unlike(nftAddress);
});
6 changes: 3 additions & 3 deletions server/api/post/[id]/like/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { postService } from '~/server/services/post.service';
import { usePostService } from '~/server/services/post.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
Expand All @@ -10,6 +10,6 @@ export default defineEventHandler(async (event) => {
});
}

const { getLikes } = postService(jwt);
return await getLikes(nftAddress);
const service = usePostService(jwt);
return await service.getLikes(nftAddress);
});
6 changes: 3 additions & 3 deletions server/api/post/[id]/like/index.post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { postService } from '~/server/services/post.service';
import { usePostService } from '~/server/services/post.service';
import { AuthenticatedUser } from '~/types/auth';

export default defineEventHandler(async (event) => {
Expand All @@ -10,6 +10,6 @@ export default defineEventHandler(async (event) => {
});
}

const { likePost } = postService(jwt);
return await likePost(nftAddress);
const service = usePostService(jwt);
return await service.like(nftAddress);
});
6 changes: 3 additions & 3 deletions server/api/post/my.get.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { postService } from '~/server/services/post.service';
import { usePostService } from '~/server/services/post.service';
import { AuthenticatedUser } from '~/types/auth';
import { PaginationDTO } from '~/types/dtos';

export default defineEventHandler(async (event) => {
const { jwt } = await readBody<AuthenticatedUser>(event);
const { pageNumber, pageSize } = getQuery<PaginationDTO>(event);

const { getMyPosts } = postService(jwt);
return await getMyPosts(pageNumber, pageSize);
const service = usePostService(jwt);
return await service.getMyPosts(pageNumber, pageSize);
});
2 changes: 1 addition & 1 deletion server/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useApi } from '../utils/api';
import { GetNonceDTO, RefreshTokenDTO, RefreshTokenRequestDTO, VerifyNonce } from '~/types/auth';

export function authService(token?: string) {
export function useAuthService(token?: string) {
const getNonce = async (address: string): Promise<string> =>
await useApi<string, GetNonceDTO>('auth/nonce-message', undefined, {
method: 'POST',
Expand Down
13 changes: 10 additions & 3 deletions server/services/comment.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useApi } from '../utils/api';
import { AddCommentDTO, CommentDTO, CommentLikeDTO, UpdateCommentDTO } from '~/types/dtos';

export function commentService(token: string) {
export function useCommentService(token: string) {
const getAll = async (nftAddress: string, pageNumber: number, pageSize: number): Promise<CommentDTO[]> => {
const params: URLSearchParams = new URLSearchParams({
pageNumber: String(pageNumber),
Expand Down Expand Up @@ -43,16 +43,23 @@ export function commentService(token: string) {
return await useApi<CommentLikeDTO[]>(`post/comments/${commentId}/likes`, token);
};

const likeComment = async (commentId: number): Promise<CommentLikeDTO> => {
const like = async (commentId: number): Promise<CommentLikeDTO> => {
return await useApi<CommentLikeDTO>(`post/comments/${commentId}/ likes`, token, {
method: 'POST',
});
};

const unlike = async (commentId: number): Promise<void> => {
await useApi(`post/comments/${commentId}/ likes`, token, {
method: 'DELETE',
});
};

return {
getAll,
getLikes,
likeComment,
like,
unlike,
add,
update,
remove,
Expand Down
13 changes: 10 additions & 3 deletions server/services/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useApi } from '../utils/api';
import { LikePostResponseDTO, NFTPost, PostLikeDTO } from '~/types/dtos';

export function postService(token: string) {
export function usePostService(token: string) {
const getMyPosts = async (pageNumber: number, pageSize: number): Promise<NFTPost[]> => {
const params: URLSearchParams = new URLSearchParams({
pageNumber: String(pageNumber),
Expand All @@ -15,12 +15,19 @@ export function postService(token: string) {
return await useApi<PostLikeDTO[]>(`post/${nftAddress}/likes}`, token);
};

const likePost = async (nftAddress: string): Promise<LikePostResponseDTO> => {
const like = async (nftAddress: string): Promise<LikePostResponseDTO> => {
const resp = await useApi<LikePostResponseDTO>(`post/${nftAddress}/likes`, token, {
method: 'POST',
});
return resp;
};

return { getMyPosts, likePost, getLikes };
const unlike = async (nftAddress: string): Promise<LikePostResponseDTO> => {
const resp = await useApi<LikePostResponseDTO>(`post/${nftAddress}/likes`, token, {
method: 'DELETE',
});
return resp;
};

return { getMyPosts, like, unlike, getLikes };
}

0 comments on commit c4829cd

Please sign in to comment.