studentService大体构建完成,辅以一些工具类

This commit is contained in:
chuyan 2020-09-09 17:03:23 +08:00
parent 0bf91bd2ed
commit 74559712bc
20 changed files with 278 additions and 37 deletions

View File

@ -51,7 +51,7 @@ public class ASEJSONTokenAuthenticationFilter extends OncePerRequestFilter {
// 服务端API测试豁免签名
if(signed != null && signed.equals("6d4923fca4dcb51f67b85e54a23a8d763d9e02af")){
//执行授权
doAuthentication("u_id_88883b9e023c8824310760d8bb8b6542e5a3f16a0d67253214e01ee7ab0e96a1", request);
doAuthentication("u_id_c70233fd7cb1f8a770c440975a0d13ad4013d3631b6f93c0037c7294bbc42140", request);
}
// 正常认证
else if (signed != null && username != null && timestamp != null) {

View File

@ -18,6 +18,7 @@ import javax.servlet.http.HttpServletRequest;
import java.util.Optional;
@Controller
@RequestMapping("register")
public class RegisterController {
@Resource
private UserService userService;
@ -31,13 +32,22 @@ public class RegisterController {
@Resource
private QuickJSONRespond quickJSONRespond;
@RequestMapping(value = "/register")
@RequestMapping(value = "")
String registerView(Model model){
return "register";
}
@PostMapping(value="baseinfo")
@ResponseBody
String addBaseInfo(){
BaseStudentInfo bsi = baseInformationService.constructStudentInfo("2018303209", "14011809", "Jack",
"","软件学院","软件工程","汉族","群众","未知");
baseInformationService.save(bsi);
return "OK";
}
// 处理注册表单
@PostMapping(value = "/register/do_register")
@PostMapping(value = "user")
@ResponseBody
String doRegister(HttpServletRequest request){

View File

@ -4,24 +4,23 @@ import com.codesdream.ase.model.student.Comment;
import com.codesdream.ase.repository.student.CommentRepository;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController("test")
@RestController
@RequestMapping("test")
public class TestController {
@Resource
CommentRepository commentRepository;
private CommentRepository commentRepository;
@RequestMapping
@GetMapping("")
@ResponseStatus(HttpStatus.OK)
public boolean test() throws InterruptedException {
int userId = 1;
String userId = "1";
for (int i = 0;i < 10; i++){
Comment comment = new Comment();
comment.setUserId(userId);
@ -30,8 +29,11 @@ public class TestController {
Thread.sleep(20);
}
Thread.sleep(1000);
List<Comment> comments = commentRepository.findByUserId(new Integer(userId).toString(),
Sort.by(Sort.Direction.ASC, "date"));
List<Comment> comments = commentRepository.findByUserId(userId,
Sort.by(Sort.Direction.DESC, "date"));
if(!comments.isEmpty()){
System.out.println(comments.toString());
}
return true;
}
}

View File

@ -16,4 +16,8 @@ public class DataInvalidFormatException extends FormatException {
public DataInvalidFormatException(){
super();
}
public DataInvalidFormatException(String s) {
information = s;
}
}

View File

@ -0,0 +1,16 @@
package com.codesdream.ase.exception.innerservererror;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class InvalidDataException extends FormatException{
private String message;
public InvalidDataException(String message){
super(message);
this.message = message;
}
}

View File

@ -20,10 +20,10 @@ public class Activity {
String title;
@ManyToOne
User Creator;
User creator;
@ManyToOne
User Manager;
@ManyToMany
List<User> manager;
@ElementCollection
List<Integer> participantIds = new ArrayList<>();

View File

@ -25,6 +25,6 @@ public class Message {
int type;
@OneToMany
User Receiver;
List<User> Receiver;
}

View File

@ -14,10 +14,5 @@ import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
public class Parent extends User {
@Column(nullable = false)
String parentId;
@Column(nullable = false)
String name;
}

View File

@ -1,5 +1,6 @@
package com.codesdream.ase.model.permission;
import com.codesdream.ase.model.file.Image;
import com.codesdream.ase.model.mark.Tag;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;

View File

@ -1,5 +1,6 @@
package com.codesdream.ase.model.permission;
import com.codesdream.ase.model.file.Image;
import com.codesdream.ase.model.information.*;
import lombok.Data;
@ -47,4 +48,13 @@ public class UserDetail {
// 在校认证状态
private boolean atSchool = false;
// 电话
private String telNum;
// 邮箱
private String email;
@OneToOne
private Image profilePic;
}

View File

@ -13,8 +13,13 @@ import java.util.List;
@Table
@Data
public class Faq {
@Id
int id;
String answer;
String question;
@ElementCollection
List<String> tag;
}

View File

@ -14,7 +14,7 @@ public class Comment {
int id;
// 发出此评论的用户
int userId;
String userId;
// 评论内容
String context;

View File

@ -15,7 +15,7 @@ public class Honor {
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
String studentId;
int studentId;
// 荣誉描述
String description;

View File

@ -1,10 +1,13 @@
package com.codesdream.ase.model.student;
import com.codesdream.ase.model.file.File;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.List;
@Entity
@Table
@ -17,5 +20,7 @@ public class Notification {
String title;
@ManyToMany
List<File> files;
}

View File

@ -6,21 +6,24 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
public class Student extends User {
@Column(nullable = false)
String studentId;
@Column(nullable = false)
String name;
String profilePicture;
@Column(nullable = false)
Boolean is_showgrade;
Boolean isShowGrade = true;
@ElementCollection
Map<String, Boolean> privacy = new HashMap<String, Boolean>(){{
put("score", true);
put("attendance", true);
put("step", true);
put("honor", true);
}};
}

View File

@ -10,9 +10,5 @@ import java.util.Optional;
@Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
Optional<Student> findByStudentId(String studentId);
List<Student> findByName(String name, Sort sort);
List<Student> findByName(String name);
}

View File

@ -0,0 +1,41 @@
package com.codesdream.ase.service.activity;
import com.codesdream.ase.model.activity.Activity;
import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.repository.activity.ActivityRepository;
import com.codesdream.ase.repository.permission.UserRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Optional;
@Service
public class ActivityService {
@Resource
ActivityRepository activityRepository;
@Resource
UserRepository userRepository;
public Activity addMember(int activityId, int memberId, boolean type){
Optional<Activity> optionalActivity = activityRepository.findById(activityId);
Optional<User> optionalUser = userRepository.findById(memberId);
if(!optionalActivity.isPresent() || !optionalUser.isPresent()){
return null;
}
Activity activity = optionalActivity.get();
User user = optionalUser.get();
if(!type){
activity.getManager().add(user);
}
else{
activity.getParticipantIds().add(user.getId());
}
return activityRepository.save(activity);
}
}

View File

@ -0,0 +1,135 @@
package com.codesdream.ase.service.student;
import com.codesdream.ase.exception.innerservererror.DataInvalidFormatException;
import com.codesdream.ase.exception.innerservererror.InvalidDataException;
import com.codesdream.ase.exception.notfound.NotFoundException;
import com.codesdream.ase.model.activity.Activity;
import com.codesdream.ase.model.file.File;
import com.codesdream.ase.model.file.Image;
import com.codesdream.ase.model.permission.UserDetail;
import com.codesdream.ase.model.student.Honor;
import com.codesdream.ase.model.student.Notification;
import com.codesdream.ase.model.student.Student;
import com.codesdream.ase.repository.student.HonorRepository;
import com.codesdream.ase.repository.student.NotificationRepository;
import com.codesdream.ase.repository.student.StudentRepository;
import com.codesdream.ase.service.activity.ActivityService;
import com.codesdream.ase.validator.GeneralValidator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
public class StudentService {
@Resource
StudentRepository studentRepository;
@Resource
NotificationRepository notificationRepository;
@Resource
ActivityService activityService;
@Resource
HonorRepository honorRepository;
public Notification createNotification(String title, String description, List<File> files){
Notification notification = new Notification();
notification.setTitle(title);
notification.setContext(description);
notification.setFiles(files);
return notificationRepository.save(notification);
}
public boolean cancelNotification(int notificationId){
Optional<Notification> notification = notificationRepository.findById(notificationId);
if(notification.isPresent()){
notificationRepository.delete(notification.get());
return true;
}
else{
throw new NotFoundException("No such notification.");
}
}
public Activity attendActivity(int studentId, int activityId){
return activityService.addMember(activityId, studentId, true);
}
public Honor createHonor(int studentId, String description, List<Image> images){
Honor honor = new Honor();
honor.setDescription(description);
honor.setImages(images);
honor.setLastModification(honor.getCreationDate());
honor.setStudentId(studentId);
return honorRepository.save(honor);
}
public Honor updateHonor(int honorId, String description, List<Image> images){
Optional<Honor> optionalHonor = honorRepository.findById(honorId);
if(!optionalHonor.isPresent()){
throw new NotFoundException("No such honor.");
}
Honor honor = optionalHonor.get();
if(!description.isEmpty()){
honor.setDescription(description);
}
if(!images.isEmpty()){
honor.setImages(images);
}
return honorRepository.save(honor);
}
public boolean updatePrivacy(int studentId, List<String> privacyList){
Optional<Student> optionalStudent = studentRepository.findById(studentId);
if(optionalStudent.isPresent()){
Student student = optionalStudent.get();
if(student.getPrivacy().keySet().containsAll(privacyList)){
Map<String, Boolean> privacyMap = student.getPrivacy();
for(String privacy : privacyList){
privacyMap.put(privacy, true);
}
return true;
}
else {
throw new DataInvalidFormatException("Invalid privacy with illegal phrases.");
}
}
else {
throw new NotFoundException("No such privacy.");
}
}
public Student editProfile(int studentId, String telNum, Image profilePic, String email){
Optional<Student> optionalStudent = studentRepository.findById(studentId);
if(!optionalStudent.isPresent()){
throw new NotFoundException("No such student.");
}
if(!GeneralValidator.isTelNumber(telNum) && !telNum.equals("0")){
throw new InvalidDataException("Invalid telephone number.");
}
Student student = optionalStudent.get();
UserDetail userDetail = student.getUserDetail();
if(!telNum.equals("0")){
userDetail.setTelNum(telNum);
}
if(profilePic!=null){
userDetail.setProfilePic(profilePic);
}
if(!email.isEmpty()){
userDetail.setEmail(email);
}
return studentRepository.save(student);
}
}

View File

@ -0,0 +1,18 @@
package com.codesdream.ase.validator;
import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
public class GeneralValidator {
public static boolean isTelNumber (String telNum){
String reg = "^[1](([3][0-9])|([4][5,7,9])|([5][^4,6,9])|([6][6])" +
"|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(telNum);
return m.find();
}
}

View File

@ -19,8 +19,8 @@ public class JpaTest {
@Test
public void test() throws InterruptedException {
CommentRepository commentRepository = aseSpringUtil.getBean(CommentRepository.class);
int userId = 1;
/*CommentRepository commentRepository = aseSpringUtil.getBean(CommentRepository.class);
String userId = "1";
for (int i = 0;i < 10; i++){
Comment comment = new Comment();
comment.setUserId(userId);
@ -30,6 +30,6 @@ public class JpaTest {
}
Thread.sleep(1000);
List<Comment> comments = commentRepository.findByUserId(new Integer(userId).toString(),
Sort.by(Sort.Direction.ASC, "date"));
Sort.by(Sort.Direction.ASC, "date"));*/
}
}