Added commisions endponts

This commit is contained in:
Filip Izydorczyk 2020-09-30 17:46:04 +02:00
parent b4d1c87461
commit b503ebcbcc
6 changed files with 58 additions and 22 deletions

View File

@ -29,6 +29,9 @@ public class Commision {
this.commisionOwner = user; this.commisionOwner = user;
} }
public Commision() {
}
public Long getId() { public Long getId() {
return this.id; return this.id;
} }

View File

@ -68,4 +68,8 @@ public class User {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Long getId() {
return this.id;
}
} }

View File

@ -1,11 +1,17 @@
package com.plannaplan.repositories; package com.plannaplan.repositories;
import java.util.List;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import org.springframework.data.jpa.repository.JpaRepository; 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; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface CommisionRepository extends JpaRepository<Commision, Long> { public interface CommisionRepository extends JpaRepository<Commision, Long> {
@Query("FROM Commision WHERE owner_id = ?1")
List<Commision> getUsers(@Param("owner_id") Long id);
} }

View File

@ -1,6 +1,9 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.List;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.repositories.CommisionRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,4 +22,9 @@ public class CommisionService {
return commision; return commision;
} }
public List<Commision> getUsersCommisions(User user) {
Long id = user.getId();
return this.repo.getUsers(id);
}
} }

View File

@ -48,5 +48,13 @@ public class App {
mac.setSurname("Głowacki"); mac.setSurname("Głowacki");
mac.setRole(UserRoles.STUDENT); mac.setRole(UserRoles.STUDENT);
this.userService.save(mac); 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

@ -1,17 +1,17 @@
package com.plannaplan.controllers; package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.util.List;
import java.util.Optional;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.Controller;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.models.ConfigData;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService; import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService; import com.plannaplan.services.GroupService;
@ -22,14 +22,10 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@RestController @RestController
@CrossOrigin @CrossOrigin
@ -52,25 +48,36 @@ public class CommisionController {
} }
@PostMapping("/add") @PostMapping("/add")
public ResponseEntity<String> addCommision(@RequestParam("id") Long id) { public ResponseEntity<String> addCommision(@RequestBody List<Long> groups) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user;
if (!(authentication instanceof AnonymousAuthenticationToken)) { User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
user = this.userService.getUserByEmail(authentication.getName());
} else {
return new ResponseEntity<>("Succes", HttpStatus.UNAUTHORIZED);
}
Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException());
Commision com = new Commision(user); Commision com = new Commision(user);
Assignment a = new Assignment(group, com);
this.commisionService.save(com); 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); this.assignmentService.save(a);
});
return new ResponseEntity<>("Succes", HttpStatus.OK); return new ResponseEntity<>("Succes", HttpStatus.OK);
} }
@GetMapping("/getAllCommisions")
public ResponseEntity<List<Commision>> getAlCommisions() {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException());
List<Commision> result = this.commisionService.getUsersCommisions(user);
return new ResponseEntity<>(result, HttpStatus.OK);
}
private Optional<User> getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
return Optional.of(this.userService.getUserByEmail(authentication.getName()));
} else {
return Optional.empty();
}
}
} }