Skip to content

Commit

Permalink
feat: update queries
Browse files Browse the repository at this point in the history
  • Loading branch information
HereEast committed Sep 10, 2024
1 parent 7914141 commit 47db159
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
10 changes: 4 additions & 6 deletions client/src/api/getTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import axios from "axios";
import { BASE_URL } from "~/utils";
import { ITask } from "~/~/models/Task";

export async function getTasks(userId: string): Promise<ITask[] | undefined> {
export async function getTasksByUserId(
userId: string,
): Promise<ITask[] | undefined> {
try {
const response = await axios.get(`${BASE_URL}/tasks`, {
params: {
userId,
},
});
const response = await axios.get(`${BASE_URL}/tasks/${userId}`);

if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
Expand Down
4 changes: 2 additions & 2 deletions client/src/hooks/useTasks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";

import { getTasks } from "~/api";
import { getTasksByUserId } from "~/api";
import { ITask } from "~/~/models/Task";

export function useTasks(userId: string) {
Expand All @@ -14,7 +14,7 @@ export function useTasks(userId: string) {
setError(false);

try {
const userTasks = await getTasks(userId);
const userTasks = await getTasksByUserId(userId);

setData(userTasks);
setIsLoading(false);
Expand Down
51 changes: 21 additions & 30 deletions server/src/controllers/getTasks.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
import { Request, Response } from "express";

import { Task } from "../models/Task.js";
import { IEntry } from "../models/Entry.js";
import mongoose from "mongoose";

// Get all tasks by UserId
export async function getMonthTasks(req: Request, res: Response) {
const { userId, year, month } = req.params;
export async function getTasksByUserId(req: Request, res: Response) {
const { userId } = req.params;

if (!userId) {
throw new Error("User ID is required.");
}

const tasks = await Task.find({ userId }).exec();

// const tasksWithFilteredEntries = tasks.map((task) => {
// const filteredEntries = task.entries.filter((entry) => {
// const entryYear = entry.date.getFullYear();

// return entryYear === Number(year);
// });

// return {
// ...task.toObject(),
// entries: filteredEntries,
// };
// });

return res.json(tasks);
}

// Get all tasks by UserId
export async function getTasksByUserId(req: Request, res: Response) {
const { userId } = req.params;
// export async function getMonthTasks(req: Request, res: Response) {
// const { userId, year, month } = req.params;

if (!userId) {
throw new Error("User ID is required.");
}
// if (!userId) {
// throw new Error("User ID is required.");
// }

const tasks = await Task.find({ userId }).exec();
// const tasks = await Task.find({ userId }).exec();

return res.json(tasks);
}
// const tasksWithFilteredEntries = tasks.map((task) => {
// const filteredEntries = task.entries.filter((entry) => {
// const entryYear = entry.date.getFullYear();

// Get all tasks
export async function getAllTasks(req: Request, res: Response) {
const tasks = await Task.find();
// return entryYear === Number(year);
// });

return res.json(tasks);
}
// return {
// ...task.toObject(),
// entries: filteredEntries,
// };
// });

// return res.json(tasks);
// }

0 comments on commit 47db159

Please sign in to comment.