v0.2.10 完成活动报名点赞评论模块构建
parent
3540bd8de9
commit
6232ada60d
|
|
@ -0,0 +1,61 @@
|
|||
package com.bruce.sams.controller.ams;
|
||||
|
||||
import com.bruce.sams.domain.ams.Comment;
|
||||
import com.bruce.sams.domain.entity.AjaxResult;
|
||||
import com.bruce.sams.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 活动评论控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/comment")
|
||||
public class CommentController {
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
/**
|
||||
* 用户发表评论
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@PostMapping("/add")
|
||||
public AjaxResult addComment(@RequestParam Long userId, @RequestParam Long actId,
|
||||
@RequestParam String content, @RequestParam(required = false) Long parentCommentId) {
|
||||
commentService.addComment(userId, actId, content, parentCommentId);
|
||||
return AjaxResult.success("评论成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户删除自己的评论 / 管理员删除任何评论
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
|
||||
@DeleteMapping("/delete")
|
||||
public AjaxResult deleteComment(@RequestParam Long commentId, @RequestParam Long userId,
|
||||
@RequestParam boolean isAdmin) {
|
||||
commentService.deleteComment(commentId, userId, isAdmin);
|
||||
return AjaxResult.success("评论已删除");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活动的所有评论
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public AjaxResult getCommentsForActivity(@RequestParam Long actId) {
|
||||
List<Comment> comments = commentService.getCommentsForActivity(actId);
|
||||
return AjaxResult.success(comments);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某条评论的所有回复
|
||||
*/
|
||||
@GetMapping("/replies")
|
||||
public AjaxResult getRepliesForComment(@RequestParam Long parentCommentId) {
|
||||
List<Comment> replies = commentService.getRepliesForComment(parentCommentId);
|
||||
return AjaxResult.success(replies);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
package com.bruce.sams.controller.ams;
|
||||
|
||||
import com.bruce.sams.common.enums.ReactionType;
|
||||
import com.bruce.sams.domain.ams.Reaction;
|
||||
import com.bruce.sams.domain.entity.AjaxResult;
|
||||
import com.bruce.sams.service.ReactionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 活动点赞/点踩控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/reaction")
|
||||
public class ReactionController {
|
||||
|
||||
@Autowired
|
||||
private ReactionService reactionService;
|
||||
|
||||
/**
|
||||
* 用户点赞或点踩
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@PostMapping("/add")
|
||||
public AjaxResult addReaction(@RequestParam Long actId, @RequestParam Long userId, @RequestParam ReactionType reactionType) {
|
||||
reactionService.addReaction(actId, userId, reactionType);
|
||||
return AjaxResult.success("操作成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消点赞或点踩
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@DeleteMapping("/remove")
|
||||
public AjaxResult removeReaction(@RequestParam Long actId, @RequestParam Long userId) {
|
||||
reactionService.removeReaction(actId, userId);
|
||||
return AjaxResult.success("已取消点赞/点踩");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活动的点赞/点踩统计数据
|
||||
*/
|
||||
@GetMapping("/stats")
|
||||
public AjaxResult getReactionStats(@RequestParam Long actId) {
|
||||
Map<String, Long> stats = reactionService.getReactionStats(actId);
|
||||
return AjaxResult.success(stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员获取某个活动的所有点赞/点踩记录
|
||||
*/
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@GetMapping("/list")
|
||||
public AjaxResult getAllReactionsForActivity(@RequestParam Long actId) {
|
||||
List<Reaction> reactions = reactionService.getAllReactionsForActivity(actId);
|
||||
return AjaxResult.success(reactions);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package com.bruce.sams.controller.ams;
|
||||
|
||||
import com.bruce.sams.domain.ams.Registration;
|
||||
import com.bruce.sams.domain.entity.AjaxResult;
|
||||
import com.bruce.sams.service.RegistrationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 活动报名控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/registration")
|
||||
public class RegistrationController {
|
||||
|
||||
@Autowired
|
||||
private RegistrationService registrationService;
|
||||
|
||||
/**
|
||||
* 用户报名活动
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@PostMapping("/register")
|
||||
public AjaxResult register(@RequestParam Long actId, @RequestParam Long userId) {
|
||||
registrationService.register(actId, userId);
|
||||
return AjaxResult.success("报名成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消报名
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@PutMapping("/cancel")
|
||||
public AjaxResult cancelRegistration(@RequestParam Long actId, @RequestParam Long userId) {
|
||||
registrationService.cancelRegistration(actId, userId);
|
||||
return AjaxResult.success("报名已取消");
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户签到活动
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@PutMapping("/attend")
|
||||
public AjaxResult attendActivity(@RequestParam Long actId, @RequestParam Long userId) {
|
||||
registrationService.attendActivity(actId, userId);
|
||||
return AjaxResult.success("签到成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户报名的活动
|
||||
*/
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@GetMapping("/my-registrations")
|
||||
public AjaxResult listMyRegistrations(@RequestParam Long userId) {
|
||||
List<Registration> registrations = registrationService.listMyRegistrations(userId);
|
||||
return AjaxResult.success(registrations);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员获取某个活动的报名列表
|
||||
*/
|
||||
@PreAuthorize("hasRole('ADMIN') or hasRole('COLLEGE_ADMIN') or hasRole('DEPARTMENT_ADMIN')")
|
||||
@GetMapping("/list")
|
||||
public AjaxResult listRegistrationsForActivity(@RequestParam Long actId) {
|
||||
List<Registration> registrations = registrationService.listRegistrationsForActivity(actId);
|
||||
return AjaxResult.success(registrations);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,11 +3,16 @@ package com.bruce.sams.service;
|
|||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bruce.sams.domain.ams.Comment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【ams_comment(活动评论表)】的数据库操作Service
|
||||
* @createDate 2025-02-20 15:08:30
|
||||
*/
|
||||
public interface CommentService extends IService<Comment> {
|
||||
|
||||
void addComment(Long userId, Long actId, String content, Long parentCommentId);
|
||||
void deleteComment(Long commentId, Long userId, boolean isAdmin);
|
||||
List<Comment> getCommentsForActivity(Long actId);
|
||||
List<Comment> getRepliesForComment(Long parentCommentId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,20 @@
|
|||
package com.bruce.sams.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bruce.sams.common.enums.ReactionType;
|
||||
import com.bruce.sams.domain.ams.Reaction;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【ams_reaction(活动点赞/点踩表)】的数据库操作Service
|
||||
* @createDate 2025-02-20 15:08:43
|
||||
*/
|
||||
public interface ReactionService extends IService<Reaction> {
|
||||
|
||||
void addReaction(Long actId, Long userId, ReactionType reactionType);
|
||||
void removeReaction(Long actId, Long userId);
|
||||
Map<String, Long> getReactionStats(Long actId);
|
||||
List<Reaction> getAllReactionsForActivity(Long actId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,11 +3,18 @@ package com.bruce.sams.service;
|
|||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.bruce.sams.domain.ams.Registration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author bruce
|
||||
* @description 针对表【ams_registration(活动报名表)】的数据库操作Service
|
||||
* @createDate 2025-02-20 15:08:53
|
||||
*/
|
||||
public interface RegistrationService extends IService<Registration> {
|
||||
void register(Long actId, Long userId);
|
||||
void cancelRegistration(Long actId, Long userId);
|
||||
void attendActivity(Long actId, Long userId);
|
||||
List<Registration> listMyRegistrations(Long userId);
|
||||
List<Registration> listRegistrationsForActivity(Long actId);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,87 @@
|
|||
package com.bruce.sams.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.domain.ams.Comment;
|
||||
import com.bruce.sams.mapper.CommentMapper;
|
||||
import com.bruce.sams.service.CommentService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 活动评论服务实现类
|
||||
* @author bruce
|
||||
* @description 针对表【ams_comment(活动评论表)】的数据库操作Service实现
|
||||
* @createDate 2025-02-20 15:08:30
|
||||
*/
|
||||
@Service
|
||||
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
|
||||
implements CommentService {
|
||||
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
|
||||
|
||||
/**
|
||||
* 用户发表评论
|
||||
* @param userId 用户ID
|
||||
* @param actId 活动ID
|
||||
* @param content 评论内容
|
||||
* @param parentCommentId 父评论ID(如果是回复)
|
||||
*/
|
||||
@Override
|
||||
public void addComment(Long userId, Long actId, String content, Long parentCommentId) {
|
||||
Comment comment = new Comment();
|
||||
comment.setUserId(userId);
|
||||
comment.setActId(actId);
|
||||
comment.setContent(content);
|
||||
comment.setParentCommentId(parentCommentId);
|
||||
comment.setCreatedAt(new Date(System.currentTimeMillis()));
|
||||
|
||||
this.save(comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论
|
||||
* @param commentId 评论ID
|
||||
* @param userId 用户ID(用于权限判断)
|
||||
* @param isAdmin 是否为管理员操作
|
||||
*/
|
||||
@Override
|
||||
public void deleteComment(Long commentId, Long userId, boolean isAdmin) {
|
||||
Comment comment = this.getById(commentId);
|
||||
if (comment != null) {
|
||||
if (isAdmin || comment.getUserId().equals(userId)) {
|
||||
this.removeById(commentId);
|
||||
} else {
|
||||
throw new RuntimeException("无权限删除该评论");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活动的所有评论
|
||||
* @param actId 活动ID
|
||||
* @return 评论列表
|
||||
*/
|
||||
@Override
|
||||
public List<Comment> getCommentsForActivity(Long actId) {
|
||||
LambdaQueryWrapper<Comment> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Comment::getActId, actId).isNull(Comment::getParentCommentId);
|
||||
return this.list(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某条评论的所有回复
|
||||
* @param parentCommentId 父评论ID
|
||||
* @return 回复列表
|
||||
*/
|
||||
@Override
|
||||
public List<Comment> getRepliesForComment(Long parentCommentId) {
|
||||
LambdaQueryWrapper<Comment> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Comment::getParentCommentId, parentCommentId);
|
||||
return this.list(query);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +1,86 @@
|
|||
package com.bruce.sams.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.common.enums.ReactionType;
|
||||
import com.bruce.sams.domain.ams.Reaction;
|
||||
import com.bruce.sams.mapper.ReactionMapper;
|
||||
import com.bruce.sams.service.ReactionService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 活动点赞/点踩服务实现类
|
||||
* @author bruce
|
||||
* @description 针对表【ams_reaction(活动点赞/点踩表)】的数据库操作Service实现
|
||||
* @createDate 2025-02-20 15:08:43
|
||||
*/
|
||||
@Service
|
||||
public class ReactionServiceImpl extends ServiceImpl<ReactionMapper, Reaction>
|
||||
implements ReactionService {
|
||||
public class ReactionServiceImpl extends ServiceImpl<ReactionMapper, Reaction> implements ReactionService {
|
||||
|
||||
/**
|
||||
* 用户点赞或点踩
|
||||
* @param actId 活动ID
|
||||
* @param userId 用户ID
|
||||
* @param reactionType 反应类型(点赞 / 点踩)
|
||||
*/
|
||||
@Override
|
||||
public void addReaction(Long actId, Long userId, ReactionType reactionType) {
|
||||
LambdaQueryWrapper<Reaction> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Reaction::getActId, actId).eq(Reaction::getUserId, userId);
|
||||
Reaction existingReaction = this.getOne(query);
|
||||
|
||||
if (existingReaction == null) {
|
||||
Reaction reaction = new Reaction();
|
||||
reaction.setActId(actId);
|
||||
reaction.setUserId(userId);
|
||||
reaction.setReactionType(reactionType);
|
||||
reaction.setCreatedAt(new Date(System.currentTimeMillis()));
|
||||
this.save(reaction);
|
||||
} else if (existingReaction.getReactionType() != reactionType) {
|
||||
existingReaction.setReactionType(reactionType);
|
||||
this.updateById(existingReaction);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消点赞或点踩
|
||||
* @param actId 活动ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
@Override
|
||||
public void removeReaction(Long actId, Long userId) {
|
||||
LambdaQueryWrapper<Reaction> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Reaction::getActId, actId).eq(Reaction::getUserId, userId);
|
||||
this.remove(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活动的点赞/点踩统计数据
|
||||
* @param actId 活动ID
|
||||
* @return 点赞和点踩数量
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Long> getReactionStats(Long actId) {
|
||||
Map<String, Long> stats = new HashMap<>();
|
||||
stats.put("likes", this.lambdaQuery().eq(Reaction::getActId, actId).eq(Reaction::getReactionType, ReactionType.LIKE).count());
|
||||
stats.put("dislikes", this.lambdaQuery().eq(Reaction::getActId, actId).eq(Reaction::getReactionType, ReactionType.DISLIKE).count());
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个活动的所有点赞/点踩记录
|
||||
* @param actId 活动ID
|
||||
* @return 点赞/点踩列表
|
||||
*/
|
||||
@Override
|
||||
public List<Reaction> getAllReactionsForActivity(Long actId) {
|
||||
return this.lambdaQuery().eq(Reaction::getActId, actId).list();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +1,96 @@
|
|||
package com.bruce.sams.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bruce.sams.common.enums.RegistrationRole;
|
||||
import com.bruce.sams.common.enums.RegistrationStatus;
|
||||
import com.bruce.sams.domain.ams.Registration;
|
||||
import com.bruce.sams.mapper.RegistrationMapper;
|
||||
import com.bruce.sams.service.RegistrationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 活动报名服务实现类
|
||||
* @author bruce
|
||||
* @description 针对表【ams_registration(活动报名表)】的数据库操作Service实现
|
||||
* @createDate 2025-02-20 15:08:53
|
||||
*/
|
||||
@Service
|
||||
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration>
|
||||
implements RegistrationService {
|
||||
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService {
|
||||
|
||||
/**
|
||||
* 用户报名活动
|
||||
* @param actId 活动ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
@Override
|
||||
public void register(Long actId, Long userId) {
|
||||
Registration registration = new Registration();
|
||||
registration.setActId(actId);
|
||||
registration.setUserId(userId);
|
||||
registration.setRole(RegistrationRole.PARTICIPANT);
|
||||
registration.setStatus(RegistrationStatus.REGISTERED);
|
||||
registration.setRegisterTime(new Date(System.currentTimeMillis()));
|
||||
|
||||
this.save(registration);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消报名
|
||||
* @param actId 活动ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
@Override
|
||||
public void cancelRegistration(Long actId, Long userId) {
|
||||
LambdaQueryWrapper<Registration> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Registration::getActId, actId).eq(Registration::getUserId, userId);
|
||||
Registration registration = this.getOne(query);
|
||||
if (registration != null) {
|
||||
registration.setStatus(RegistrationStatus.CANCELLED);
|
||||
this.updateById(registration);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户签到活动
|
||||
* @param actId 活动ID
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
@Override
|
||||
public void attendActivity(Long actId, Long userId) {
|
||||
LambdaQueryWrapper<Registration> query = new LambdaQueryWrapper<>();
|
||||
query.eq(Registration::getActId, actId).eq(Registration::getUserId, userId);
|
||||
Registration registration = this.getOne(query);
|
||||
if (registration != null && registration.getStatus() == RegistrationStatus.REGISTERED) {
|
||||
registration.setStatus(RegistrationStatus.ATTENDED);
|
||||
registration.setAttendTime(new Date(System.currentTimeMillis()));
|
||||
this.updateById(registration);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户报名的所有活动
|
||||
* @param userId 用户ID
|
||||
* @return 报名列表
|
||||
*/
|
||||
@Override
|
||||
public List<Registration> listMyRegistrations(Long userId) {
|
||||
return this.lambdaQuery().eq(Registration::getUserId, userId).list();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个活动的所有报名信息
|
||||
* @param actId 活动ID
|
||||
* @return 报名列表
|
||||
*/
|
||||
@Override
|
||||
public List<Registration> listRegistrationsForActivity(Long actId) {
|
||||
return this.lambdaQuery().eq(Registration::getActId, actId).list();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue