1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
15 # Actions for Opportunity web application
16 module opportunity_controller
20 import opportunity_model
22 # Any kind of opportunity `Action` (serves a request)
23 abstract class OpportunityAction
27 var db_path
= "opportunity"
29 # Returns a bad request with an error code 400
31 # TODO: Add a specific body to the bad request page.
32 fun bad_req
: HttpResponse do
33 var rsp
= new HttpResponse(400)
34 rsp
.body
= (new OpportunityHomePage).write_to_string
39 # Welcome page for Opportunity
40 class OpportunityWelcome
41 super OpportunityAction
43 redef fun answer
(request
, url
) do
44 print
"Received request for {url}"
45 var get
= request
.get_args
46 var rq
= url
.split
("/")
47 if rq
.has
("meetup_create") then
48 var ansset
= new HashSet[String]
49 var ans_tmp
= "answer_"
52 var anss
= request
.string_arg
(ans_tmp
+ cnt
.to_s
)
54 if anss
== null then break
55 if ansset
.has
(anss
) then continue
59 var mname
= request
.string_arg
("meetup_name")
60 var mdate
= request
.string_arg
("meetup_date")
61 var mplace
= request
.string_arg
("meetup_place")
62 var mmodestr
= request
.string_arg
("meetup_mode")
64 if mdate
== null then mdate
= ""
65 if mplace
== null then mplace
= ""
66 if mmodestr
!= null then
71 var rsp
= new HttpResponse(200)
72 var meetpage
= new MeetupCreationPage
73 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
76 meetpage
.error
= "'Meetup name' is a mandatory fields."
77 rsp
.body
= meetpage
.write_to_string
81 var db
= new OpportunityDB.open
(db_path
)
82 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
83 if ansset
.is_empty
then
85 var rsp
= new HttpResponse(200)
86 var meetpage
= new MeetupCreationPage
88 meetpage
.error
= "You need to input at least one answer."
89 rsp
.body
= meetpage
.write_to_string
92 if not meet
.commit
(db
) then
94 var rsp
= new HttpResponse(200)
95 var meetpage
= new MeetupCreationPage
98 meetpage
.error
= """<p>Could not create Meetup.</p>
99 <p>Hmm, that's embarassing, the database indicates that your meetup already exists.</p>
100 <p>If this is not a duplicated submission, please contact the mainainers of the website, you might have found a bug !</p>"""
101 rsp
.body
= meetpage
.write_to_string
105 var ans
= new Answer(v
)
110 var rsp
= new HttpResponse(200)
111 rsp
.body
= (new MeetupConfirmation(meet
)).write_to_string
114 if rq
.has
("new_meetup") then
115 var rsp
= new HttpResponse(200)
116 var page
= new MeetupCreationPage
117 rsp
.body
= page
.write_to_string
120 if get
.has_key
("meetup_id") then
121 var rsp
= new HttpResponse(200)
122 rsp
.body
= (new OpportunityMeetupPage.from_id
(get
["meetup_id"])).write_to_string
125 var rsp
= new HttpResponse(200)
126 rsp
.body
= (new OpportunityHomePage).write_to_string
132 # Any kind of REST request to Opportunity
133 class OpportunityRESTAction
134 super OpportunityAction
136 redef fun answer
(request
, uri
) do
137 print
"Received REST request from {uri}"
138 var req
= uri
.split
("/")
139 if req
.has
("people") then
140 return (new OpportunityPeopleREST).answer
(request
, uri
)
141 else if req
.has
("answer") then
142 return (new OpportunityAnswerREST).answer
(request
, uri
)
143 else if req
.has
("meetup") then
144 return (new OpportunityMeetupREST).answer
(request
, uri
)
146 return new HttpResponse(400)
152 # REST Actions working on People
153 class OpportunityPeopleREST
154 super OpportunityAction
156 redef fun answer
(request
, uri
) do
157 # Should be DELETE for true REST API
158 # TODO : change method to DELETE once supported by Nitcorn
159 if request
.method
== "POST" then
160 var meth
= request
.string_arg
("method")
161 if meth
== null then return bad_req
162 if meth
!= "DELETE" then return bad_req
163 var pid
= request
.int_arg
("p_id")
164 if pid
== null then return bad_req
165 var db
= new OpportunityDB.open
(db_path
)
166 db
.remove_people_by_id
(pid
)
168 return new HttpResponse(200)
170 return new HttpResponse(400)
175 # REST Actions working on Answers
176 class OpportunityAnswerREST
177 super OpportunityAction
179 redef fun answer
(request
, uri
) do
180 var persid
= request
.int_arg
("pers_id")
181 var ansid
= request
.int_arg
("answer_id")
182 var ans
= request
.int_arg
("answer")
183 if persid
== null or ansid
== null or ans
== null then return bad_req
184 var db
= new OpportunityDB.open
(db_path
)
185 db
.change_answer
(ansid
, persid
, ans
)
187 return new HttpResponse(200)
191 # REST Actions working on Meetups
192 class OpportunityMeetupREST
193 super OpportunityAction
195 redef fun answer
(request
, uri
) do
196 var args
= uri
.split
("/")
197 if args
.has
("new_pers") then
198 var name
= request
.string_arg
("persname")
199 var m_id
= request
.string_arg
("meetup_id")
200 var ans_str
= request
.string_arg
("answers")
201 if name
== null or m_id
== null or ans_str
== null then return bad_req
202 var ans
= ans_str
.split
("&")
203 var ansmap
= new HashMap[Int, Int]
205 var mp
= i
.split
("=")
206 var id
= mp
.first
.split
("_").last
208 if not id
.is_numeric
then continue
211 var db
= new OpportunityDB.open
(db_path
)
212 var m
= db
.find_meetup_by_id
(m_id
)
213 var sublen
= name
.index_of
(' ')
219 rsurname
= name
.substring
(0, sublen
)
220 rname
= name
.substring_from
(sublen
+ 1)
222 var p
= new People(rname
, rsurname
)
223 for i
in m
.answers
(db
) do
224 if not ansmap
.has_key
(i
.id
) then
227 p
.answers
[i
] = ansmap
[i
.id
]
232 var rsp
= new HttpResponse(200)
236 return new HttpResponse(400)