dodanie listy aktywności

This commit is contained in:
yetju000 2019-12-08 11:33:18 +01:00
parent e38aa097c1
commit e815b8ebc4
3 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,118 @@
package studycave.studycaverestservice.controller;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import studycave.studycaverestservice.model.studyGroup.GroupRepository;
import studycave.studycaverestservice.model.user.UserRepository;
import studycave.studycaverestservice.model.userActivity.SortType;
import studycave.studycaverestservice.model.userActivity.UserActivity;
import studycave.studycaverestservice.model.userActivity.UserActivityDTO;
import studycave.studycaverestservice.model.userActivity.UserActivityRepository;
@RestController
@CrossOrigin
@Api
@PreAuthorize("isAuthenticated()")
public class UserActivityController {
@Autowired
UserRepository userRepository;
@Autowired
GroupRepository groupRepository;
@Autowired
UserActivityRepository userActivityRepository;
@GetMapping("/groups/{groupId}/users/activity")
public ResponseEntity<?> getUserActivity(@PathVariable(required = true) Long groupId,
@RequestParam(value = "startDate", required = false) Date startDate,
@RequestParam(value = "endDate", required = false) Date endDate,
@RequestParam(value = "sort", required = false) SortType sort) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
Long userId = (userRepository.findByUsername(currentPrincipalName).orElse(null)).getId();
Timestamp endDateTime = null;
if (endDate != null ) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(endDate.getTime());
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 0);
endDateTime = new Timestamp(c.getTimeInMillis());
}
List<UserActivity> userActivity = this.userActivityRepository
.findAllByToUserOrFromUserAndGroupAndTime(userId, groupId, startDate, endDateTime);
List<UserActivityDTO> list = new ArrayList<UserActivityDTO>();
Collections.sort(userActivity, new Comparator<UserActivity>() {
public int compare(UserActivity o1, UserActivity o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
for (UserActivity u : userActivity) {
String resourceType = null;
String resourceName = null;
if (u.getMaterial() != null) {
resourceType = "material";
resourceName = u.getMaterial().getTitle();
}
if (u.getTest() != null) {
resourceType = "test";
resourceName = u.getTest().getTitle();
}
if (u.getSet() != null) {
resourceType = "flashcards";
resourceName = u.getSet().getName();
}
UserActivityDTO dto;
dto = new UserActivityDTO(
u.getId(),
u.getType(),
u.getPoints(),
u.getComment(),
new SimpleDateFormat("yyyy-MM-dd").format(u.getDate()),
resourceType,
resourceName,
u.getFromUser() != null ? u.getFromUser().getUsername() : null,
u.getToUser() != null ? u.getToUser().getUsername() : null
);
list.add(dto);
}
if (sort == SortType.DESC) {
Collections.reverse(list);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}
}

View File

@ -0,0 +1,5 @@
package studycave.studycaverestservice.model.userActivity;
public enum SortType {
ASC, DESC;
}

View File

@ -0,0 +1,112 @@
package studycave.studycaverestservice.model.userActivity;
public class UserActivityDTO {
private Long id;
private String type;
private float points;
private String comment;
private String date;
private String resourceType;
private String resourceName;
private String from;
private String to;
public UserActivityDTO(Long id, String type, float points, String comment, String date, String resourceType,
String resourceName, String from, String to) {
super();
this.id = id;
this.type = type;
this.points = points;
this.comment = comment;
this.date = date;
this.resourceType = resourceType;
this.resourceName = resourceName;
this.from = from;
this.to = to;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public float getPoints() {
return points;
}
public void setPoints(float points) {
this.points = points;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getResourceType() {
return resourceType;
}
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}