Commit 37c61d64 authored by “Yevgeniy's avatar “Yevgeniy

#25 finished task, user CR, fix many to many relations

parent 9c90a525
......@@ -6,7 +6,8 @@ import {
BaseEntity,
ManyToOne,
ManyToMany,
OneToMany
OneToMany,
JoinTable
} from 'typeorm';
import {User} from './User';
import {Task} from './Task';
......@@ -61,12 +62,13 @@ import {
@Column({ name: 'department', type: Boolean,nullable:true ,default: false})
department!: boolean;
@ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects)
@ManyToOne(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
admin!: User;
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects)
workers!: User[];
@OneToMany(() => Task, (task: { project: Project; })=>task.project)
@OneToMany(() => Task, (task: { project: Project; })=>task.project,{eager : true})
tasks!:Task[]|null;
@ManyToMany(() => User, (user: { projects: Project[]; }) => user.projects,{eager : true})
@JoinTable()
workers!: User[];
}
\ No newline at end of file
......@@ -5,7 +5,8 @@ import {
CreateDateColumn,
BaseEntity,
ManyToOne,
ManyToMany
ManyToMany,
JoinTable
} from 'typeorm';
import {User} from './User';
import {Project} from './Project';
......@@ -17,12 +18,12 @@ import {
title: string;
description: string;
createdAt: Date;
// dateTimeStart:Date| null;
// dateTimeDue:Date| null;
assignedTo: User[];
dateTimeStart:Date| null;
dateTimeDue:Date| null;
accomplish: taskFinishType;
author: User;
project:Project|null;
executors:User[]
}
@Entity({ name: 'Task' })
......@@ -35,10 +36,10 @@ import {
description!: string
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date;
// @CreateDateColumn({ name: 'dateTimeStart', type: Date,nullable: true })
// dateTimeStart!: Date | null;
// @CreateDateColumn({ name: 'dateTimeDue', type: Date,nullable: true })
// dateTimeDue!: Date | null;
@Column({ name: 'dateTimeStart', type: Date,nullable: true })
dateTimeStart!: Date | null;
@Column({ name: 'dateTimeDue', type: Date,nullable: true })
dateTimeDue!: Date | null;
@Column({
type: "enum",
enum: ["opened", "done" , "failed"],
......@@ -47,12 +48,16 @@ import {
accomplish!: taskFinishType
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks)
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User;
@ManyToMany(() => User, (user: { tasks: Task[]; }) => user.tasks)
assignedTo!: User[];
@ManyToMany(() => User,{eager : true})
@JoinTable()
executors!: User[];
@ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks)
project!: Project | null;
}
......@@ -6,7 +6,8 @@ import {
BeforeInsert,
BaseEntity,
ManyToMany,
OneToMany
OneToMany,
JoinTable
} from 'typeorm';
import {IsEmail
......@@ -31,7 +32,9 @@ interface IUser {
token: string;
role: userRoleType;
createdAt: Date;
createdTasks:Task[]| null;
createdTasks:Task[];
workerInProjects:Project[];
adminInProjects:Project[];
}
......@@ -77,13 +80,14 @@ export class User extends BaseEntity implements IUser {
createdTasks!: Task[];
@ManyToMany(() => Task, (task: { users: User[] }) =>task.users)
tasksToMake!: Task[];
tasks!: Task[];
@OneToMany(() => Project, (project: { user: User }) => project.user)
adminInProjects!: Project[];
@ManyToMany(() => Project, (project: { users: User[] }) =>project.users)
@ManyToMany(() => Project,(project: { user: User }) => project.user)
@JoinTable()
workerInProjects!: Project[];
......
......@@ -12,29 +12,16 @@ router.get('/',async (req:Request, res:Response): Promise<Response>=> {
return res.send({projects})
})
router.get("/project_id/:project_id",async (req:Request, res:Response): Promise<Response> => {
const project = await dataSource
.createQueryBuilder()
.select("project")
.from(Project, "project")
// .where("user.workerInProjects= :projects", {projects.id : req.params.project_id } )
// .innerJoinAndSelect('project.workers','user')
.leftJoinAndSelect('project.admin','user')
.where("project.id = :id", { id: req.params.project_id })
.getOne()
if (!project) return res.status(404).send({Message:'no such project in the base'})
return res.send({project})
})
router.post('/', async (req:Request, res:Response): Promise<Response> => {
if (!req.body) return res.status(400).send({Message:'problem in incoming req.body'})
const {title, dateDue,color,department, userId,workers,tasks}= req.body;
const token = req.get('Authorization');
const {title, dateDue,color,department,workers,tasks}= req.body;
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.id = :id", { id: userId })
.where("user.token = :token", { token: token })
.getOne()
if(!user) return res.status(404).send({Message:'user not found'})
const project : Project= new Project()
......@@ -49,8 +36,19 @@ router.post('/', async (req:Request, res:Response): Promise<Response> => {
return res.send({project})
})
router.get("/id/:id",async (req:Request, res:Response): Promise<Response> => {
const project = await dataSource
.createQueryBuilder()
.select("project")
.from(Project, "project")
.leftJoinAndSelect('project.admin', 'user')
.where("project.id = :id", { id: req.params.id })
.getOne()
if (!project) return res.status(404).send({Message:'no info on this project'})
return res.send({project})
})
router.get('/:userId', async (req : Request, res : Response): Promise<Response>=>{
router.get('/userId/:userId', async (req : Request, res : Response): Promise<Response>=>{
const userId:string = req.params.userId
const user = await dataSource
.createQueryBuilder()
......@@ -63,7 +61,8 @@ router.get('/:userId', async (req : Request, res : Response): Promise<Response>=
.createQueryBuilder()
.select('project')
.from(Project, "project")
.where("Project_adminId = :id", { id: userId })
.where("project.admin = :id", { id: userId })
.getMany();
return res.send({userProjects})
})
......
import express,{Router, Request, Response} from 'express';
// import {User} from '../models/User';
// import {Project} from '../models/Project';
import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source';
// import { nanoid } from 'nanoid';
import { User } from '../models/User';
const router:Router = express.Router();
const dataSource = myDataSource;
router.get('/', async(req:Request, res:Response):Promise<Response> => {
const tasks = await dataSource.manager.find(Task)
const tasks = await dataSource
.getRepository(Task)
.find()
return res.send({tasks})
})
export default router;
router.post('/', async(req:Request, res:Response):Promise<Response>=>{
const token = req.get('Authorization');
const newTask = new Task();
const {title,description,project,executors,dateTimeDue,dateTimeStart} = req.body;
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.token = :token", { token: token })
.getOne()
if (!user) return res.status(404).send({Message:'user not found'})
newTask.title = title;
newTask.description = description;
newTask.project = project;
newTask.executors = executors;
newTask.dateTimeDue = dateTimeDue;
newTask.dateTimeStart = dateTimeStart;
newTask.author= user;
await newTask.save();
return res.send({newTask})
})
router.get('/userId/:userId', async (req: Request, res: Response)=>{
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})
})
router.get('/my', async (req: Request, res: Response)=>{
const token = req.get('Authorization');
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.token = :token", { token: token })
.getOne()
if(!user) return res.status(404).send({Message:'user not found'})
const tasks = await dataSource
.getRepository(Task)
.createQueryBuilder('task')
.innerJoinAndSelect('task.executors', 'user')
.where('user.id = :userId', {userId :user.id})
.getMany()
return res.send({tasks})
})
\ No newline at end of file
......@@ -7,7 +7,10 @@ const router:Router = express.Router();
const dataSource = myDataSource;
router.get('/', async (req : Request, res : Response):Promise<object> => {
const users = await dataSource.manager.find(User)
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.getMany()
return res.send({users})
})
......
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