added UserContentController
This commit is contained in:
parent
7b70281db9
commit
9cdd14b6ca
|
@ -0,0 +1,169 @@
|
||||||
|
package studycave.studycaverestservice.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
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.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import studycave.studycaverestservice.model.material.Material;
|
||||||
|
import studycave.studycaverestservice.model.material.MaterialRepository;
|
||||||
|
import studycave.studycaverestservice.model.material.dto.MaterialGetDTO;
|
||||||
|
import studycave.studycaverestservice.model.simpleSet.SimpleSet;
|
||||||
|
import studycave.studycaverestservice.model.simpleSet.SimpleSetRepository;
|
||||||
|
import studycave.studycaverestservice.model.simpleSet.dto.SimpleSetDTO;
|
||||||
|
import studycave.studycaverestservice.model.simpleTest.SimpleTest;
|
||||||
|
import studycave.studycaverestservice.model.simpleTest.SimpleTestRepository;
|
||||||
|
import studycave.studycaverestservice.model.simpleTest.dto.SimpleTestDTO;
|
||||||
|
import studycave.studycaverestservice.model.studyGroupMember.StudyGroupMemberRepository;
|
||||||
|
import studycave.studycaverestservice.model.user.User;
|
||||||
|
import studycave.studycaverestservice.model.user.UserRepository;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@CrossOrigin
|
||||||
|
@PreAuthorize("isAuthenticated()")
|
||||||
|
@Api
|
||||||
|
public class UserContentController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
StudyGroupMemberRepository memberRepository;
|
||||||
|
@Autowired
|
||||||
|
SimpleSetRepository simpleSetRepository;
|
||||||
|
@Autowired
|
||||||
|
SimpleTestRepository simpleTestRepository;
|
||||||
|
@Autowired
|
||||||
|
ModelMapper modelMapper;
|
||||||
|
@Autowired
|
||||||
|
MaterialRepository materialRepository;
|
||||||
|
|
||||||
|
@GetMapping("/users/sets")
|
||||||
|
public ResponseEntity<?> getUserSets(@RequestParam(value = "excludedGroupId", required = false) String groupId) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String currentPrincipalName = authentication.getName();
|
||||||
|
Optional<User> user = userRepository.findByUsername(currentPrincipalName);
|
||||||
|
Integer ownerId = user.isPresent() ? user.get().getId().intValue() : null;
|
||||||
|
if (!user.isPresent())
|
||||||
|
return new ResponseEntity<>(new String("User not found"), HttpStatus.NOT_FOUND);
|
||||||
|
|
||||||
|
ArrayList<SimpleSetDTO> setDTOs = new ArrayList<SimpleSetDTO>();
|
||||||
|
List<SimpleSet> sets = simpleSetRepository.findByOptionalPermissionAndOptionalOwner(null, ownerId);
|
||||||
|
|
||||||
|
for (SimpleSet set : sets) {
|
||||||
|
Boolean isInGroup = false;
|
||||||
|
for (SimpleSet set2 : sets) {
|
||||||
|
if (groupId != null && set.getName().equals(set2.getName()) && set != set2) {
|
||||||
|
if (set.getGroupId() != null && set.getGroupId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (set2.getGroupId() != null && set2.getGroupId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isInGroup) {
|
||||||
|
SimpleSetDTO setDTO = modelMapper.map(set, SimpleSetDTO.class);
|
||||||
|
setDTO.setOwner(currentPrincipalName);
|
||||||
|
setDTOs.add(setDTO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ResponseEntity<List<SimpleSetDTO>>(setDTOs, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/users/materials")
|
||||||
|
public ResponseEntity<?> getUserMaterials(
|
||||||
|
@RequestParam(value = "excludedGroupId", required = false) String groupId) {
|
||||||
|
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String currentPrincipalName = authentication.getName();
|
||||||
|
Optional<User> user = userRepository.findByUsername(currentPrincipalName);
|
||||||
|
Integer ownerId = user.isPresent() ? user.get().getId().intValue() : null;
|
||||||
|
if (!user.isPresent())
|
||||||
|
return new ResponseEntity<>(new String("User not found"), HttpStatus.NOT_FOUND);
|
||||||
|
|
||||||
|
ArrayList<MaterialGetDTO> materialDTOs = new ArrayList<MaterialGetDTO>();
|
||||||
|
List<Material> materials = materialRepository.findByOptionalPermissionAndOptionalOwner(null, ownerId);
|
||||||
|
|
||||||
|
for (Material material : materials) {
|
||||||
|
Boolean isInGroup = false;
|
||||||
|
for (Material material2 : materials) {
|
||||||
|
if (groupId != null && material.getTitle().equals(material2.getTitle()) && material != material2) {
|
||||||
|
if (material.getGroup() != null && material.getGroup().getId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (material2.getGroup() != null && material2.getGroup().getId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isInGroup) {
|
||||||
|
MaterialGetDTO materialDTO = modelMapper.map(material, MaterialGetDTO.class);
|
||||||
|
materialDTO.setOwner(currentPrincipalName);
|
||||||
|
materialDTOs.add(materialDTO);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ResponseEntity<List<MaterialGetDTO>>(materialDTOs, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/users/tests")
|
||||||
|
public ResponseEntity<?> getUserTests(
|
||||||
|
@RequestParam(value = "excludedGroupId", required = false) String groupId) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String currentPrincipalName = authentication.getName();
|
||||||
|
Optional<User> user = userRepository.findByUsername(currentPrincipalName);
|
||||||
|
Integer ownerId = user.isPresent() ? user.get().getId().intValue() : null;
|
||||||
|
if (!user.isPresent())
|
||||||
|
return new ResponseEntity<>(new String("User not found"), HttpStatus.NOT_FOUND);
|
||||||
|
|
||||||
|
ArrayList<SimpleTestDTO> testDTOs = new ArrayList<SimpleTestDTO>();
|
||||||
|
List<SimpleTest> tests = simpleTestRepository.findByOptionalPermissionAndOptionalOwner(null, new Long(ownerId));
|
||||||
|
|
||||||
|
for (SimpleTest test : tests) {
|
||||||
|
Boolean isInGroup = false;
|
||||||
|
for (SimpleTest test2 : tests) {
|
||||||
|
if (groupId != null && test.getTitle().equals(test2.getTitle()) && test != test2) {
|
||||||
|
if (test.getGroupId() != null && test.getGroupId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test2.getGroupId() != null && test2.getGroupId() == Integer.parseInt(groupId)) {
|
||||||
|
isInGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isInGroup) {
|
||||||
|
SimpleTestDTO testDTO = modelMapper.map(test, SimpleTestDTO.class);
|
||||||
|
testDTO.setOwner(currentPrincipalName);
|
||||||
|
testDTOs.add(testDTO);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ResponseEntity<List<SimpleTestDTO>>(testDTOs, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package studycave.studycaverestservice.model.material;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface MaterialRepository extends JpaRepository<Material, Long> {
|
||||||
|
Material save(Material material);
|
||||||
|
List<Material> findAll();
|
||||||
|
|
||||||
|
|
||||||
|
@Query("select m from Material m where (:permission is null or m.permission = :permission) and "
|
||||||
|
+ "(:owner is null or m.owner = :owner)")
|
||||||
|
List<Material> findByOptionalPermissionAndOptionalOwner(@Param("permission") String permission,
|
||||||
|
@Param("owner") Integer owner);
|
||||||
|
|
||||||
|
|
||||||
|
@Query("select t from Material t where (t.permission = 'GROUP' and t.group.id = :g and t.status = 'VERIFIED' )")
|
||||||
|
List<Material> findMaterialByGroup(@Param("g") Long g);
|
||||||
|
|
||||||
|
@Query("select t from Material t where (t.permission = 'GROUP' and t.group.id = :g and t.status = 'UNVERIFIED' )")
|
||||||
|
List<Material> findWaitingMaterialByGroupKey(@Param("g") Long g);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package studycave.studycaverestservice.model.material.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class MaterialGetDTO {
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String owner;
|
||||||
|
private String permission;
|
||||||
|
@JsonProperty("add_date")
|
||||||
|
private Date addDate;
|
||||||
|
@JsonProperty("edit_date")
|
||||||
|
private Date editDate;
|
||||||
|
private int grade;
|
||||||
|
private String group;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleSet;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import studycave.studycaverestservice.model.flashcardset.Set;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "flashcardset")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class SimpleSet {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
@Column(name="id_owner")
|
||||||
|
@JsonProperty("owner")
|
||||||
|
private int idOwner;
|
||||||
|
|
||||||
|
@Column(name="add_date")
|
||||||
|
@JsonProperty("add_date")
|
||||||
|
private Date addDate;
|
||||||
|
|
||||||
|
@Column(name="edit_date")
|
||||||
|
@JsonProperty("edit_date")
|
||||||
|
private Date editDate;
|
||||||
|
|
||||||
|
private int grade;
|
||||||
|
|
||||||
|
private String permission;
|
||||||
|
|
||||||
|
@Column(name="group_id")
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
private Integer groupId;
|
||||||
|
|
||||||
|
|
||||||
|
public SimpleSet(String name, String category, int idOwner) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.category = category;
|
||||||
|
this.idOwner = idOwner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleSet(Set set) {
|
||||||
|
super();
|
||||||
|
this.name = set.getName();
|
||||||
|
this.category = set.getCategory();
|
||||||
|
this.idOwner = set.getIdOwner();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleSet;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface SimpleSetRepository extends JpaRepository<SimpleSet, Long> {
|
||||||
|
SimpleSet save(SimpleSet set);
|
||||||
|
|
||||||
|
List<SimpleSet> findAll();
|
||||||
|
|
||||||
|
Optional<SimpleSet> findById(Long id);
|
||||||
|
|
||||||
|
|
||||||
|
@Query("select s from SimpleSet s where (:permission is null or s.permission = :permission) and "
|
||||||
|
+ "(:owner is null or s.idOwner = :owner)")
|
||||||
|
List<SimpleSet> findByOptionalPermissionAndOptionalOwner(@Param("permission") String permission,
|
||||||
|
@Param("owner") Integer owner);
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleSet.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class SimpleSetDTO {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
@JsonProperty("owner")
|
||||||
|
private String owner;
|
||||||
|
|
||||||
|
@JsonProperty("add_date")
|
||||||
|
private Date addDate;
|
||||||
|
|
||||||
|
@JsonProperty("edit_date")
|
||||||
|
private Date editDate;
|
||||||
|
|
||||||
|
private int grade;
|
||||||
|
|
||||||
|
private String permission;
|
||||||
|
|
||||||
|
private String group;
|
||||||
|
|
||||||
|
public SimpleSetDTO(String name, String category, String owner) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.category = category;
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleTest;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import studycave.studycaverestservice.model.test.Test;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "test")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SimpleTest {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(name="id_owner")
|
||||||
|
@JsonProperty("owner")
|
||||||
|
private Long idOwner;
|
||||||
|
|
||||||
|
@Column(name="add_date")
|
||||||
|
@JsonProperty("add_date")
|
||||||
|
private Date addDate;
|
||||||
|
|
||||||
|
@Column(name="edit_date")
|
||||||
|
@JsonProperty("edit_date")
|
||||||
|
private Date editDate;
|
||||||
|
|
||||||
|
private int grade;
|
||||||
|
|
||||||
|
private String permission;
|
||||||
|
|
||||||
|
@Column(name="group_id")
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
private Integer groupId;
|
||||||
|
|
||||||
|
public SimpleTest(String title,Long idOwner) {
|
||||||
|
super();
|
||||||
|
this.title = title;
|
||||||
|
this.idOwner = idOwner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleTest(Test test) {
|
||||||
|
super();
|
||||||
|
this.title = test.getTitle();
|
||||||
|
this.idOwner = test.getIdOwner();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleTest;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface SimpleTestRepository extends JpaRepository<SimpleTest, Long> {
|
||||||
|
List<SimpleTest> findAll();
|
||||||
|
|
||||||
|
@Query("select t from SimpleTest t where (:permission is null or t.permission = :permission) and "
|
||||||
|
+ "(:owner is null or t.idOwner = :owner)")
|
||||||
|
List<SimpleTest> findByOptionalPermissionAndOptionalOwner(@Param("permission") String permission,
|
||||||
|
@Param("owner") Long owner);
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package studycave.studycaverestservice.model.simpleTest.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class SimpleTestDTO {
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String owner;
|
||||||
|
@JsonIgnore
|
||||||
|
private Long idOwner;
|
||||||
|
private Date addDate;
|
||||||
|
private Date editDate;
|
||||||
|
@ApiModelProperty(value = "Default value for note", required = true,example = "public")
|
||||||
|
private String permission;
|
||||||
|
private Long grade;
|
||||||
|
private String group;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue