Merge pull request 'master' (#7) from master into main

Reviewed-on: #7
This commit is contained in:
wzp 2024-04-25 09:48:20 +00:00
commit 6f87eeb619
18 changed files with 236 additions and 12 deletions

View File

@ -0,0 +1,26 @@
package org.mmga.clubs.controller;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.mmga.clubs.entities.BaseResponse;
import org.mmga.clubs.entities.club.ClubInformation;
import org.mmga.clubs.service.ClubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/club")
@Tag(name = "社团", description = "社团相关接口")
public class ClubController {
private final ClubService clubService;
@Autowired
public ClubController(ClubService clubService) {
this.clubService = clubService;
}
@GetMapping("/")
public BaseResponse<ClubInformation> getClub(@RequestParam("id") int clubId){
return clubService.getClubInformationById(clubId);
}
}

View File

@ -1,7 +1,6 @@
package org.mmga.clubs.dao;
import org.apache.ibatis.annotations.Mapper;
import org.mmga.clubs.entities.permission.Permission;
import org.mmga.clubs.entities.permission.PermissionVo;
import java.util.List;

View File

@ -0,0 +1,9 @@
package org.mmga.clubs.dao;
import org.apache.ibatis.annotations.Mapper;
import org.mmga.clubs.entities.club.ClubVo;
@Mapper
public interface ClubDao {
ClubVo getClubById(int id);
}

View File

@ -0,0 +1,12 @@
package org.mmga.clubs.dao;
import org.apache.ibatis.annotations.Mapper;
import org.mmga.clubs.entities.ClubUserAuthVo;
import java.util.List;
@Mapper
public interface ClubUserAuthDao {
ClubUserAuthVo getClubUserAuthVoByUserId(int userId);
List<ClubUserAuthVo> getUsersByClubId(int clubId);
}

View File

@ -1,9 +1,6 @@
package org.mmga.clubs.dao;
import org.apache.ibatis.annotations.Mapper;
import org.mmga.clubs.entities.permission.Permission;
import java.util.List;
@Mapper
public interface PermissionDao {

View File

@ -2,6 +2,7 @@ package org.mmga.clubs.dao;
import org.apache.ibatis.annotations.Mapper;
import org.mmga.clubs.entities.user.UserRegResponseVo;
import org.mmga.clubs.entities.user.UserSimpleInformation;
import org.mmga.clubs.entities.user.UserVo;
import java.util.List;
@ -20,4 +21,6 @@ public interface UserDao {
void changePasswordAdmin(int id, String password);
void changeAuth(int id, int authId);
void changeAvatar(int id, String sha);
UserSimpleInformation getUserSimpleInformation(int userId);
}

View File

@ -0,0 +1,6 @@
package org.mmga.clubs.entities;
import java.util.Date;
public record ClubUserAuthVo(int userId, int clubId, int authId, Date createTime, Date updateTime) {
}

View File

@ -1,7 +0,0 @@
package org.mmga.clubs.entities;
import java.util.Date;
public record ClubUserVo(int clubId, int userId, Date createTime, Date updateTime) {
}

View File

@ -0,0 +1,18 @@
package org.mmga.clubs.entities.club;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.mmga.clubs.entities.auth.Auth;
@Data
@Schema(description = "社团信息")
public class Club {
@Schema(description = "社团ID")
private int id;
@Schema(description = "社团名称")
private String name;
@Schema(description = "社团简介")
private String commit;
@Schema(description = "用户在社团中所属权限组")
private Auth auth;
}

View File

@ -0,0 +1,20 @@
package org.mmga.clubs.entities.club;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.mmga.clubs.entities.user.ClubUserSimpleInformation;
import java.util.List;
@Data
@Schema(description = "社团详细详细")
public class ClubInformation {
@Schema(description = "社团ID")
private int id;
@Schema(description = "社团名称")
private String name;
@Schema(description = "社团简介")
private String commit;
@Schema(description = "社团用户信息")
private List<ClubUserSimpleInformation> users;
}

View File

@ -0,0 +1,15 @@
package org.mmga.clubs.entities.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "社团用户简单信息")
public class ClubUserSimpleInformation extends UserSimpleInformation{
@Schema(description = "社团ID")
private int clubId;
@Schema(description = "社团职务名称")
private String clubAuthName;
}

View File

@ -3,6 +3,7 @@ package org.mmga.clubs.entities.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.mmga.clubs.entities.auth.Auth;
import org.mmga.clubs.entities.club.Club;
@Data
@Schema(description = "用户")
@ -15,4 +16,6 @@ public class User {
private Auth auth;
@Schema(description = "用户头像SHA1值")
private String avatar;
@Schema(description = "用户所属社团")
private Club club;
}

View File

@ -0,0 +1,15 @@
package org.mmga.clubs.entities.user;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "用户基础信息")
public class UserSimpleInformation {
@Schema(description = "用户ID")
private int id;
@Schema(description = "用户名")
private String name;
@Schema(description = "用户头像SHA1")
private String avatar;
}

View File

