blob: c9a573c6dd2e3b7fad8f35ab67d8254ca7657d59 [file] [log] [blame]
package com.g9.g9backend.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.g9.g9backend.mapper.UserPurchaseMapper;
import com.g9.g9backend.pojo.*;
import com.g9.g9backend.pojo.DTO.*;
import com.g9.g9backend.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
/**
* ResourceController 资源控制器类,处理与资源相关的请求
*
* @author hcy
*/
@RestController
@RequestMapping("/resource")
public class ResourceController {
private final ResourceService resourceService;
private final GameplayService gameplayService;
private final RewardService rewardService;
private final UserUploadService userUploadService;
private final CommunityService communityService;
private final UserService userService;
private final UserPurchaseService userPurchaseService;
private final UserLikeService userLikeService;
private final UserCollectionService userCollectionService;
private final NotificationService notificationService;
public ResourceController(ResourceService resourceService, GameplayService gameplayService, RewardService rewardService, UserUploadService userUploadService, CommunityService communityService, UserService userService, UserPurchaseMapper userPurchaseMapper, UserPurchaseService userPurchaseService, UserLikeService userLikeService, UserCollectionService userCollectionService, NotificationService notificationService) {
this.resourceService = resourceService;
this.gameplayService = gameplayService;
this.rewardService = rewardService;
this.userUploadService = userUploadService;
this.communityService = communityService;
this.userService = userService;
this.userPurchaseService = userPurchaseService;
this.userLikeService = userLikeService;
this.userCollectionService = userCollectionService;
this.notificationService = notificationService;
}
private final Logger logger = LoggerFactory.getLogger(ResourceController.class);
/**
* 上传资源
*
* @param postResourceDTO 上传资源信息
* @return 上传资源结果
*/
@PostMapping
public ResponseEntity<String> uploadResource(@RequestBody PostResourceDTO postResourceDTO) {
// 存资源
Resource resource = postResourceDTO.getResource();
resourceService.save(resource);
// 存玩法列表
String[] gameplayList = postResourceDTO.getGameplayList();
for (String gameplayName : gameplayList) {
Gameplay gameplay = new Gameplay();
gameplay.setGameplayName(gameplayName);
gameplay.setResourceId(postResourceDTO.getResource().getResourceId());
gameplayService.save(gameplay);
}
// 完成对应悬赏
if (postResourceDTO.getCompleteRewardId() != 0) {
UpdateWrapper<Reward> rewardUpdate = new UpdateWrapper<>();
rewardUpdate.eq("reward_id", postResourceDTO.getCompleteRewardId()).set("completed_by", postResourceDTO.getUserId()).set("completed_at", postResourceDTO.getResource().getUploadTime()).set("resource_id", postResourceDTO.getResource().getResourceId());
rewardService.update(rewardUpdate);
}
// 存用户上传表
UserUpload userUpload = new UserUpload();
userUpload.setUserId(postResourceDTO.getUserId());
userUpload.setResourceId(postResourceDTO.getResource().getResourceId());
userUploadService.save(userUpload);
// 创建资源社区
Community community = new Community();
community.setCommunityName(postResourceDTO.getResource().getResourceName());
community.setType(postResourceDTO.getResource().getClassify());
community.setResourceId(postResourceDTO.getResource().getResourceId());
communityService.save(community);
return ResponseEntity.ok("");
}
/**
* 购买资源
*
* @param userResourceDTO 购买资源信息
* @return 购买资源结果
*/
@PostMapping("purchase")
public ResponseEntity<String> purchaseResource(@RequestBody UserResourceDTO userResourceDTO) {
QueryWrapper<User> userQuery = new QueryWrapper<>();
userQuery.eq("user_id", userResourceDTO.getUserId());
User user = userService.getOne(userQuery);
QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
Resource resource = resourceService.getOne(ResourceQuery);
if (user.getCredits() < resource.getPrice()) {
// 积分余额不足
logger.info("The balance of points is insufficient to cover the price of this resource: {}", resource.getPrice());
return ResponseEntity.status(412).body("");
} else {
// 扣除用户积分
UpdateWrapper<User> userUpdate = new UpdateWrapper<>();
userUpdate.eq("user_id", user.getUserId()).set("credits", user.getCredits() - resource.getPrice());
userService.update(userUpdate);
// 添加购买资源记录
UserPurchase userPurchase = new UserPurchase();
userPurchase.setUserId(user.getUserId());
userPurchase.setResourceId(resource.getResourceId());
userPurchaseService.save(userPurchase);
// 给上传该资源的用户发送通知
Notification notification = new Notification();
QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
UserUpload userUpload = userUploadService.getOne(userUploadQuery);
notification.setUserId(userUpload.getUserId());
notification.setTitle("资源被购买");
notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 购买了!");
notification.setCreateAt(new Date());
notification.setRead(false);
notification.setTriggeredBy(userResourceDTO.getUserId());
notification.setRelatedId(userResourceDTO.getResourceId());
notificationService.save(notification);
return ResponseEntity.ok("");
}
}
/**
* 点赞资源
*
* @param userResourceDTO 点赞资源信息
* @return 点赞资源结果
*/
@PostMapping("like")
public ResponseEntity<String> likeResource(@RequestBody UserResourceDTO userResourceDTO) {
QueryWrapper<User> userQuery = new QueryWrapper<>();
userQuery.eq("user_id", userResourceDTO.getUserId());
User user = userService.getOne(userQuery);
QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
Resource resource = resourceService.getOne(ResourceQuery);
UserLike userLike = new UserLike();
userLike.setUserId(userResourceDTO.getUserId());
userLike.setResourceId(userResourceDTO.getResourceId());
userLikeService.save(userLike);
// 给上传该资源的用户发送通知
Notification notification = new Notification();
QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
UserUpload userUpload = userUploadService.getOne(userUploadQuery);
notification.setUserId(userUpload.getUserId());
notification.setTitle("资源被点赞");
notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 点赞了!");
notification.setCreateAt(new Date());
notification.setRead(false);
notification.setTriggeredBy(userResourceDTO.getUserId());
notification.setRelatedId(userResourceDTO.getResourceId());
notificationService.save(notification);
return ResponseEntity.ok("");
}
/**
* 收藏资源
*
* @param userResourceDTO 收藏资源信息
* @return 收藏资源结果
*/
@PostMapping("collection")
public ResponseEntity<String> collectResource(@RequestBody UserResourceDTO userResourceDTO) {
QueryWrapper<User> userQuery = new QueryWrapper<>();
userQuery.eq("user_id", userResourceDTO.getUserId());
User user = userService.getOne(userQuery);
QueryWrapper<Resource> ResourceQuery = new QueryWrapper<>();
ResourceQuery.eq("resource_id", userResourceDTO.getResourceId());
Resource resource = resourceService.getOne(ResourceQuery);
UserCollection userCollection = new UserCollection();
userCollection.setUserId(userResourceDTO.getUserId());
userCollection.setResourceId(userResourceDTO.getResourceId());
userCollectionService.save(userCollection);
// 给上传该资源的用户发送通知
Notification notification = new Notification();
QueryWrapper<UserUpload> userUploadQuery = new QueryWrapper<>();
userUploadQuery.eq("resource_id", userResourceDTO.getResourceId());
UserUpload userUpload = userUploadService.getOne(userUploadQuery);
notification.setUserId(userUpload.getUserId());
notification.setTitle("资源被收藏");
notification.setContent("你的资源:" + resource.getResourceName() + " 被: " + user.getUsername() + " 收藏了!");
notification.setCreateAt(new Date());
notification.setRead(false);
notification.setTriggeredBy(userResourceDTO.getUserId());
notification.setRelatedId(userResourceDTO.getResourceId());
notificationService.save(notification);
return ResponseEntity.ok("");
}
/**
* 删除资源
*
* @param resourceId 资源id
* @param userId 用户id
* @param password 密码
* @return 删除资源结果
*/
@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<String> userDelete(@RequestParam int resourceId, @RequestParam int userId, @RequestParam String password) {
logger.warn("Delete resource with id: {}", resourceId);
// 根据用户id查询该用户
QueryWrapper<User> userQuery = new QueryWrapper<>();
userQuery.eq("user_id", userId);
User userCheck = userService.getOne(userQuery);
// 只允许在用户上传资源页进行删除操作,所以不需要验证该资源是由该用户上传的
if (userCheck.getPassword().equals(password)) {
// 删除资源
resourceService.removeById(resourceId);
return ResponseEntity.noContent().build();
} else {
logger.warn("Delete failed. Incorrect password for account: {}", password);
return ResponseEntity.status(408).body("");
}
}
/**
* 取消点赞
*
* @param userId 用户id
* @param resourceId 资源id
* @return 取消点赞结果
*/
@DeleteMapping("like")
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<String> likeDelete(@RequestParam int userId, @RequestParam int resourceId) {
QueryWrapper<UserLike> userLikeQuery = new QueryWrapper<>();
userLikeQuery.eq("user_id", userId).eq("resource_id", resourceId);
userLikeService.remove(userLikeQuery);
return ResponseEntity.noContent().build();
}
/**
* 取消收藏
*
* @param userId 用户id
* @param resourceId 资源id
* @return 取消收藏结果
*/
@DeleteMapping("collection")
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<String> collectionDelete(@RequestParam int userId, @RequestParam int resourceId) {
QueryWrapper<UserCollection> userCollectionQuery = new QueryWrapper<>();
userCollectionQuery.eq("user_id", userId).eq("resource_id", resourceId);
userCollectionService.remove(userCollectionQuery);
return ResponseEntity.noContent().build();
}
}