v0.2.10 完成活动报名点赞评论模块构建

v0.2o
bruce 2025-02-21 15:40:40 +08:00
parent 3540bd8de9
commit 6232ada60d
9 changed files with 427 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}