@ -0,0 +1,77 @@
package org.mmga.clubs.service;
import org.mmga.clubs.dao.ClubDao;
import org.mmga.clubs.dao.ClubUserAuthDao;
import org.mmga.clubs.entities.BaseResponse;
import org.mmga.clubs.entities.ClubUserAuthVo;
import org.mmga.clubs.entities.auth.Auth;
import org.mmga.clubs.entities.club.Club;
import org.mmga.clubs.entities.club.ClubInformation;
import org.mmga.clubs.entities.club.ClubVo;
import org.mmga.clubs.entities.user.ClubUserSimpleInformation;
import org.mmga.clubs.entities.user.UserSimpleInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ClubService {
private final ClubUserAuthDao clubUserAuthDao;
private final ClubDao clubDao;
private final AuthService authService;
private final UserService userService;
@Autowired
public ClubService(ClubUserAuthDao clubUserAuthDao, ClubDao clubDao, AuthService authService, @Lazy UserService userService){
this.clubDao = clubDao;
this.clubUserAuthDao = clubUserAuthDao;
this.authService = authService;
this.userService = userService;
}
public Club getClubWithoutAuthById(int id){
ClubVo clubById = this.clubDao.getClubById(id);
if (clubById == null){
return null;
}
Club club = new Club();
club.setId(id);
club.setName(clubById.name());
club.setCommit(clubById.commit());
return club;
}
public Club getClubByUserId(int userId){
ClubUserAuthVo clubUserAuthVoByUserId = this.clubUserAuthDao.getClubUserAuthVoByUserId(userId);
int i = clubUserAuthVoByUserId.authId();
Auth auth = i != -1 ? authService.getAuthById(i) : null;
Club clubById = getClubWithoutAuthById(clubUserAuthVoByUserId.clubId());
clubById.setAuth(auth);
return clubById;
}
private List<ClubUserSimpleInformation> getUserInformationByClubId(int clubId){
List<ClubUserAuthVo> usersByClubId = this.clubUserAuthDao.getUsersByClubId(clubId);
return usersByClubId.stream().map(this::packageInformation).toList();
}
private ClubUserSimpleInformation packageInformation(ClubUserAuthVo clubUserAuthVo){
int i = clubUserAuthVo.authId();
String authName = i != -1 ? authService.getAuthById(i).getName() : "社员";
UserSimpleInformation userSimpleInformation = userService.getUserSimpleInformation(clubUserAuthVo.userId());
ClubUserSimpleInformation clubUserSimpleInformation = new ClubUserSimpleInformation();
clubUserSimpleInformation.setId(userSimpleInformation.getId());
clubUserSimpleInformation.setName(userSimpleInformation.getName());
clubUserSimpleInformation.setAvatar(userSimpleInformation.getAvatar());
clubUserSimpleInformation.setClubId(clubUserAuthVo.clubId());
clubUserSimpleInformation.setClubAuthName(authName);
return clubUserSimpleInformation;
}
public BaseResponse<ClubInformation> getClubInformationById(int clubId) {
Club clubWithoutAuthById = getClubWithoutAuthById(clubId);
ClubInformation clubInformation = new ClubInformation();
clubInformation.setId(clubWithoutAuthById.getId());
clubInformation.setName(clubWithoutAuthById.getName());
clubInformation.setCommit(clubWithoutAuthById.getCommit());
clubInformation.setUsers(this.getUserInformationByClubId(clubId));
return BaseResponse.success(clubInformation);
}
}

View File

@ -45,9 +45,10 @@ public class UserService {
private final File tempDir;
private final AvatarDao avatarDao;
private final File avatarDir;
private final ClubService clubService;
@Autowired
public UserService(UserDao userDao, AuthService authService, JwtUtils jwtUtils, VerifyCodeService verifyCodeService, File tempDir, AvatarDao avatarDao, @Qualifier("avatarDir") File avatarDir){
public UserService(UserDao userDao, AuthService authService, JwtUtils jwtUtils, VerifyCodeService verifyCodeService, File tempDir, AvatarDao avatarDao, @Qualifier("avatarDir") File avatarDir, ClubService clubService){
this.userDao = userDao;
this.authService = authService;
this.jwtUtils = jwtUtils;
@ -55,6 +56,7 @@ public class UserService {
this.tempDir = tempDir;
this.avatarDao = avatarDao;
this.avatarDir = avatarDir;
this.clubService = clubService;
}
public BaseResponse<Boolean> login(UserLoginVo user, HttpServletResponse response) {
@ -105,6 +107,7 @@ public class UserService {
result.setName(vo.name());
result.setAvatar(vo.avatar());
result.setAuth(authService.getAuthById(vo.auth()));
result.setClub(clubService.getClubByUserId(vo.id()));
return result;
}
@ -227,4 +230,8 @@ public class UserService {
fileInputStream.close();
}
}
public UserSimpleInformation getUserSimpleInformation(int userId) {
return userDao.getUserSimpleInformation(userId);
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mmga.clubs.dao.ClubDao">
<select id="getClubById" resultType="org.mmga.clubs.entities.club.ClubVo">
select * from `club` where `id` = #{id};
</select>
</mapper>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mmga.clubs.dao.ClubUserAuthDao">
<select id="getClubUserAuthVoByUserId" resultType="org.mmga.clubs.entities.ClubUserAuthVo">
select * from `club_user_auth` where `user_id` = #{userId};
</select>
<select id="getUsersByClubId" resultType="org.mmga.clubs.entities.ClubUserAuthVo">
select * from `club_user_auth` where `club_id` = #{clubId};
</select>
</mapper>

View File

@ -36,4 +36,7 @@
<select id="queryTotalUserCount" resultType="java.lang.Long">
select count(*) from `user`;
</select>
<select id="getUserSimpleInformation" resultType="org.mmga.clubs.entities.user.UserSimpleInformation">
select `id`, `name`, `avatar` from `user` where `id` = #{userId};
</select>
</mapper>