Merge with assign

This commit is contained in:
Filip Izydorczyk 2020-10-01 16:46:45 +02:00
commit 398a84854f
14 changed files with 395 additions and 32 deletions

View File

@ -0,0 +1,37 @@
package com.plannaplan.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Assignment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "group_id")
private Groups group;
@ManyToOne
@JoinColumn(name = "commision_id")
private Commision commision;
public Assignment(Groups group, Commision commision) {
this.commision = commision;
this.group = group;
}
public Assignment() {
}
public Long getId() {
return this.id;
}
public Groups getGroup() {
return this.group;
}
}

View File

@ -0,0 +1,47 @@
package com.plannaplan.entities;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class Commision {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
@JoinColumn(name = "owner_id")
private User commisionOwner;
private Timestamp commisionDate;
@OneToMany(mappedBy = "commision")
private List<Assignment> assignments;
public Commision(User user) {
this.commisionDate = new Timestamp(System.currentTimeMillis());
this.commisionOwner = user;
}
public Commision() {
}
public Long getId() {
return this.id;
}
public Timestamp getCommisionDate() {
return commisionDate;
}
public User getCommisionOwner() {
return commisionOwner;
}
}

View File

@ -77,4 +77,7 @@ public class User {
this.name = name; this.name = name;
} }
public Long getId() {
return this.id;
}
} }

View File

@ -4,22 +4,16 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
//dictionary is deprecated use hashmap instead
public class FileData { public class FileData {
private HashMap<String, Integer> keys; private HashMap<String, Integer> keys;
private Iterator<Row> rows; private Iterator<Row> rows;
//why variable is called keys2, could it be more specific???
//why use setters in constructor here instead of assignments???
// thought that setters are used to change values in runtime
public FileData(HashMap<String, Integer> keys, Iterator<Row> rows) { public FileData(HashMap<String, Integer> keys, Iterator<Row> rows) {
this.keys = keys; this.keys = keys;
this.rows = rows; this.rows = rows;
} }
public Iterator<Row> getRows() { public Iterator<Row> getRows() {
return rows; return rows;
} }

View File

@ -0,0 +1,18 @@
package com.plannaplan.repositories;
import java.util.List;
import com.plannaplan.entities.Assignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
@Query("FROM Assignment WHERE commision_id = ?1")
List<Assignment> getByCommision(@Param("commision_id") Long id);
}

View File

@ -0,0 +1,20 @@
package com.plannaplan.repositories;
import java.util.List;
import com.plannaplan.entities.Commision;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface CommisionRepository extends JpaRepository<Commision, Long> {
@Query("FROM Commision WHERE owner_id = ?1")
List<Commision> getUsers(@Param("owner_id") Long id);
@Query("FROM Commision WHERE owner_id = ?1 order by commisionDate desc")
List<Commision> getNewestCommision(@Param("owner_id") Long id);
}

View File

@ -0,0 +1,28 @@
package com.plannaplan.services;
import java.util.List;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.repositories.AssignmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AssignmentService {
@Autowired
private AssignmentRepository repo;
public AssignmentService() {
super();
}
public void save(Assignment assignment) {
this.repo.save(assignment);
}
public List<Assignment> getCommisionAssignments(Commision com) {
return this.repo.getByCommision(com.getId());
}
}

View File

@ -0,0 +1,35 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.repositories.CommisionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CommisionService {
@Autowired
private CommisionRepository repo;
public CommisionService() {
}
public Commision save(Commision commision) {
this.repo.save(commision);
return commision;
}
public List<Commision> getUsersCommisions(User user) {
Long id = user.getId();
return this.repo.getUsers(id);
}
public Optional<Commision> getNewestCommision(User user) {
return Optional.of(this.repo.getNewestCommision(user.getId()).get(0));
}
}

View File

@ -37,4 +37,8 @@ public class GroupService {
public int getGroupsAmmount() { public int getGroupsAmmount() {
return (int) this.repo.count(); return (int) this.repo.count();
} }
public Optional<Groups> getGroupById(Long id) {
return this.repo.findById(id);
}
} }

View File

