Checkpoints: Part 1 of Added docs

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-06 16:23:18 +01:00
parent f24e62a335
commit df7701ebc8
Signed by: y0rune
GPG Key ID: F204C385F57EB348
4 changed files with 57 additions and 29 deletions

View File

@ -47,6 +47,14 @@ public class Assignment {
return this.group.getRegisteredStudents().contains(this.commision.getCommisionOwner());
}
/**
* Getter of commision
* @return Commision Commision of given assignments
*/
public Commision getCommision(){
return this.commision;
}
/**
* Assignment
*

View File

@ -7,6 +7,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
/**
* Entity that keeps user exchange offer.
*/
@Entity
public class Exchange {
@ -14,10 +17,6 @@ public class Exchange {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
@JoinColumn(name = "trader_id")
private User trader;
@OneToOne
@JoinColumn(name = "owned_id")
private Assignment ownedAssignment;
@ -30,38 +29,48 @@ public class Exchange {
}
public Exchange(User trader, Assignment ownedAssignment, Groups desiredAssignment){
this.trader = trader;
public Exchange(Assignment ownedAssignment, Groups desiredAssignment){
this.ownedAssignment = ownedAssignment;
this.desiredAssignment = desiredAssignment;
}
/**
* @return Long ID in database
*/
public Long getId(){
return this.id;
}
/**
* @return Groups Target group
*/
public Groups getDesiredAssignment() {
return desiredAssignment;
}
/**
* @param desiredAssignment Target group
*/
public void setDesiredAssignment(Groups desiredAssignment) {
this.desiredAssignment = desiredAssignment;
}
/**
* @return Assignment Owned assignment
*/
public Assignment getOwnedAssignment() {
return ownedAssignment;
}
/**
* @param ownedAssignment Owned assignment
*/
public void setOwnedAssignment(Assignment ownedAssignment) {
this.ownedAssignment = ownedAssignment;
}
public User getTrader() {
return trader;
}
public void setTrader(User trader) {
this.trader = trader;
}
}
}

View File

@ -12,6 +12,10 @@ public class ExchangeService {
@Autowired
private ExchangeRepository repo;
/**
* @param exchange Instance to save in database
* @return Exchange Instance contains database id
*/
public Exchange save(Exchange exchange){
return this.repo.save(exchange);
}

View File

@ -8,11 +8,10 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.repositories.ExchangeRepository;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.ExchangeService;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -30,10 +29,13 @@ import io.swagger.annotations.ApiOperation;
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/exchanges")
@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.")
public class ExchangeController {
public class ExchangeController extends TokenBasedController{
@Autowired
private UserService userService;
/*
* Endpoint do wylistowania exchangow użytkownika
* Endpoint do usuwania exchanga
*/
@Autowired
private GroupService groupService;
@ -46,28 +48,33 @@ public class ExchangeController {
@PostMapping("/exchange")
@ApiOperation(value = "Return all courses")
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest){
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest)
throws UserNotFoundException {
final Long userId = exchangeRequest.get("user");
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Long assignmentId = exchangeRequest.get("assignment");
final Long groupId = exchangeRequest.get("group");
if(userId == null || assignmentId == null|| groupId == null ){
if(assignmentId == null || groupId == null ){
return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST);
}
final Optional<User> user = this.userService.getById(userId);
final Optional<Assignment> assignment = this.assignmentService.getById(assignmentId);
final Optional<Groups> group = this.groupService.getGroupById(groupId);
if(user.isEmpty() || assignment.isEmpty() || group.isEmpty()){
if(assignment.isEmpty() || group.isEmpty()){
return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST);
}
this.exchangeService.save(new Exchange(user.get(), assignment.get(), group.get()));
final Assignment assignmentInstance = assignment.get();
//System.out.println("KUPA");
if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted())){
return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted", HttpStatus.BAD_REQUEST);
}
return null;
this.exchangeService.save(new Exchange(assignment.get(), group.get()));
return new ResponseEntity<>("Success", HttpStatus.OK);
}
}