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