@ -28,18 +28,33 @@ public class App {
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
public void importData() { public void importData() {
User testUser = new User();
testUser.setEmail("notexisting@mail.comain");
testUser.setName("Tom");
testUser.setSurname("Riddle");
testUser.setRole(UserRoles.TEST_USER);
this.userService.save(testUser);
User filip = new User(); User filip = new User();
filip.setEmail("filizy@st.amu.edu.pl"); filip.setEmail("filizy@st.amu.edu.pl");
filip.setName("Filip"); filip.setName("Filip");
filip.setSurname("Izydorczyk"); filip.setSurname("Izydorczyk");
filip.setRole(UserRoles.STUDENT); filip.setRole(UserRoles.STUDENT);
this.userService.save(filip); this.userService.save(filip);
User hub = new User();
hub.setEmail("hubwrz1@st.amu.edu.pl");
hub.setName("Hubert");
hub.setSurname("Wrzesiński");
hub.setRole(UserRoles.STUDENT);
this.userService.save(hub);
User mac = new User();
mac.setEmail("macglo2@st.amu.edu.pl");
mac.setName("Maciej");
mac.setSurname("Głowacki");
mac.setRole(UserRoles.STUDENT);
this.userService.save(mac);
User mar = new User();
mar.setEmail("marwoz16@st.amu.edu.pl");
mar.setName("Marcin");
mar.setSurname("Woźniak");
mar.setRole(UserRoles.STUDENT);
this.userService.save(mar);
} }
} }

View File

@ -0,0 +1,66 @@
package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Optional;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/assignments")
public class AssignmentsController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private AssignmentService assignmentService;
@GetMapping("/getCurrentAssignments")
public ResponseEntity<List<Object>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
Optional<Commision> com = this.commisionService.getNewestCommision(user);
if (com.isPresent()) {
List<Assignment> respone = this.assignmentService.getCommisionAssignments(com.get());
List<Object> finalResponse = new ArrayList<>();
for (Assignment a : respone) {
Dictionary<String, Object> elem = new Hashtable<>();
Dictionary<String, Object> groupInfo = new Hashtable<>();
elem.put("id", a.getId());
Groups g = a.getGroup();
groupInfo.put("id", g.getId());
groupInfo.put("day", g.getDay().label);
groupInfo.put("time", g.getTimeString());
groupInfo.put("lecturer", g.getLecturer().toString());
groupInfo.put("room", g.getRoom());
groupInfo.put("capacity", g.getCapacity());
groupInfo.put("type", g.getType());
elem.put("group", groupInfo);
finalResponse.add(elem);
}
return new ResponseEntity<>(finalResponse, HttpStatus.OK);
}
return new ResponseEntity<>(null, HttpStatus.OK);
}
}

View File

@ -0,0 +1,66 @@
package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.plannaplan.App;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
public class CommisionController extends TokenBasedController {
@Autowired
private CommisionService commisionService;
@Autowired
private GroupService groupServcicxe;
@Autowired
private AssignmentService assignmentService;
public CommisionController() {
}
@PostMapping("/add")
public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
Commision com = new Commision(user);
this.commisionService.save(com);
groups.stream().forEach((groupId) -> {
Groups group = this.groupServcicxe.getGroupById(groupId).orElseThrow(() -> new NullPointerException());
Assignment a = new Assignment(group, com);
this.assignmentService.save(a);
});
return new ResponseEntity<>("Succes", HttpStatus.OK);
}
@GetMapping("/getAllCommisions")
public ResponseEntity<List<Commision>> getAlCommisions() throws UserNotFoundException {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<Commision> result = this.commisionService.getUsersCommisions(user);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}

View File

@ -0,0 +1,30 @@
package com.plannaplan.controllers;
import java.util.Optional;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
public abstract class TokenBasedController {
@Autowired
protected UserService userService;
public TokenBasedController() {
}
protected Optional<User> getCurrentUser() throws UserNotFoundException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
} else {
return Optional.empty();
}
}
}

View File

@ -51,7 +51,7 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro
@Override @Override
public String getUsername() { public String getUsername() {
return user.getName() + " " + user.getSurname(); return user.getEmail();
} }
@Override @Override