xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame^] | 3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| 5 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 6 | import com.g9.g9backend.pojo.Community; |
| 7 | import com.g9.g9backend.pojo.DTO.*; |
| 8 | import com.g9.g9backend.pojo.Subscription; |
| 9 | import com.g9.g9backend.pojo.Thread; |
| 10 | import com.g9.g9backend.pojo.ThreadLike; |
| 11 | import com.g9.g9backend.service.*; |
| 12 | import org.jetbrains.annotations.NotNull; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame^] | 15 | import org.springframework.http.ResponseEntity; |
| 16 | import org.springframework.web.bind.annotation.*; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | import java.util.Objects; |
| 22 | import java.util.function.Function; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * CommunityController 社区控制器类,处理与社区相关的请求 |
| 26 | * |
| 27 | * @author Seamher |
| 28 | */ |
| 29 | @RestController |
| 30 | public class CommunityController { |
| 31 | |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame^] | 32 | private final CommunityService communityService; |
| 33 | |
| 34 | private final ThreadService threadService; |
| 35 | |
| 36 | private final SubscriptionService subscriptionService; |
| 37 | |
| 38 | private final ThreadLikeService threadLikeService; |
| 39 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 40 | private final Logger logger = LoggerFactory.getLogger(CommunityController.class); |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame^] | 41 | |
| 42 | public CommunityController(CommunityService communityService, ThreadService threadService, SubscriptionService subscriptionService, ThreadLikeService threadLikeService) { |
| 43 | this.communityService = communityService; |
| 44 | this.threadService = threadService; |
| 45 | this.threadLikeService = threadLikeService; |
| 46 | this.subscriptionService = subscriptionService; |
| 47 | } |
| 48 | |
| 49 | @PostMapping(value = "/thread") |
| 50 | public ResponseEntity<String> postThread(@RequestBody Thread thread) { |
| 51 | thread.setLikes(0); |
| 52 | thread.setCommentNumber(0); |
| 53 | threadService.save(thread); |
| 54 | |
| 55 | Community community = communityService.getById(thread.getCommunityId()); |
| 56 | community.setThreadNumber(community.getThreadNumber() + 1); |
| 57 | communityService.updateById(community); |
| 58 | |
| 59 | logger.info("帖子已发布"); |
| 60 | |
| 61 | return ResponseEntity.ok(""); |
| 62 | } |
| 63 | |
| 64 | @PostMapping(value = "/thread/like") |
| 65 | public ResponseEntity<String> postThreadLike(@RequestBody Map<String, Integer> request) { |
| 66 | Integer userId = request.get("userId"); |
| 67 | Integer threadId = request.get("threadId"); |
| 68 | |
| 69 | ThreadLike threadLike = new ThreadLike(userId, threadId); |
| 70 | threadLikeService.save(threadLike); |
| 71 | |
| 72 | Thread thread = threadService.getById(threadId); |
| 73 | thread.setLikes(thread.getLikes() + 1); |
| 74 | threadService.updateById(thread); |
| 75 | |
| 76 | logger.info("点赞成功"); |
| 77 | |
| 78 | return ResponseEntity.ok(""); |
| 79 | } |
| 80 | |
| 81 | @DeleteMapping(value = "/thread") |
| 82 | public ResponseEntity<String> deleteThread(@RequestParam Integer threadId) { |
| 83 | Thread thread = threadService.getById(threadId); |
| 84 | threadService.removeById(threadId); |
| 85 | |
| 86 | Community community = communityService.getById(thread.getCommunityId()); |
| 87 | community.setThreadNumber(community.getThreadNumber() - 1); |
| 88 | communityService.updateById(community); |
| 89 | |
| 90 | logger.info("帖子已删除"); |
| 91 | |
| 92 | return ResponseEntity.noContent().build(); |
| 93 | } |
| 94 | |
| 95 | @DeleteMapping(value = "/thread/like") |
| 96 | public ResponseEntity<String> deleteLike(@RequestParam Integer userId, @RequestParam Integer threadId) { |
| 97 | LambdaQueryWrapper<ThreadLike> threadLikeQuery = new LambdaQueryWrapper<ThreadLike>() |
| 98 | .eq(ThreadLike::getUserId, userId) |
| 99 | .eq(ThreadLike::getThreadId, threadId); |
| 100 | |
| 101 | threadLikeService.remove(threadLikeQuery); |
| 102 | |
| 103 | Thread thread = threadService.getById(threadId); |
| 104 | thread.setLikes(thread.getLikes() - 1); |
| 105 | threadService.updateById(thread); |
| 106 | |
| 107 | logger.info("取消点赞成功"); |
| 108 | |
| 109 | return ResponseEntity.noContent().build(); |
| 110 | } |
| 111 | |
| 112 | @GetMapping(value = "/community") |
| 113 | public ResponseEntity<GetCommunityDTO> getCommunity(@RequestParam String searchValue, |
| 114 | @RequestParam String type, |
| 115 | @RequestParam Integer pageNumber, |
| 116 | @RequestParam Integer rows) { |
| 117 | Page<Community> communityPage = new Page<>(pageNumber, rows); |
| 118 | LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>() |
| 119 | .eq(Community::getType, type) |
| 120 | .like(StringUtils.isNotBlank(searchValue), Community::getCommunityName, searchValue) |
| 121 | .orderByDesc(Community::getHot); |
| 122 | |
| 123 | Page<Community> result = communityService.page(communityPage, communityQuery); |
| 124 | |
| 125 | GetCommunityDTO getCommunityDTO = wrapCommunityPage(result, item -> { |
| 126 | GetCommunityDTO.Community community = new GetCommunityDTO.Community(); |
| 127 | community.setCommunityId(item.getCommunityId()); |
| 128 | community.setCommunityName(item.getCommunityName()); |
| 129 | community.setCommunityPicture(item.getCommunityPicture()); |
| 130 | community.setDescription(item.getDescription()); |
| 131 | community.setHot(item.getHot()); |
| 132 | community.setThreadNumber(item.getThreadNumber()); |
| 133 | community.setResourceId(item.getResourceId()); |
| 134 | return community; |
| 135 | }); |
| 136 | |
| 137 | return ResponseEntity.ok(getCommunityDTO); |
| 138 | } |
| 139 | |
| 140 | @NotNull |
| 141 | private <T> GetCommunityDTO wrapCommunityPage(Page<T> page, Function<T, GetCommunityDTO.Community> mapper) { |
| 142 | List<GetCommunityDTO.Community> records = page.getRecords().stream().map(mapper).toList(); |
| 143 | |
| 144 | return new GetCommunityDTO(records, (int) page.getTotal(), (int) page.getPages(), (int) page.getCurrent(), (int) page.getSize()); |
| 145 | } |
| 146 | |
| 147 | @GetMapping(value = "/community/info") |
| 148 | public ResponseEntity<Community> getCommunityInfo(@RequestParam Integer communityId) { |
| 149 | |
| 150 | return ResponseEntity.ok(communityService.getById(communityId)); |
| 151 | } |
| 152 | |
| 153 | @GetMapping(value = "/thread") |
| 154 | public ResponseEntity<ThreadDTO> getThread(@RequestParam Integer threadId, @RequestParam Integer userId) { |
| 155 | LambdaQueryWrapper<ThreadLike> threadLikeQuery = new LambdaQueryWrapper<ThreadLike>() |
| 156 | .eq(ThreadLike::getUserId, userId) |
| 157 | .eq(ThreadLike::getThreadId, threadId); |
| 158 | Thread thread = threadService.getById(threadId); |
| 159 | ThreadDTO threadDTO = new ThreadDTO( |
| 160 | threadId, |
| 161 | thread.getUserId(), |
| 162 | thread.getThreadPicture(), |
| 163 | thread.getTitle(), |
| 164 | thread.getContent(), |
| 165 | thread.getLikes(), |
| 166 | threadLikeService.getOne(threadLikeQuery) != null, |
| 167 | thread.getCreateAt(), |
| 168 | thread.getCommentNumber(), |
| 169 | thread.getCommunityId() |
| 170 | ); |
| 171 | |
| 172 | return ResponseEntity.ok(threadDTO); |
| 173 | } |
| 174 | |
| 175 | @GetMapping(value = "/community/threads") |
| 176 | public ResponseEntity<GetCommunityThreadsDTO> getCommunityThreads(@RequestParam Integer communityId, |
| 177 | @RequestParam Integer pageNumber, |
| 178 | @RequestParam Integer rows, |
| 179 | @RequestParam String option, |
| 180 | @RequestParam String searchValue, |
| 181 | @RequestParam Integer userId) { |
| 182 | Page<Thread> threadPage = new Page<>(pageNumber, rows); |
| 183 | LambdaQueryWrapper<Thread> threadQuery = new LambdaQueryWrapper<Thread>() |
| 184 | .eq(Thread::getCommunityId, communityId) |
| 185 | .like(StringUtils.isNotBlank(searchValue), Thread::getTitle, searchValue); |
| 186 | |
| 187 | if (Objects.equals(option, "最高热度")) { |
| 188 | |
| 189 | threadQuery.orderByDesc(Thread::getLikes); |
| 190 | } else { |
| 191 | |
| 192 | List<Integer> userIds = subscriptionService.list(new LambdaQueryWrapper<Subscription>() |
| 193 | .eq(Subscription::getUserId, userId)) |
| 194 | .stream() |
| 195 | .map(Subscription::getFollowerId) |
| 196 | .toList(); |
| 197 | |
| 198 | threadQuery.in(!userIds.isEmpty(), Thread::getUserId, userIds) |
| 199 | .orderByDesc(Thread::getLikes); |
| 200 | } |
| 201 | |
| 202 | Page<Thread> result = threadService.page(threadPage, threadQuery); |
| 203 | |
| 204 | GetCommunityThreadsDTO getCommunityThreadsDTO = wrapThreadPage(result, item -> { |
| 205 | GetCommunityThreadsDTO.Thread thread = new GetCommunityThreadsDTO.Thread(); |
| 206 | thread.setThreadId(item.getThreadId()); |
| 207 | thread.setUserId(item.getUserId()); |
| 208 | thread.setThreadPicture(item.getThreadPicture()); |
| 209 | thread.setTitle(item.getTitle()); |
| 210 | thread.setLikes(item.getLikes()); |
| 211 | thread.setCreateAt(item.getCreateAt()); |
| 212 | return thread; |
| 213 | }); |
| 214 | |
| 215 | return ResponseEntity.ok(getCommunityThreadsDTO); |
| 216 | } |
| 217 | |
| 218 | @NotNull |
| 219 | private <T> GetCommunityThreadsDTO wrapThreadPage(Page<T> page, Function<T, GetCommunityThreadsDTO.Thread> mapper) { |
| 220 | List<GetCommunityThreadsDTO.Thread> records = page.getRecords().stream().map(mapper).toList(); |
| 221 | |
| 222 | return new GetCommunityThreadsDTO(records, (int) page.getTotal(), (int) page.getPages(), (int) page.getCurrent(), (int) page.getSize()); |
| 223 | } |
| 224 | |
| 225 | @GetMapping(value = "/community/hot") |
| 226 | public ResponseEntity<GetCommunityHotDTO> getCommunityHot() { |
| 227 | Page<Community> communityPage = new Page<>(1, 3); |
| 228 | LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>() |
| 229 | .orderByDesc(Community::getHot); |
| 230 | |
| 231 | Page<Community> result = communityService.page(communityPage, communityQuery); |
| 232 | |
| 233 | List<GetCommunityHotDTO.Community> communityList = getCommunityList(result); |
| 234 | |
| 235 | GetCommunityHotDTO getCommunityHotDTO = new GetCommunityHotDTO(communityList); |
| 236 | |
| 237 | return ResponseEntity.ok(getCommunityHotDTO); |
| 238 | } |
| 239 | |
| 240 | @NotNull |
| 241 | private static List<GetCommunityHotDTO.Community> getCommunityList(Page<Community> communityPage) { |
| 242 | List<Community> communityListT = communityPage.getRecords(); |
| 243 | List<GetCommunityHotDTO.Community> communityList = new ArrayList<>(); |
| 244 | int status = 1; |
| 245 | for (Community community : communityListT) { |
| 246 | GetCommunityHotDTO.Community communityHot = new GetCommunityHotDTO.Community( |
| 247 | community.getCommunityId(), |
| 248 | community.getCommunityName(), |
| 249 | community.getCommunityPicture(), |
| 250 | community.getCommunityPicture(), |
| 251 | community.getHot(), |
| 252 | community.getType(), |
| 253 | community.getThreadNumber(), |
| 254 | community.getResourceId(), |
| 255 | status); |
| 256 | communityList.add(communityHot); |
| 257 | status++; |
| 258 | } |
| 259 | |
| 260 | return communityList; |
| 261 | } |
| 262 | |
| 263 | @GetMapping(value = "/community/common") |
| 264 | public ResponseEntity<GetCommunityCommonDTO> getCommunityCommon() { |
| 265 | Page<Community> communityPage = new Page<>(2, 3); |
| 266 | LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>() |
| 267 | .orderByDesc(Community::getHot); |
| 268 | |
| 269 | Page<Community> result = communityService.page(communityPage, communityQuery); |
| 270 | |
| 271 | List<Community> communityList = result.getRecords(); |
| 272 | |
| 273 | GetCommunityCommonDTO getCommunityCommonDTO = new GetCommunityCommonDTO(communityList); |
| 274 | |
| 275 | return ResponseEntity.ok(getCommunityCommonDTO); |
| 276 | } |
| 277 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 278 | } |