66import com .objectcomputing .pulsesurvey .repositories .ResponseKeyRepository ;
77import com .objectcomputing .pulsesurvey .repositories .ResponseRepository ;
88import com .objectcomputing .pulsesurvey .repositories .UserCommentsRepository ;
9- import io .micronaut .context .annotation .Value ;
109import io .micronaut .core .util .CollectionUtils ;
1110import io .micronaut .http .HttpResponse ;
1211import io .micronaut .http .MediaType ;
2221import javax .inject .Inject ;
2322import java .net .URI ;
2423import java .net .URISyntaxException ;
24+ import java .util .List ;
2525import java .util .Optional ;
2626import java .util .UUID ;
2727import java .util .concurrent .atomic .AtomicBoolean ;
@@ -85,7 +85,7 @@ HttpResponse<String> happiness(String currentEmotion, String surveyKey) {
8585
8686 try {
8787 LOG .info ("redirecting to /happiness/comment" );
88- return HttpResponse .redirect (new URI ("/happiness/comment?surveyKey=" +surveyKey ));
88+ return HttpResponse .temporaryRedirect (new URI ("/happiness/comment?surveyKey=" +surveyKey ));
8989 } catch (URISyntaxException e ) {
9090 e .printStackTrace ();
9191 LOG .error ("unable to redirect to /happiness/comment " + e .getMessage ());
@@ -95,8 +95,7 @@ HttpResponse<String> happiness(String currentEmotion, String surveyKey) {
9595 LOG .warn ("This key is not valid: " + surveyKey );
9696 }
9797
98- return HttpResponse .ok ("Hello, your current emotion of " + currentEmotion + "!" +
99- " is duly noted." );
98+ return HttpResponse .ok ("This key has already been used." );
10099 }
101100
102101 @ Get ("comment" )
@@ -110,16 +109,19 @@ public HttpResponse displayComments(String surveyKey) {
110109 @ Post ("userComments" )
111110 @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
112111 @ View ("thankyou" )
113- public HttpResponse sendThankYouWithCommentBlock
114- (@ Value ("userComments" ) String userComments ,
115- @ Value ("surveyKey" ) String surveyKey ) {
112+ public HttpResponse sendThankYouWithCommentBlock (String userComments , String surveyKey ) {
116113
117114 LOG .info ("The user has commented: " + userComments );
118115 LOG .info ("With surveyKey: " + surveyKey );
119116 // put comment into the db using the survey key
120- saveUserComment (surveyKey , userComments );
121-
122- return HttpResponse .ok ();
117+ // first check to see if there is currently a comment
118+ boolean alreadyIsComment = checkForComment (surveyKey );
119+ if (!alreadyIsComment ) {
120+ saveUserComment (surveyKey , userComments );
121+ return HttpResponse .ok ();
122+ } else {
123+ return HttpResponse .ok ("This key has already been used." );
124+ }
123125 }
124126
125127 @ Get ("thanks" )
@@ -164,12 +166,23 @@ boolean saveResponse(String currentEmotion, String surveyKey) {
164166 return responseAdded ;
165167 }
166168
167- private void saveUserComment (String surveyKey , String comments ) {
169+ private boolean checkForComment (String surveyKey ) {
170+
171+ LOG .info ("Validating comment " + surveyKey );
172+ List <UserComments > userComments = userCommentsRepo .findComments (surveyKey );
173+
174+ boolean isPresent = false ;
175+ isPresent = !userComments .isEmpty ();
176+ return isPresent ;
177+
178+ }
179+
180+ private void saveUserComment (String surveyKey , String commentText ) {
168181
169182 UserComments userComments = new UserComments ();
170- userComments .setCommentText (comments );
183+ userComments .setCommentText (commentText );
171184 userComments .setResponseKey (UUID .fromString (surveyKey ));
172- userCommentsRepo .save (userComments );
185+ userComments = userCommentsRepo .save (userComments );
173186 }
174187
175188 ResponseKey markKeyAsUsed (String surveyKey ) {
0 commit comments