#How do i perform this operation

1 messages · Page 1 of 1 (latest)

vernal vine
#

I am trying to perform operation for a post (question) in my project,

List<Post> acceptedAnswer = question.getAnswers().stream()
                    .filter(ans -> Objects.equals(ans.getStatus(), PostStatus.ACCEPTED))
                    .toList();
            
            if(acceptedAnswer.size() != 1) {
               log.error("Invalid accepted answers for a closed question {}", question.getId()); 
            }

is this correct way to retrieve the accepted answer (each question can have atmost 1), in earlier logics i have made sure there cannot be more than 1 accepted answer so do i need this additional check, if not how do i code to receive single value instead of list

regal knollBOT
#

<@&987246399047479336> please have a look, thanks.

storm grove
vernal vine
#

k

vernal vine
storm grove
vernal vine
#

hm, but i am confused do i need to check for multiple values, as this is the only method to change post status and it does not allow mutiple accepted answers

storm grove
#

findFirst will take the first value

#

findAny will usually take the first value but can technically can take any value

vernal vine
#

ya got that part but if there does exist >1 values it is a problem

storm grove
#

as I said, it will take the first value and ignore the rest

vernal vine
#

i dont think u understand my doubt but anyway thank for the help

storm grove
#

You ask what if there is more than 1 value, I answered that it will ignore them

vernal vine
#

like it should not have >1 accepted answer, it is not about if it return 1 value

storm grove
#

wdym

vernal vine
#

i will explain in 5 min

regal knollBOT
# vernal vine

I uploaded your attachments as Gist. This makes them more accessible, for example to mobile users.

vernal vine
#

so this is the current logic to handle answer status change

#

an answer can only have unreviewed, accepted as their status

#

so when user tries to change status of an answer (unreviewed by default), following cases are possible:
-> (new operation) if current question status is not CLOSED, it will accept current answer as ACCEPTED
-> (remove operation) if current question status is CLOSED and current answer status is ACCEPTED, it will remove current answer as ACCEPTED and also reopen question (ie remove CLOSED)
-> (update operation) if current question status is CLOSED and current answer status is not ACCEPTED, it will remove prev answer as ACCEPTED, mark current answer as ACCEPTED

#

for the update operation, i want ACCEPTED answer (prev answer) from list of answer of question, that is y i am performing stream operation

#

the list of answer should not have multiple accepted answers, how do i make sure of it

#

@storm grove

storm grove
vernal vine
#

y two streams?

storm grove
# vernal vine y two streams?
 if(question.getAnswers().stream()
                    .filter(ans -> Objects.equals(ans.getStatus(), PostStatus.ACCEPTED))
                    .count() != 1) {
  log.error("Invalid accepted answers for a closed question {}", question.getId()); 
}
Post post = question.getAnswers().stream()
                    .filter(ans -> Objects.equals(ans.getStatus(), PostStatus.ACCEPTED))
                    .findAny().orElseThrow();
#

But well

#

just keep your apprach

vernal vine
#

k thx

#

also do u understand my code (ie what it does)?

storm grove
vernal vine
#

k, had a question related to it but already solved