'Why iterate data twice in java
In my program all work properly but problem is the total like
get twice for one user i try to iterate properly but i am not succeed i can try to solve this problem from 2 days...
My Output:
Profile(1): 1 2 Like | Profile(2): 1 2 Like
Expected Output:
Profile(1): 1 Like | Profile(2): 2 Like
Repository:
// all query work fine
public interface postlikeRepo extends JpaRepository<Likepost, Integer>{
@Query(nativeQuery = true, value = "SELECT COUNT(*) FROM like_master WHERE post_id = ?")
public int getTotalLike(Integer Id);
}
public interface requestRepo extends JpaRepository<Request, Integer> {
@Query(nativeQuery = true, value="SELECT * FROM request_master WHERE sender_id = ? AND status = ?")
List<requestEntity> getAcceptRequestFrnd(Integer Sender_id, String Status);
@Query(nativeQuery = true, value="SELECT rgm.u_id,pm.profile, rsm.sender_id, rgm.username, upm.post_id, upm.post, upm.date FROM registration_master AS rgm INNER JOIN profile_master AS pm ON rgm.u_id = pm.user_id INNER JOIN uploadpost_master AS upm ON rgm.u_id = upm.user_id INNER JOIN request_master AS rsm ON rgm.u_id = rsm.receiver_id WHERE rsm.receiver_id = ? ORDER BY upm.date DESC LIMIT 1")
List<ProfileDto> getPostWithAccount(Integer Receiver_id);
}
Service:
@Service
public class pojoServiceImpl implements pojoService {
@Autowired
private requestRepo requestRepo;
@Autowired
private postlikeRepo postlikeRepo;
// get user with newest post
@Override
public List<ProfileDto> getPostWithAccount(Integer Receiver_id) {
return this.requestRepo.getPostWithAccount(Receiver_id);
}
// get totle like
@Override
public int getTotalLike(int Id) {
return this.postlikeRepo.getTotalLike(Id);
}
}
Controller:
@Controller
public class meetzenController {
@Autowired
private pojoService pojoService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model mdl, HttpSession session, Request Request){
Integer SessionId = Integer.parseInt(session.getAttribute("Userid").toString());
Map<Integer, List<ProfileDto>> ListOfPost = new HashMap<>();
Map<Integer, Integer> LikeCount = new HashMap<>();
List<Request> GetUser = this.pojoService.getAcceptRequestFrnd(SessionId, "Accept");
for(Request GetUserForPost : GetUser)
{
List<ProfileDto> GetUserWithPost = this.pojoService.getPostWithAccount(GetUserForPost.getReceiver_id());
for(ProfileDto GetLikeCount: GetUserWithPost)
{
Integer GetTotalLike = this.pojoService.getTotalLike(GetLikeCount.getPost_id());
// Problem Here
LikeCount.put(GetTotalLike, GetLikeCount.getPost_id());
}
ListOfPost.put(GetUserForPost.getReceiver_id(), GetUserWithPost);
}
mdl.addAttribute("likeCount", LikeCount);
mdl.addAttribute("ListOfPost", ListOfPost);
return "post";
}
}
Thymeleaf:
<div th:each="ExtractListOfData :${ListOfPost}">
<div th:each="ExtractListOfSubData: ${ExtractListOfData.value}">
<div class="media">
<div class="row js-masonry"
data-masonry='{ "itemSelector": ".grid-item", "columnWidth": ".grid-sizer", "percentPosition": true }'>
<div class="grid-item col-md-12 col-sm-12">
<div class="post clearfix">
<div class="media-grid">
<div class="user-info">
<a th:href="@{/profile/{uid}(uid=${ExtractListOfSubData.u_id})}">
<img th:src="${ExtractListOfSubData.profile}" alt=" " class="profile-photo-sm pull-left" />
</a>
<div class="user">
<h6>
<a th:href="@{/profile/{uid}(uid=${ExtractListOfSubData.u_id})}" class="profile-link" th:text="${ExtractListOfSubData.username}"></a>
</h6>
</div>
</div>
<div class="img-wrapper" data-toggle="modal" data-target=".modal-1">
<img th:src="${ExtractListOfSubData.post}" alt=" " class="img-responsive post-image" />
</div>
<div class="media-info">
<div class="reaction">
<div class="button-toggle">
<a type='submit' class='like-button-style' th:id="'unlike' + ${ExtractListOfSubData.post_id}" th:onclick="'btnunlike('+${ExtractListOfSubData.post_id}+')'">
<img src="https://img.icons8.com/material-outlined/24/000000/like--v1.png"/>
</a>
</div>
<button type="submit" class="comment">
<i class="fa fa-comment-o"></i>
</button>
<div class='like-style'>
<!-- My problem is here -->
<span th:each="data: ${likeCount}">
<span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${data}"></span>
</span>
<label>like</label>
</div>
<div class="row comment-top">
<div class="col-sm-12">
<div class="comment-box">
<input type="text" class="Comment-Textbox"
autocomplete="off" placeholder="Add your comment..." />
<input type="submit" class="postComment-button blur"
disabled="disabled" value="Post" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my Output:
Here is Expected Output:
Debug:
Solution 1:[1]
Thank you @andrewjames
I really appreciate your suggestion.
Solved
I have likeCount
model in Controller who pass HashMap(K,V)...
Remove <span th:each="data: ${likeCount}">
iteration and direct get value of this HashMap
from parent loop receiver_id()
like th:text="${likeCount.get(ExtractListOfSubData.receiver_id)}"
...
Change:
<span th:each="data: ${likeCount}">
<span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${data}"></span>
</span>
To:
<span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${likeCount.get(ExtractListOfSubData.receiver_id}"></span>
Output:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |