#68 change create task router & added onDelete and eager options in DateTimeTask…

#68 change create task router & added onDelete and eager options in DateTimeTask ans Task models respectfully
parent b2296f46
...@@ -36,6 +36,6 @@ import { ...@@ -36,6 +36,6 @@ import {
dateTimeDue!: Date; dateTimeDue!: Date;
@ManyToOne(() => Task, (task: { dateTimeTasks: DateTimeTask[]; }) => task.dateTimeTasks,{cascade: true, onUpdate:'CASCADE',nullable:true}) @ManyToOne(() => Task, (task: { dateTimeTasks: DateTimeTask[]; }) => task.dateTimeTasks,{cascade: true, onUpdate:'CASCADE',onDelete:'CASCADE',nullable:true})
task!: Task; task!: Task;
} }
\ No newline at end of file
...@@ -80,6 +80,6 @@ import { DateTimeTask } from './DateTimeTask'; ...@@ -80,6 +80,6 @@ import { DateTimeTask } from './DateTimeTask';
@ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks,{eager : true,nullable: true,onUpdate:'CASCADE'}) @ManyToOne(()=>Project,(project:{tasks: Task[]}) => project.tasks,{eager : true,nullable: true,onUpdate:'CASCADE'})
project!: Project; project!: Project;
@OneToMany(() => DateTimeTask, (dateTimeTask: { task: Task }) => dateTimeTask.task) @OneToMany(() => DateTimeTask, (dateTimeTask: { task: Task }) => dateTimeTask.task,{eager : true,nullable: true,onUpdate:'CASCADE'})
dateTimeTasks!: DateTimeTask[]; dateTimeTasks!: DateTimeTask[];
} }
...@@ -4,6 +4,7 @@ import {myDataSource} from '../app-data-source'; ...@@ -4,6 +4,7 @@ import {myDataSource} from '../app-data-source';
import { User } from '../models/User'; import { User } from '../models/User';
import { Member } from '../models/Member'; import { Member } from '../models/Member';
import { In } from 'typeorm'; import { In } from 'typeorm';
import { DateTimeTask } from '../models/DateTimeTask';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -23,24 +24,34 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => { ...@@ -23,24 +24,34 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => {
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,dateTimeDue,dateTimeStart,accomplish,priority} = req.body; const {title,description,project,dateTimeTaskArray, dateTimeDeadLine,accomplish,priority} = req.body;
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
.from(User, "user") .from(User, "user")
.where("user.token = :token", { token: token }) .where("user.token = :token", { token: token })
.getOne() .getOne();
if (!user) return res.status(404).send({Message:'user not found'}) if (!user) return res.status(404).send({Message:'user not found'})
const dateTimeArray=[];
if(dateTimeTaskArray && dateTimeTaskArray.length>0){
for (let dateTimeStartDue of dateTimeTaskArray){
const newDateTimeTask = new DateTimeTask();
newDateTimeTask.dateTimeStart = dateTimeStartDue.dateTimeStart;
newDateTimeTask.dateTimeDue = dateTimeStartDue.dateTimeDue;
await newDateTimeTask.save();
dateTimeArray.push(newDateTimeTask);
}
}
newTask.title = title; newTask.title = title;
newTask.description = description; newTask.description = description;
newTask.project = project; newTask.project = project;
newTask.dateTimeDue = dateTimeDue; newTask.dateTimeTasks=dateTimeArray;
newTask.dateTimeStart = dateTimeStart; newTask.dateTimeDeadLine=dateTimeDeadLine;
newTask.author= user; newTask.author= user;
newTask.accomplish = accomplish; newTask.accomplish = accomplish;
newTask.priority = priority; newTask.priority = priority;
await newTask.save(); await newTask.save();
return res.send({newTask}) return res.send({newTask});
}) })
/**check tasks of specific user by userID */ /**check tasks of specific user by userID */
......
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