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 var get
= request
.get_args
45 var rq
= url
.split
("/")
46 if rq
.has
("meetup_create") then
47 var ansset
= new HashSet[String]
48 var ans_tmp
= "answer_"
51 var anss
= request
.string_arg
(ans_tmp
+ cnt
.to_s
)
53 if anss
== null then break
54 if ansset
.has
(anss
) then continue
58 var mname
= request
.string_arg
("meetup_name")
59 var mdate
= request
.string_arg
("meetup_date")
60 var mplace
= request
.string_arg
("meetup_place")
61 var mmodestr
= request
.string_arg
("meetup_mode")
63 if mdate
== null then mdate
= ""
64 if mplace
== null then mplace
= ""
65 if mmodestr
!= null then
70 var rsp
= new HttpResponse(200)
71 var meetpage
= new MeetupCreationPage
72 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
75 meetpage
.error
= "'Meetup name' is a mandatory fields."
76 rsp
.body
= meetpage
.write_to_string
80 var db
= new OpportunityDB.open
(db_path
)
81 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
82 if ansset
.is_empty
then
84 var rsp
= new HttpResponse(200)
85 var meetpage
= new MeetupCreationPage
87 meetpage
.error
= "You need to input at least one answer."
88 rsp
.body
= meetpage
.write_to_string
92 var success
= meet
.commit
(db
)
95 var rsp
= new HttpResponse(500)
96 var meetpage
= new MeetupCreationPage
100 <p>Failed to create event</p>
101 <p>This is a server side error, it has been logged.
102 You may still want to contact the maintainers of this website.</p>"""
103 rsp
.body
= meetpage
.write_to_string
108 var ans
= new Answer(v
)
113 var rsp
= new HttpResponse(200)
114 rsp
.body
= (new MeetupConfirmation(meet
)).write_to_string
117 if rq
.has
("new_meetup") then
118 var rsp
= new HttpResponse(200)
119 var page
= new MeetupCreationPage
120 rsp
.body
= page
.write_to_string
123 if get
.has_key
("meetup_id") then
124 var rsp
= new HttpResponse(200)
125 rsp
.body
= (new OpportunityMeetupPage.from_id
(get
["meetup_id"])).write_to_string
128 var rsp
= new HttpResponse(200)
129 rsp
.body
= (new OpportunityHomePage).write_to_string
135 # Any kind of REST request to Opportunity
136 class OpportunityRESTAction
137 super OpportunityAction
139 redef fun answer
(request
, uri
) do
140 var req
= uri
.split
("/")
141 if req
.has
("people") then
142 return (new OpportunityPeopleREST).answer
(request
, uri
)
143 else if req
.has
("answer") then
144 return (new OpportunityAnswerREST).answer
(request
, uri
)
145 else if req
.has
("meetup") then
146 return (new OpportunityMeetupREST).answer
(request
, uri
)
148 return new HttpResponse(400)
154 # REST Actions working on People
155 class OpportunityPeopleREST
156 super OpportunityAction
158 redef fun answer
(request
, uri
) do
159 # Should be DELETE for true REST API
160 # TODO : change method to DELETE once supported by Nitcorn
161 if request
.method
== "POST" then
162 var meth
= request
.string_arg
("method")
163 if meth
== null then return bad_req
164 if meth
!= "DELETE" then return bad_req
165 var pid
= request
.int_arg
("p_id")
166 if pid
== null then return bad_req
167 var db
= new OpportunityDB.open
(db_path
)
168 db
.remove_people_by_id
(pid
)
170 return new HttpResponse(200)
172 return new HttpResponse(400)
177 # REST Actions working on Answers
178 class OpportunityAnswerREST
179 super OpportunityAction
181 redef fun answer
(request
, uri
) do
182 var persid
= request
.int_arg
("pers_id")
183 var ansid
= request
.int_arg
("answer_id")
184 var ans
= request
.int_arg
("answer")
185 if persid
== null or ansid
== null or ans
== null then return bad_req
186 var db
= new OpportunityDB.open
(db_path
)
187 db
.change_answer
(ansid
, persid
, ans
)
189 return new HttpResponse(200)
193 # REST Actions working on Meetups
194 class OpportunityMeetupREST
195 super OpportunityAction
197 redef fun answer
(request
, uri
) do
198 var args
= uri
.split
("/")
199 if args
.has
("new_pers") then
200 var name
= request
.string_arg
("persname")
201 var m_id
= request
.string_arg
("meetup_id")
202 var ans_str
= request
.string_arg
("answers")
203 if name
== null or m_id
== null or ans_str
== null then return bad_req
204 var ans
= ans_str
.split
("&")
205 var ansmap
= new HashMap[Int, Int]
207 var mp
= i
.split
("=")
208 var id
= mp
.first
.split
("_").last
210 if not id
.is_numeric
then continue
213 var db
= new OpportunityDB.open
(db_path
)
214 var m
= db
.find_meetup_by_id
(m_id
)
215 var sublen
= name
.index_of
(' ')
221 rsurname
= name
.substring
(0, sublen
)
222 rname
= name
.substring_from
(sublen
+ 1)
224 var p
= new People(rname
, rsurname
)
225 for i
in m
.answers
(db
) do
226 if not ansmap
.has_key
(i
.id
) then
229 p
.answers
[i
] = ansmap
[i
.id
]
234 var rsp
= new HttpResponse(200)
238 return new HttpResponse(400)