Merge branch 'task-33-feature/put_delete_tasks' into 'development'

#33 added put and update routers/tasks

See merge request !16
parents 15ec7a8c c7557dfc
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
import {Project} from './Project'; import {Project} from './Project';
type taskFinishType = "open" | "done" |"failed"; type taskFinishType = "open" | "done" |"failed";
type priorityType = "A" | "B" |"C";
interface ITask{ interface ITask{
id: string; id: string;
...@@ -21,6 +22,7 @@ import { ...@@ -21,6 +22,7 @@ import {
dateTimeStart:Date| null; dateTimeStart:Date| null;
dateTimeDue:Date| null; dateTimeDue:Date| null;
accomplish: taskFinishType; accomplish: taskFinishType;
priority: priorityType;
author: User; author: User;
project:Project|null; project:Project|null;
executors:User[] executors:User[]
...@@ -48,10 +50,15 @@ import { ...@@ -48,10 +50,15 @@ import {
}) })
accomplish!: taskFinishType accomplish!: taskFinishType
@Column({ name: 'dateTimeStart', type: Date,nullable: true })
dateTimeStart!: Date | null; @Column({
@Column({ name: 'dateTimeDue', type: Date,nullable: true }) type: "enum",
dateTimeDue!: Date | null; enum: ["A", "B" , "C"],
default: "C"
})
priority!: priorityType
@ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true}) @ManyToOne(() => User, (user: { tasks: Task[]; }) => user.tasks,{eager : true})
author!: User; author!: User;
......
...@@ -41,26 +41,26 @@ interface IUser { ...@@ -41,26 +41,26 @@ interface IUser {
@Entity({ name: 'User' }) @Entity({ name: 'User' })
export class User extends BaseEntity implements IUser { export class User extends BaseEntity implements IUser {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn("uuid")
id!: string id!: string;
@Column({ name: 'name', type: 'varchar', length:20,nullable: false }) @Column({ name: 'name', type: 'varchar', length:20,nullable: false })
name!: string name!: string;
@Column({ name: 'surname', type: 'varchar', length:30,nullable: false }) @Column({ name: 'surname', type: 'varchar', length:30,nullable: false })
surname!: string surname!: string;
@Column({ name: 'displayName', type: 'varchar', length:30,nullable: false }) @Column({ name: 'displayName', type: 'varchar', length:30,nullable: false })
displayName!: string displayName!: string;
@Column({ name: 'email', type: 'varchar',length:20, unique: true, nullable: false }) @Column({ name: 'email', type: 'varchar',length:20, unique: true, nullable: false })
@IsEmail() @IsEmail()
email!: string email!: string;
@Column({ name: 'phone', type: 'varchar',length:10, unique: true, nullable: true}) @Column({ name: 'phone', type: 'varchar',length:10, unique: true, nullable: true})
phone?: string phone?: string;
@Column({ name: 'token', type: 'varchar',length:100, unique: true, nullable: false }) @Column({ name: 'token', type: 'varchar',length:100, unique: true, nullable: false })
token!: string token!: string;
@CreateDateColumn({ name: 'created_at', type: Date, default: new Date() }) @CreateDateColumn({ name: 'created_at', type: Date, default: new Date() })
createdAt!: Date; createdAt!: Date;
...@@ -74,7 +74,7 @@ export class User extends BaseEntity implements IUser { ...@@ -74,7 +74,7 @@ export class User extends BaseEntity implements IUser {
@Column({ type: 'varchar', nullable: false, select:true }) @Column({ type: 'varchar', nullable: false, select:true })
@Exclude({ toPlainOnly: true }) @Exclude({ toPlainOnly: true })
password!: string password!: string;
@OneToMany(() => Task, (task: { user: User }) => task.user) @OneToMany(() => Task, (task: { user: User }) => task.user)
......
...@@ -17,7 +17,7 @@ export default router; ...@@ -17,7 +17,7 @@ export default router;
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} = req.body; const {title,description,project,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("user") .select("user")
...@@ -33,11 +33,12 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{ ...@@ -33,11 +33,12 @@ router.post('/', async(req:Request, res:Response):Promise<Response>=>{
newTask.dateTimeStart = dateTimeStart; newTask.dateTimeStart = dateTimeStart;
newTask.author= user; newTask.author= user;
newTask.accomplish = accomplish; newTask.accomplish = accomplish;
newTask.priority = priority;
await newTask.save(); await newTask.save();
return res.send({newTask}) return res.send({newTask})
}) })
router.get('/userId/:userId', async (req: Request, res: Response)=>{ router.get('/userId/:userId', async (req: Request, res: Response):Promise<Response>=>{
const userId = req.params.userId; const userId = req.params.userId;
const tasks = await dataSource const tasks = await dataSource
.getRepository(Task) .getRepository(Task)
...@@ -48,8 +49,7 @@ router.get('/userId/:userId', async (req: Request, res: Response)=>{ ...@@ -48,8 +49,7 @@ router.get('/userId/:userId', async (req: Request, res: Response)=>{
return res.send({tasks}) return res.send({tasks})
}) })
router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
router.get('/my', async (req: Request, res: Response)=>{
const token = req.get('Authorization'); const token = req.get('Authorization');
const user = await dataSource const user = await dataSource
.createQueryBuilder() .createQueryBuilder()
...@@ -66,3 +66,55 @@ router.get('/my', async (req: Request, res: Response)=>{ ...@@ -66,3 +66,55 @@ router.get('/my', async (req: Request, res: Response)=>{
.getMany() .getMany()
return res.send({tasks}) return res.send({tasks})
}) })
router.delete('/:taskId',async (req: Request, res: Response):Promise<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 taskId = req.params.taskId;
await myDataSource
.createQueryBuilder()
.delete()
.from(Task)
.where("id = :id", { id: taskId })
.execute()
return res.send({message: 'Task deleted successfully'})
})
router.put('/',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 {id,title,description,project,executors,dateTimeDue,dateTimeStart,accomplish,priority} = req.body;
await dataSource
.createQueryBuilder()
.update(Task)
.set({
title: title,
description: description,
project: project,
executors: executors,
dateTimeDue: dateTimeDue,
dateTimeStart: dateTimeStart,
author:user,
accomplish: accomplish,
priority: priority
})
.where("id = :id", { id: id })
.execute()
res.send({message:'update task successfully'})
// res.send({task})
})
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