1+ package custom .application .v1 ;
2+
3+ import custom .objects .Feedback ;
4+ import custom .objects .User ;
5+ import custom .util .AuthenticationService ;
6+ import org .tinystruct .AbstractApplication ;
7+ import org .tinystruct .ApplicationException ;
8+ import org .tinystruct .data .component .Table ;
9+ import org .tinystruct .data .component .Builder ;
10+ import org .tinystruct .http .Request ;
11+ import org .tinystruct .http .Response ;
12+ import org .tinystruct .http .ResponseStatus ;
13+ import org .tinystruct .system .annotation .Action ;
14+
15+ import java .util .Date ;
16+
17+ public class feedback extends AbstractApplication {
18+ @ Override
19+ public void init () {
20+ }
21+
22+ @ Override
23+ public String version () {
24+ return "1.0" ;
25+ }
26+
27+ @ Action ("feedback/submit" )
28+ public String submit (Request request , Response response ) {
29+ String content = request .getParameter ("content" );
30+ if (content == null || content .trim ().isEmpty ()) {
31+ response .setStatus (ResponseStatus .BAD_REQUEST );
32+ return "{\" success\" :false,\" message\" :\" Feedback content is required.\" }" ;
33+ }
34+ Feedback feedback = new Feedback ();
35+ feedback .setContent (content .trim ());
36+ feedback .setCreatedAt (new Date ());
37+ Object userId = request .getSession ().getAttribute ("user_id" );
38+ if (userId != null ) {
39+ try {
40+ feedback .setUserId (Integer .parseInt (userId .toString ()));
41+ } catch (Exception ignored ) {
42+ }
43+ }
44+ try {
45+ boolean status = feedback .append ();
46+ return "{\" success\" :" + status + "}" ;
47+ } catch (Exception e ) {
48+ response .setStatus (ResponseStatus .INTERNAL_SERVER_ERROR );
49+ return "{\" success\" :false,\" message\" :\" Failed to save feedback.\" }" ;
50+ }
51+ }
52+
53+ @ Action ("feedback/list" )
54+ public String list (Request request , Response response ) throws ApplicationException {
55+ // Only admin can view
56+ Object userId = request .getSession ().getAttribute ("user_id" );
57+ if (userId == null ) {
58+ response .setStatus (ResponseStatus .UNAUTHORIZED );
59+ return "{\" error\" :\" not_authenticated\" }" ;
60+ }
61+ User user = AuthenticationService .getCurrentUser (userId .toString ());
62+ if (user == null || !user .getIsAdmin ()) {
63+ response .setStatus (ResponseStatus .FORBIDDEN );
64+ return "{\" error\" :\" forbidden\" }" ;
65+ }
66+ Feedback feedback = new Feedback ();
67+ Table all = feedback .orderBy (new String []{"created_at" }).findAll ();
68+ StringBuilder json = new StringBuilder ();
69+ json .append ("[\n " );
70+ for (int i = 0 ; i < all .size (); i ++) {
71+ Feedback f = new Feedback ();
72+ f .setData (all .get (i ));
73+ json .append (f );
74+ if (i < all .size () - 1 ) json .append (",\n " );
75+ }
76+ json .append ("\n ]" );
77+ return json .toString ();
78+ }
79+
80+ private String escapeJson (String s ) {
81+ return s == null ? "" : s .replace ("\\ " , "\\ \\ " ).replace ("\" " , "\\ \" " ).replace ("\n " , "\\ n" ).replace ("\r " , "" );
82+ }
83+ }
0 commit comments