Skip to content

Commit

Permalink
Merge pull request #78 from FLiotta/server/validation-on-moving-task
Browse files Browse the repository at this point in the history
Added validation to move task endpoint, close create task after reque…
  • Loading branch information
FLiotta authored Oct 23, 2020
2 parents 8a371b9 + 89a643e commit e14717c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
**/.pytest_cache
server/.env
server/env
.pylintrc

# Client

Expand Down
16 changes: 11 additions & 5 deletions client/src/components/CreateTask/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ const CreateTask = ({ addTask, listId }: CreateTaskProps): JSX.Element => {
const clickOutsideRef = useOnclickOutside(() => setIsActive(false));

const onSubmit = ({ title }: CreateTaskForm): void => {
addTask(title, listId);
trackEvent({
category: 'Tasks',
action: 'Task created'
});
setIsLoading(true);

addTask(title, listId)
.then(() => {
setIsLoading(false);
setIsActive(false);
trackEvent({
category: 'Tasks',
action: 'Task created'
});
});
};

return (
Expand Down
1 change: 1 addition & 0 deletions server/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Lists(db.Model):
board_id = Column(Integer, ForeignKey('boards.id'))
title = Column(VARCHAR(100))
tasks = relationship('Tasks', cascade="all, delete", backref="list", lazy="joined")
createdat = Column(Integer, default=int(datetime.datetime.now().timestamp()))

def __init__(self, board_id, user_id, title):
self.board_id = board_id
Expand Down
5 changes: 4 additions & 1 deletion server/app/routes/board_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ def get_board(board_id):
temp_list = {
"id": list_.id,
"title": list_.title,
"createdat": list_.createdat,
"tasks": list(map(lambda t: {
'id': t.id,
'title': t.title,
'priority': t.priority.value if t.priority else None,
'position': t.position,
}, list_.tasks))
}

temp_list["tasks"] = sorted(temp_list["tasks"], key=itemgetter('position'))
board_lists.append(temp_list)

board_lists = sorted(board_lists, key=itemgetter('createdat'))

response = {
'id': board.id,
'title': board.title,
Expand Down
28 changes: 16 additions & 12 deletions server/app/routes/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,30 @@ def delete_task(task_id):
@protected_route
def update_task_list(task_id):
req_data = request.get_json()
list_id = int(req_data.get('listId'))
new_position = int(req_data.get('position'))
list_id = req_data.get('listId')
new_position = req_data.get('position')
user_id = g.user.get('id')

if list_id is None or task_id is None or new_position is None:
return jsonify(msg="Missing params"), 400

tasks = Tasks.query.options(noload('priority')).filter((Tasks.list_id==list_id) | (Tasks.id==task_id)).order_by(Tasks.position).all()
list_id = int(list_id)
new_position = int(new_position)

temp_task = None
results = db.session.query(Lists, Tasks)\
.filter(Lists.id==list_id)\
.join(Tasks, Tasks.id==task_id)\
.first_or_404()

for i in range(len(tasks)):
if tasks[i].id == int(task_id):
tasks[i].list_id = list_id
temp_task = tasks.pop(i)
break

tasks.insert(new_position, temp_task)
if user_id != results[0].user_id:
return jsonify(msg="You can't perform this action."), 403

for i, task in enumerate(tasks):
order = [x for x in results[0].tasks]
order.insert(new_position, results[1])

results[1].list_id = list_id

for i, task in enumerate(order):
task.position = i

db.session.commit()
Expand Down

0 comments on commit e14717c

Please sign in to comment.