-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_api.py
108 lines (101 loc) · 3.65 KB
/
board_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, jsonify, request, session
from flask_restful import reqparse, abort, Api, Resource
from config import config
from models import db, Board
import json
parser = reqparse.RequestParser()
parser.add_argument("id")
parser.add_argument("name")
# parser.add_argument("user_id")
class board(Resource):
def get(self):
boardlist = Board.query.all()
#https://stackoverflow.com/questions/7102754/jsonify-a-sqlalchemy-result-set-in-flask
#결과를 jsonify 할 수 있게 만들어야됨
result = [b.to_dict() for b in boardlist]
response = {
"status": "success",
"result": result,
"message": "printing board list"
}
return jsonify(response)
def post(self):
args = parser.parse_args()
boardname = args["name"]
manager = session.get("user")
if manager:
new_board = Board(boardname, manager)
db.session.add(new_board)
db.session.commit()
response ={
"status": "success",
"result": {"boardname": boardname},
"message": "new board is added"
}
return jsonify(response)
else:
response ={
"status": "error",
"message": "you have to login first to create a board"
}
return jsonify(response)
def put(self):
args = parser.parse_args()
board_id = args["id"]
boardname = args["name"]
manager = session.get("user")
if manager:
current_board = Board.query.filter_by(id = board_id).first()
if current_board.user_id == manager:
current_board.boardname = boardname
db.session.commit()
response ={
"status": "success",
"result": {
"id":board_id,
"name": boardname
},
"message": "successfully changed board name"
}
return jsonify(response)
else:
response={
"status": "error",
"message": "the user is not allowed to update current board"
}
return jsonify(response)
else:
response ={
"status": "error",
"message": "you have to login first to change board name"
}
return jsonify(response)
def delete(self):
args = parser.parse_args()
board_id = args["id"]
manager = session.get("user")
if manager:
current_board = Board.query.filter_by(id = board_id).first()
if current_board.user_id == manager:
Board.query.filter_by(id=board_id).delete()
db.session.commit()
response ={
"status": "success",
"result": {
"id":board_id,
},
"message": "successfully deleted"
}
return jsonify(response)
else:
response={
"status": "error",
"message": "the user is not allowed to delete current board"
}
return jsonify(response)
else:
response ={
"status": "error",
"message": "you have to login first to delete board"
}
return jsonify(response)