tests: update test_ffi_java_types to be more portable
[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 ansset = new HashSet[String]
50 var ans_tmp = "answer_"
51 var cnt = 1
52 loop
53 var anss = request.string_arg(ans_tmp + cnt.to_s)
54 cnt += 1
55 if anss == null then break
56 if ansset.has(anss) then continue
57 ansset.add anss
58 end
59
60 var mname = request.string_arg("meetup_name")
61 var mdate = request.string_arg("meetup_date")
62 var mplace = request.string_arg("meetup_place")
63 if mdate == null then mdate = ""
64 if mplace == null then mplace = ""
65 if mname == null then
66 mname = ""
67 var rsp = new HttpResponse(200)
68 var meetpage = new MeetupCreationPage
69 var meet = new Meetup(mname, mdate, mplace)
70 meetpage.ans = ansset
71 meetpage.meet = meet
72 meetpage.error = "'Meetup name' is a mandatory fields."
73 rsp.body = meetpage.write_to_string
74 return rsp
75
76 end
77 var db = new OpportunityDB.open(db_path)
78 var meet = new Meetup(mname, mdate, mplace)
79 if ansset.is_empty then
80 db.close
81 var rsp = new HttpResponse(200)
82 var meetpage = new MeetupCreationPage
83 meetpage.meet = meet
84 meetpage.error = "You need to input at least one answer."
85 rsp.body = meetpage.write_to_string
86 return rsp
87 end
88 if not meet.commit(db) then
89 db.close
90 var rsp = new HttpResponse(200)
91 var meetpage = new MeetupCreationPage
92 meetpage.meet = meet
93 meetpage.ans = ansset
94 meetpage.error = """<p>Could not create Meetup.</p>
95 <p>Hmm, that's embarassing, the database indicates that your meetup already exists.</p>
96 <p>If this is not a duplicated submission, please contact the mainainers of the website, you might have found a bug !</p>"""
97 rsp.body = meetpage.write_to_string
98 return rsp
99 end
100 for v in ansset do
101 var ans = new Answer(v)
102 ans.meetup = meet
103 ans.commit(db)
104 end
105 db.close
106 var rsp = new HttpResponse(200)
107 rsp.body = (new MeetupConfirmation(meet)).write_to_string
108 return rsp
109 end
110 if rq.has("new_meetup") then
111 var rsp = new HttpResponse(200)
112 var page = new MeetupCreationPage
113 rsp.body = page.write_to_string
114 return rsp
115 end
116 if get.has_key("meetup_id") then
117 var rsp = new HttpResponse(200)
118 rsp.body = (new OpportunityMeetupPage.from_id(get["meetup_id"])).write_to_string
119 return rsp
120 end
121 var rsp = new HttpResponse(200)
122 rsp.body = (new OpportunityHomePage).write_to_string
123 return rsp
124 end
125
126 end
127
128 # Any kind of REST request to Opportunity
129 class OpportunityRESTAction
130 super OpportunityAction
131
132 redef fun answer(request, uri) do
133 print "Received REST request from {uri}"
134 var get = request.get_args
135 var req = uri.split("/")
136 if req.has("people") then
137 return (new OpportunityPeopleREST).answer(request, uri)
138 else if req.has("answer") then
139 return (new OpportunityAnswerREST).answer(request, uri)
140 else if req.has("meetup") then
141 return (new OpportunityMeetupREST).answer(request, uri)
142 else
143 return new HttpResponse(400)
144 end
145 end
146
147 end
148
149 # REST Actions working on People
150 class OpportunityPeopleREST
151 super OpportunityAction
152
153 redef fun answer(request, uri) do
154 # Should be DELETE for true REST API
155 # TODO : change method to DELETE once supported by Nitcorn
156 if request.method == "POST" then
157 var meth = request.string_arg("method")
158 if meth == null then return bad_req
159 if meth != "DELETE" then return bad_req
160 var pid = request.int_arg("p_id")
161 if pid == null then return bad_req
162 var db = new OpportunityDB.open(db_path)
163 db.remove_people_by_id(pid)
164 db.close
165 return new HttpResponse(200)
166 end
167 return new HttpResponse(400)
168 end
169
170 end
171
172 # REST Actions working on Answers
173 class OpportunityAnswerREST
174 super OpportunityAction
175
176 redef fun answer(request, uri) do
177 var persid = request.int_arg("pers_id")
178 var ansid = request.int_arg("answer_id")
179 var ans = request.bool_arg("answer")
180 if persid == null or ansid == null or ans == null then return bad_req
181 var db = new OpportunityDB.open(db_path)
182 db.change_answer(ansid, persid, ans)
183 db.close
184 return new HttpResponse(200)
185 end
186 end
187
188 # REST Actions working on Meetups
189 class OpportunityMeetupREST
190 super OpportunityAction
191
192 redef fun answer(request, uri) do
193 var args = uri.split("/")
194 if args.has("new_pers") then
195 var name = request.string_arg("persname")
196 var m_id = request.string_arg("meetup_id")
197 var ans = request.string_arg("answers").split("&")
198 if name == null or m_id == null then return bad_req
199 print ans
200 var ansmap = new HashMap[Int, Bool]
201 for i in ans do
202 var mp = i.split("=")
203 var b = false
204 if mp.last == "true" then b = true
205 var id = mp.first.split("_").last
206 if not id.is_numeric then continue
207 ansmap[id.to_i] = b
208 end
209 var db = new OpportunityDB.open(db_path)
210 var m = db.find_meetup_by_id(m_id)
211 var sublen = name.index_of(' ')
212 var rname = ""
213 var rsurname = ""
214 if sublen == -1 then
215 rsurname = name
216 else
217 rsurname = name.substring(0, sublen)
218 rname = name.substring_from(sublen + 1)
219 end
220 var p = new People(rname, rsurname)
221 for i in m.answers(db) do
222 if not ansmap.has_key(i.id) then
223 p.answers[i] = false
224 else
225 p.answers[i] = ansmap[i.id]
226 end
227 end
228 p.commit(db)
229 db.close
230 var rsp = new HttpResponse(200)
231 rsp.body = p.id.to_s
232 return rsp
233 end
234 return new HttpResponse(400)
235 end
236 end