#58 tasks/user/:userId resolved

parent 21d8af09
...@@ -3,6 +3,9 @@ import {Project} from '../models/Project'; ...@@ -3,6 +3,9 @@ import {Project} from '../models/Project';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
import { User } from '../models/User'; import { User } from '../models/User';
import { Member, MemberRole } from '../models/Member'; import { Member, MemberRole } from '../models/Member';
import { userInfo } from 'os';
import { Task } from '../models/Task';
import { getRepository } from 'typeorm';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -58,14 +61,28 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => { ...@@ -58,14 +61,28 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
.from(Project, "project") .from(Project, "project")
.leftJoinAndSelect('project.members', 'member') .leftJoinAndSelect('project.members', 'member')
.leftJoinAndSelect('member.user', 'user' ) .leftJoinAndSelect('member.user', 'user' )
.leftJoinAndSelect('project.tasks','task')
.where("project.id = :id", { id: req.params.id }) .where("project.id = :id", { id: req.params.id })
.getOne() .getOne()
if (!project) return res.status(404).send({Message:'no info on this project'}) if (!project) return res.status(404).send({Message:'no info on this project'})
return res.send({project}) const tasks = await dataSource.getRepository('Task')
.find(
{
relations:{
executor:true,
author:true,
},
where:{
project:{
id:req.params.id
}
}
}
)
return res.send({project, tasks})
}) })
router.get('/userId/:userId', async (req : Request, res : Response): Promise<Response>=>{
router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{
const userId:string = req.params.userId const userId:string = req.params.userId
const userProjects = await dataSource const userProjects = await dataSource
.createQueryBuilder() .createQueryBuilder()
......
...@@ -2,8 +2,6 @@ import express,{Router, Request, Response} from 'express'; ...@@ -2,8 +2,6 @@ import express,{Router, Request, Response} from 'express';
import {Task} from '../models/Task'; import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
import { User } from '../models/User'; import { User } from '../models/User';
import { Project } from '../models/Project';
import { Member } from '../models/Member';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -11,14 +9,18 @@ const dataSource = myDataSource; ...@@ -11,14 +9,18 @@ const dataSource = myDataSource;
router.get('/', async(req:Request, res:Response):Promise<Response> => { router.get('/', async(req:Request, res:Response):Promise<Response> => {
const tasks = await dataSource const tasks = await dataSource
.getRepository(Task) .getRepository(Task)
.find() .find({
relations:{
executor:true,
author:true,
}})
return res.send({tasks}) return res.send({tasks})
}) })
router.post('/', async(req:Request, res:Response):Promise<Response>=>{ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
const token = req.get('Authorization'); const token = req.get('Authorization');
const newTask = new Task(); const newTask = new Task();
const {title,description,project,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body; const {title,description,project,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
...@@ -29,7 +31,6 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -29,7 +31,6 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.title = title; newTask.title = title;
newTask.description = description; newTask.description = description;
newTask.project = project; newTask.project = project;
// newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue; newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart; newTask.dateTimeStart = dateTimeStart;
newTask.author= user; newTask.author= user;
...@@ -41,22 +42,26 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -41,22 +42,26 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{
const userId = req.params.userId; const userId = req.params.userId;
// const tasks = await dataSource
// .getRepository(Task)
// .createQueryBuilder('task')
// .innerJoinAndSelect('task.executors', 'user')
// .where('user.id = :userId', {userId :userId})
// .getMany()
// return res.send({tasks})
const tasks = await dataSource const tasks = await dataSource
.getRepository(Member) .getRepository(Task)
.createQueryBuilder('member') .find({
.innerJoinAndSelect('member.project','project') relations:{
.innerJoinAndSelect('project.tasks','task') executor:true,
.where('member.user.id = :userId') author:true,
.andWhere('member.project.id= task.project.id') },
.select('task.*') where:[
.getMany() {
executor:{
id:userId
}
},
{
author:{
id:userId
}
},
]
})
return res.send({tasks}) return res.send({tasks})
}) })
...@@ -71,10 +76,24 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{ ...@@ -71,10 +76,24 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
if(!user) return res.status(404).send({Message:'user not found'}) if(!user) return res.status(404).send({Message:'user not found'})
const tasks = await dataSource const tasks = await dataSource
.getRepository(Task) .getRepository(Task)
.createQueryBuilder('task') .find({
.innerJoinAndSelect('task.executors', 'user') relations:{
.where('user.id = :userId', {userId :user.id}) executor:true,
.getMany() author:true,
},
where:[
{
executor:{
id:user.id
}
},
{
author:{
id:user.id
}
},
]
})
return res.send({tasks}) return res.send({tasks})
}) })
...@@ -152,24 +171,4 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{ ...@@ -152,24 +171,4 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
// router.post('/projects',async (req: Request, res: Response):Promise<Response>=>{
// let projectArray :string[]= req.body;
// console.log('projectArray ', projectArray)
// if (projectArray.length===0) {
// const tasks = await
// dataSource
// .getRepository(Task)
// .createQueryBuilder('task')
// // .innerJoinAndSelect('task.executors', 'user')
// .innerJoinAndSelect('task.executors AND task.author', 'user')
// .where('task.project IS NULL')
// .getMany()
// return res.send({tasks})
// }
// ///ssome code
// return res.send({message:"some other staff"})
// })
export default router; export default router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment