868dd9e932010d8aeac4429a18db5c7e09cbb864
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
21 import opportunity_model
23 # Any kind of opportunity `Action` (serves a request)
24 abstract class OpportunityAction
28 var db_path
= "opportunity"
30 # Returns a bad request with an error code 400
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
40 # Welcome page for Opportunity
41 class OpportunityWelcome
42 super OpportunityAction
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_"
53 var anss
= request
.string_arg
(ans_tmp
+ cnt
.to_s
)
55 if anss
== null then break
56 if ansset
.has
(anss
) then continue
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 var mmodestr
= request
.string_arg
("meetup_mode")
65 if mdate
== null then mdate
= ""
66 if mplace
== null then mplace
= ""
67 if mmodestr
!= null then
72 var rsp
= new HttpResponse(200)
73 var meetpage
= new MeetupCreationPage
74 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
77 meetpage
.error
= "'Meetup name' is a mandatory fields."
78 rsp
.body
= meetpage
.write_to_string
82 var db
= new OpportunityDB.open
(db_path
)
83 var meet
= new Meetup(mname
, mdate
, mplace
, mmode
)
84 if ansset
.is_empty
then
86 var rsp
= new HttpResponse(200)
87 var meetpage
= new MeetupCreationPage
89 meetpage
.error
= "You need to input at least one answer."
90 rsp
.body
= meetpage
.write_to_string
93 if not meet
.commit
(db
) then
95 var rsp
= new HttpResponse(200)
96 var meetpage
= new MeetupCreationPage
99 meetpage
.error
= """<p>Could not create Meetup.</p>
100 <p>Hmm, that's embarassing, the database indicates that your meetup already exists.</p>
101 <p>If this is not a duplicated submission, please contact the mainainers of the website, you might have found a bug !</p>"""
102 rsp
.body
= meetpage
.write_to_string
106 var ans
= new Answer(v
)
111 var rsp
= new HttpResponse(200)
112 rsp
.body
= (new MeetupConfirmation(meet
)).write_to_string
115 if rq
.has
("new_meetup") then
116 var rsp
= new HttpResponse(200)
117 var page
= new MeetupCreationPage
118 rsp
.body
= page
.write_to_string
121 if get
.has_key
("meetup_id") then
122 var rsp
= new HttpResponse(200)
123 rsp
.body
= (new OpportunityMeetupPage.from_id
(get
["meetup_id"])).write_to_string
126 var rsp
= new HttpResponse(200)
127 rsp
.body
= (new OpportunityHomePage).write_to_string
133 # Any kind of REST request to Opportunity
134 class OpportunityRESTAction
135 super OpportunityAction
137 redef fun answer
(request
, uri
) do
138 print
"Received REST request from {uri}"
139 var req
= uri
.split
("/")
140 if req
.has
("people") then
141 return (new OpportunityPeopleREST).answer
(request
, uri
)
142 else if req
.has
("answer") then
143 return (new OpportunityAnswerREST).answer
(request
, uri
)
144 else if req
.has
("meetup") then
145 return (new OpportunityMeetupREST).answer
(request
, uri
)
147 return new HttpResponse(400)
153 # REST Actions working on People
154 class OpportunityPeopleREST
155 super OpportunityAction
157 redef fun answer
(request
, uri
) do
158 # Should be DELETE for true REST API
159 # TODO : change method to DELETE once supported by Nitcorn
160 if request
.method
== "POST" then
161 var meth
= request
.string_arg
("method")
162 if meth
== null then return bad_req
163 if meth
!= "DELETE" then return bad_req
164 var pid
= request
.int_arg
("p_id")
165 if pid
== null then return bad_req
166 var db
= new OpportunityDB.open
(db_path
)
167 db
.remove_people_by_id
(pid
)
169 return new HttpResponse(200)
171 return new HttpResponse(400)
176 # REST Actions working on Answers
177 class OpportunityAnswerREST
178 super OpportunityAction
180 redef fun answer
(request
, uri
) do
181 var persid
= request
.int_arg
("pers_id")
182 var ansid
= request
.int_arg
("answer_id")
183 var ans
= request
.int_arg
("answer")
184 if persid
== null or ansid
== null or ans
== null then return bad_req
185 var db
= new OpportunityDB.open
(db_path
)
186 db
.change_answer
(ansid
, persid
, ans
)
188 return new HttpResponse(200)
192 # REST Actions working on Meetups
193 class OpportunityMeetupREST
194 super OpportunityAction
196 redef fun answer
(request
, uri
) do
197 var args
= uri
.split
("/")
198 if args
.has
("new_pers") then
199 var name
= request
.string_arg
("persname")
200 var m_id
= request
.string_arg
("meetup_id")
201 var ans_str
= request
.string_arg
("answers")
202 if name
== null or m_id
== null or ans_str
== null then return bad_req
203 var ans
= ans_str
.split
("&")
204 var ansmap
= new HashMap[Int, Int]
206 var mp
= i
.split
("=")
207 var id
= mp
.first
.split
("_").last
209 if not id
.is_numeric
then continue
212 var db
= new OpportunityDB.open
(db_path
)
213 var m
= db
.find_meetup_by_id
(m_id
)
214 var sublen
= name
.index_of
(' ')
220 rsurname
= name
.substring
(0, sublen
)
221 rname
= name
.substring_from
(sublen
+ 1)
223 var p
= new People(rname
, rsurname
)
224 for i
in m
.answers
(db
) do
225 if not ansmap
.has_key
(i
.id
) then
228 p
.answers
[i
] = ansmap
[i
.id
]
233 var rsp
= new HttpResponse(200)
237 return new HttpResponse(400)