Skip to content

Commit

Permalink
完成控制器类的设计以及数据传送模型类
Browse files Browse the repository at this point in the history
  • Loading branch information
YangZX1428 committed Mar 27, 2021
1 parent 9dabb51 commit 185b8b4
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.eepractice.demo.controller;

import com.eepractice.demo.bean.AuthResponse;
import com.eepractice.demo.bean.User;
import com.eepractice.demo.forms.AuthForm;
import com.eepractice.demo.properties.CodeProperty;
import com.eepractice.demo.service.AuthServiceImpl;
import com.eepractice.demo.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/auth")
@CrossOrigin(origins = "*")
public class AuthController {


@Autowired
CodeProperty codeProperty;


@Autowired
AuthServiceImpl authService;

@Autowired
CommonUtils commonUtils;

/**
* 处理登录的控制器
* @param authForm
* @return
*/
@PostMapping(value = "/login",produces = "application/json")
public ResponseEntity<?> processLogin(@RequestBody AuthForm authForm){

System.out.println(authForm);
// authForm为登录数据表单
Map<String ,Object> responseMap = new HashMap<>();
// 用户名或密码为空
if(authForm.empty()){
responseMap.put("result", new AuthResponse(null,codeProperty.PARAMETER_ERROR,"参数错误",null));
}else{
Optional<User> userOptional = authService.login(authForm.getUsername(), authForm.getPassword(),authForm.getRole());
// 登录失败
if (!userOptional.isPresent()){
responseMap.put("result",new AuthResponse(null,codeProperty.LOGIN_FAILED,"用户名不存在或密码错误",null));
}else{
// 登录成功
User user = userOptional.get();
//生成token
responseMap.put("result",new AuthResponse(user,codeProperty.SUCCESS,"登录成功!", commonUtils.generateJwtToken(user)));
}
}
return ResponseEntity.ok(responseMap);
}


@PostMapping(value = "/register",produces = "application/json")
public ResponseEntity<?> processRegister(@RequestBody AuthForm authForm){
// authForm为登录数据表单
Map<String ,Object> responseMap = new HashMap<>();
// 用户名或密码为空
if(authForm.empty()){
responseMap.put("result", new AuthResponse(null,codeProperty.PARAMETER_ERROR,"参数错误",null));
}else{
boolean registerResult = authService.register(authForm.getUsername(), authForm.getPassword());
if (!registerResult) responseMap.put("result",new AuthResponse(null,codeProperty.REGISTER_FAILED,"注册失败",null));
else{
responseMap.put("result",new AuthResponse(null,codeProperty.SUCCESS,"注册成功",null));
}
}
return ResponseEntity.ok(responseMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.eepractice.demo.controller;

import com.eepractice.demo.bean.CommonResponse;
import com.eepractice.demo.bean.Notification;
import com.eepractice.demo.bean.SubForum;
import com.eepractice.demo.bean.User;
import com.eepractice.demo.forms.InfoForm;
import com.eepractice.demo.properties.CodeProperty;
import com.eepractice.demo.properties.RoleProperty;
import com.eepractice.demo.repository.NotificationRepository;
import com.eepractice.demo.repository.SubForumRepository;
import com.eepractice.demo.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/default")
@CrossOrigin(origins = "*")
public class DefaultController {

@Autowired
CodeProperty codeProperty;

@Autowired
RoleProperty roleProperty;

@Autowired
SubForumRepository subForumRepository;

@Autowired
CommonUtils commonUtils;

@Autowired
NotificationRepository notificationRepository;
/**
* 获取首页需要的数据
* @return
*/

@GetMapping(value = "/info",produces = "application/json")
public ResponseEntity<?> processInfo(){
List<InfoForm> infoFormList = new ArrayList<>();
Map<String,Object> responseMap = new HashMap<>();
// 分论坛信息
Iterable<SubForum> subForums = subForumRepository.findAll();
for (SubForum subForum : subForums) {
infoFormList.add(commonUtils.transformSubForum(subForum));
}
responseMap.put("data",infoFormList);
return ResponseEntity.ok(responseMap);
}


/**
* 发布通知,只有主席或秘书才能发布
*/
@PostMapping(value = "/add/{id}",consumes = "application/json")
public ResponseEntity<?> processAddNotification(@RequestBody Notification notification,
@RequestHeader("Authorization") String token,
@PathVariable("id") Integer id) {
String newToken = token.replaceAll("Basic ","");
// 用户验证信息无效
User user = commonUtils.parseJwtToken(newToken);
System.out.println(user.getId());
if (user == null) return CommonResponse.getMap(codeProperty.TOKEN_INVALID,"用户验证信息无效",null);
SubForum subForum = subForumRepository.findById(id).get();
// 只有分论坛的主席和秘书能发布通知
if (user.getId().equals(subForum.getChairManId()) || user.getId().equals(subForum.getSecretaryId())) {
// 用户有效,发布通知
notification.setSubForumId(id);
List<User> members = subForum.getMembers();
String unreadUsers = commonUtils.parseUserList(members,subForum.getChairManId(),subForum.getSecretaryId());
notification.setUnreadUsers(unreadUsers);
notificationRepository.save(notification);
return CommonResponse.getMap(codeProperty.SUCCESS,"发布成功!",null);
}
return CommonResponse.getMap(codeProperty.NO_PERMISSIONS,"用户权限不足",null);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.eepractice.demo.controller;

import com.eepractice.demo.bean.CommonResponse;
import com.eepractice.demo.bean.Notification;
import com.eepractice.demo.bean.SubForum;
import com.eepractice.demo.bean.User;
import com.eepractice.demo.forms.NotificationForm;
import com.eepractice.demo.properties.CodeProperty;
import com.eepractice.demo.repository.NotificationRepository;
import com.eepractice.demo.repository.SubForumRepository;
import com.eepractice.demo.repository.UserRepository;
import com.eepractice.demo.utils.CommonUtils;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/note")
@CrossOrigin(origins = "*")
public class NotificationController {


@Autowired
NotificationRepository notificationRepository;

@Autowired
UserRepository userRepository;

@Autowired
SubForumRepository subForumRepository;

@Autowired
CommonUtils commonUtils;

@Autowired
CodeProperty codeProperty;

/**
* 接受用户id查看是否有通知
* @return
*/
@GetMapping(value = "/has_note/{userId}",produces = "application/json")
public ResponseEntity<?> processHasNote(@PathVariable("userId") Integer userId){
String uId = String.valueOf(userId);
List<Notification> notifications = notificationRepository.findNotificationsByUnreadUsersContains(uId);
List<NotificationForm> resultList = new ArrayList<>();
for (Notification notification : notifications) {
NotificationForm notificationForm = new NotificationForm();
notificationForm.setTitle(notification.getTitle()).setContent(notification.getContent());
SubForum subForum = subForumRepository.findById(notification.getSubForumId()).get();
User chairman = userRepository.findById(subForum.getId()).get();
notificationForm.setBelongSubForum(subForum.getTitle()).setBelongChairMan(chairman.getUsername())
.setId(notification.getId());
resultList.add(notificationForm);
}
return ResponseEntity.ok(resultList);
}


@GetMapping(value = "/read/{noteId}",produces = "application/json")
public ResponseEntity<?> processReadNote(@PathVariable("noteId") Integer noteId,@RequestHeader("Authorization") String token){
User user = commonUtils.parseJwtToken(token);
if (user == null) return CommonResponse.getMap(codeProperty.TOKEN_INVALID,"用户验证信息无效",null);
// 该用户已读通知
Notification notification = notificationRepository.findById(noteId).get();
notification.handleMsg(user.getId());
notificationRepository.save(notification);
return CommonResponse.getMap(codeProperty.SUCCESS,"操作成功!",null);
}

}
17 changes: 17 additions & 0 deletions web_backend/src/main/java/com/eepractice/demo/forms/AuthForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.eepractice.demo.forms;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AuthForm {
private String username;
private String password;
private String role;
public boolean empty(){
return this.username.trim().equals("") || this.password.trim().equals("") || this.role.equals("");
}
}
19 changes: 19 additions & 0 deletions web_backend/src/main/java/com/eepractice/demo/forms/InfoForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.eepractice.demo.forms;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class InfoForm {
private Integer id;
private String title;
private String chairman;
private String secretary;
private Integer memberNum;
private String startDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.eepractice.demo.forms;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class NotificationForm {
Integer id;
String title;
String content;
String belongSubForum;
String belongChairMan;
}

0 comments on commit 185b8b4

Please sign in to comment.