Profile page, follow/unfollow button

This commit is contained in:
Michael Wain 2024-02-28 03:09:41 +03:00
parent 4a67bac3d9
commit 4f94d47a56
6 changed files with 75 additions and 11 deletions

View File

@ -1,8 +1,7 @@
package com.alterdekim.game.controller;
import com.alterdekim.game.dto.AuthApiObject;
import com.alterdekim.game.dto.FriendPageResult;
import com.alterdekim.game.dto.SelectOption;
import com.alterdekim.game.dto.*;
import com.alterdekim.game.entities.FriendStatus;
import com.alterdekim.game.entities.Image;
import com.alterdekim.game.entities.User;
import com.alterdekim.game.repository.ImageRepository;
@ -73,9 +72,21 @@ public class StaticController {
@GetMapping("/profile/{id}")
public String profilePage(@PathVariable("id") Long id, Model model) {
AuthenticationUtil.authProfile(model, userService);
User self = AuthenticationUtil.authProfile(model, userService);
User u = userService.findById(id);
model.addAttribute("page_user", new FriendPageResult("", "background-image: url(\"/image/store/"+u.getAvatarId()+"\");", u.getUsername(), u.getId(), u.getDisplayName()));
FriendFollowState state = FriendFollowState.NOT_FOLLOWED;
FriendStatus fs = friendService.getFollow(self.getId(), u.getId());
if( fs != null ) {
if( fs.getFirstUserId().longValue() == self.getId().longValue() ) {
state = FriendFollowState.FOLLOWED;
} else {
state = FriendFollowState.ACCEPT;
}
}
if( friendService.getFriend(self.getId(), u.getId()) != null ) {
state = FriendFollowState.FOLLOWED;
}
model.addAttribute("page_user", new ProfilePageResult("", "background-image: url(\"/image/store/"+u.getAvatarId()+"\");", u.getUsername(), u.getId(), u.getDisplayName(), 0, 0, friendService.getFriendsOfUserId(u.getId()).size(), state));
return "profile";
}

View File

@ -0,0 +1,7 @@
package com.alterdekim.game.dto;
public enum FriendFollowState {
NOT_FOLLOWED,
FOLLOWED,
ACCEPT
}

View File

@ -0,0 +1,20 @@
package com.alterdekim.game.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class ProfilePageResult {
private String href;
private String avatar;
private String username;
private Long id;
private String displayName;
private Integer games;
private Integer wins;
private Integer friends_cnt;
private FriendFollowState follow_state;
}

View File

@ -24,6 +24,9 @@ public interface FriendRepository extends JpaRepository<FriendStatus, Long> {
@Query(value = "SELECT f FROM FriendStatus f WHERE ((f.firstUserId = :userId AND f.secondUserId = :friendId) OR (f.firstUserId = :friendId AND f.secondUserId = :userId)) AND f.status = 1")
FriendStatus getFollow(@Param("userId") Long userId, @Param("friendId") Long friendId);
@Query(value = "SELECT f FROM FriendStatus f WHERE ((f.firstUserId = :userId AND f.secondUserId = :friendId) OR (f.firstUserId = :friendId AND f.secondUserId = :userId)) AND f.status = 2")
FriendStatus getFriend(@Param("userId") Long userId, @Param("friendId") Long friendId);
@Transactional
@Modifying
@Query(value = "UPDATE FriendStatus f SET f.status = 2 WHERE ((f.firstUserId = :userId AND f.secondUserId = :friendId) OR (f.firstUserId = :friendId AND f.secondUserId = :userId)) AND f.status = 1")

View File

@ -27,4 +27,12 @@ public class FriendServiceImpl {
}
repository.setFriend(userId, friendId);
}
public FriendStatus getFollow(Long userId, Long friendId) {
return repository.getFollow(userId, friendId);
}
public FriendStatus getFriend(Long userId, Long friendId) {
return repository.getFriend(userId, friendId);
}
}

View File

@ -14,19 +14,34 @@
</div>
<div class="profile-second-row">
<div class="profile-top-actions">
<button class="btn btn-primary" onClick="followUser(this)" th:data-profile-id="${page_user.id}">Follow</button>
<th:block th:switch="${page_user.id}">
<th:block th:case="${profile.id}"></th:block>
<th:block th:case="*">
<th:block th:switch="${page_user.follow_state}">
<th:block th:case="${T(com.alterdekim.game.dto.FriendFollowState).NOT_FOLLOWED}">
<button class="btn btn-primary" onClick="followUser(this)" th:data-profile-id="${page_user.id}">Follow</button>
</th:block>
<th:block th:case="${T(com.alterdekim.game.dto.FriendFollowState).FOLLOWED}">
<button class="btn btn-danger" onClick="unfollowUser(this)" th:data-profile-id="${page_user.id}">Unfollow</button>
</th:block>
<th:block th:case="${T(com.alterdekim.game.dto.FriendFollowState).ACCEPT}">
<button class="btn btn-primary" onClick="followUser(this)" th:data-profile-id="${page_user.id}">Accept</button>
</th:block>
</th:block>
</th:block>
</th:block>
</div>
<div class="profile-top-stat-list">
<div class="profile-stat">
<div class="_val">5</div>
<div class="_key">Friends</div>
<div class="_val" th:text="${page_user.games}"></div>
<div class="_key">Games</div>
</div>
<div class="profile-stat">
<div class="_val">5</div>
<div class="_key">Friends</div>
<div class="_val" th:text="${page_user.wins}"></div>
<div class="_key">Wins</div>
</div>
<div class="profile-stat">
<div class="_val">5</div>
<div class="_val" th:text="${page_user.friends_cnt}"></div>
<div class="_key">Friends</div>
</div>
</div>