Merge: Robust debug
[nit.git] / contrib / opportunity / src / opportunity_controller.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License
14
15 # Actions for Opportunity web application
16 module opportunity_controller
17
18 import nitcorn
19 import sha1
20 import templates
21 import opportunity_model
22
23 # Any kind of opportunity `Action` (serves a request)
24 abstract class OpportunityAction
25 super Action
26
27 # Path to db
28 var db_path = "opportunity"
29
30 # Returns a bad request with an error code 400
31 #
32 # TODO: Add a specific body to the bad request page.
33 fun bad_req: HttpResponse do
34 var rsp = new HttpResponse(400)
35 rsp.body = (new OpportunityHomePage).write_to_string
36 return rsp
37 end
38 end
39
40 # Welcome page for Opportunity
41 class OpportunityWelcome
42 super OpportunityAction
43
44 redef fun answer(request, url) do
45 print "Received request for {url}"
46 var get = request.get_args
47 var rq = url.split("/")
48 if rq.has("meetup_create") then
49 var mname = request.string_arg("meetup_name")
50 var mdate = request.string_arg("meetup_date")
51 var mplace = request.string_arg("meetup_place")
52 if mname == null or mdate == null or mplace == null then return bad_req
53 var db = new OpportunityDB.open(db_path)
54 var meet = new Meetup(mname, mdate, mplace)
55 if meet == null then
56 db.close
57 return bad_req
58 end
59 meet.commit(db)
60 var ans_tmp = "answer_"
61 var cnt = 1
62 loop
63 var anss = request.string_arg(ans_tmp + cnt.to_s)
64 if anss == null then break
65 var ans = new Answer(anss)
66 ans.meetup = meet
67 ans.commit(db)
68 cnt += 1
69 end
70 db.close
71 var rsp = new HttpResponse(200)
72 if meet.id == "" then
73 rsp.body = (new MeetupCreationPage).write_to_string
74 else
75 rsp.body = (new MeetupConfirmation(meet)).write_to_string
76 end
77 return rsp
78 end
79 if rq.has("new_meetup") then
80 var rsp = new HttpResponse(200)
81 var page = new MeetupCreationPage
82 rsp.body = page.write_to_string
83 return rsp
84 end
85 if get.has_key("meetup_id") then
86 var rsp = new HttpResponse(200)
87 rsp.body = (new OpportunityMeetupPage.from_id(get["meetup_id"])).write_to_string
88 return rsp
89 end
90 var rsp = new HttpResponse(200)
91 rsp.body = (new OpportunityHomePage).write_to_string
92 return rsp
93 end
94
95 end
96
97 # Any kind of REST request to Opportunity
98 class OpportunityRESTAction
99 super OpportunityAction
100
101 redef fun answer(request, uri) do
102 print "Received REST request from {uri}"
103 var get = request.get_args
104 var req = uri.split("/")
105 if req.has("people") then
106 return (new OpportunityPeopleREST).answer(request, uri)
107 else if req.has("answer") then
108 return (new OpportunityAnswerREST).answer(request, uri)
109 else if req.has("meetup") then
110 return (new OpportunityMeetupREST).answer(request, uri)
111 else
112 return new HttpResponse(400)
113 end
114 end
115
116 end
117
118 # REST Actions working on People
119 class OpportunityPeopleREST
120 super OpportunityAction
121
122 redef fun answer(request, uri) do
123 # Should be DELETE for true REST API
124 # TODO : change method to DELETE once supported by Nitcorn
125 if request.method == "POST" then
126 var meth = request.string_arg("method")
127 if meth == null then return bad_req
128 if meth != "DELETE" then return bad_req
129 var pid = request.int_arg("p_id")
130 if pid == null then return bad_req
131 var db = new OpportunityDB.open(db_path)
132 db.remove_people_by_id(pid)
133 db.close
134 return new HttpResponse(200)
135 end
136 return new HttpResponse(400)
137 end
138
139 end
140
141 # REST Actions working on Answers
142 class OpportunityAnswerREST
143 super OpportunityAction
144
145 redef fun answer(request, uri) do
146 var persid = request.int_arg("pers_id")
147 var ansid = request.int_arg("answer_id")
148 var ans = request.bool_arg("answer")
149 if persid == null or ansid == null or ans == null then return bad_req
150 var db = new OpportunityDB.open(db_path)
151 db.change_answer(ansid, persid, ans)
152 db.close
153 return new HttpResponse(200)
154 end
155 end
156
157 # REST Actions working on Meetups
158 class OpportunityMeetupREST
159 super OpportunityAction
160
161 redef fun answer(request, uri) do
162 var args = uri.split("/")
163 if args.has("new_pers") then
164 var name = request.string_arg("persname")
165 var m_id = request.string_arg("meetup_id")
166 var ans = request.string_arg("answers").split("&")
167 if name == null or m_id == null then return bad_req
168 print ans
169 var ansmap = new HashMap[Int, Bool]
170 for i in ans do
171 var mp = i.split("=")
172 var b = false
173 if mp.last == "true" then b = true
174 var id = mp.first.split("_").last
175 if not id.is_numeric then continue
176 ansmap[id.to_i] = b
177 end
178 var db = new OpportunityDB.open(db_path)
179 var m = db.find_meetup_by_id(m_id)
180 var sublen = name.index_of(' ')
181 var rname = ""
182 var rsurname = ""
183 if sublen == -1 then
184 rsurname = name
185 else
186 rsurname = name.substring(0, sublen)
187 rname = name.substring_from(sublen + 1)
188 end
189 var p = new People(rname, rsurname)
190 for i in m.answers(db) do
191 if not ansmap.has_key(i.id) then
192 p.answers[i] = false
193 else
194 p.answers[i] = ansmap[i.id]
195 end
196 end
197 p.commit(db)
198 db.close
199 var rsp = new HttpResponse(200)
200 rsp.body = p.id.to_s
201 return rsp
202 end
203 return new HttpResponse(400)
204 end
205 end