2021-01-06 13:40:04 +01:00
|
|
|
package com.plannaplan.controllers;
|
|
|
|
|
|
|
|
import java.util.Map;
|
2021-01-06 14:34:25 +01:00
|
|
|
import java.util.Optional;
|
2021-01-06 13:40:04 +01:00
|
|
|
|
|
|
|
import com.plannaplan.App;
|
2021-01-06 14:34:25 +01:00
|
|
|
import com.plannaplan.entities.Assignment;
|
2021-01-06 14:50:34 +01:00
|
|
|
import com.plannaplan.entities.Exchange;
|
2021-01-06 14:34:25 +01:00
|
|
|
import com.plannaplan.entities.Groups;
|
|
|
|
import com.plannaplan.entities.User;
|
|
|
|
import com.plannaplan.repositories.ExchangeRepository;
|
2021-01-06 14:50:34 +01:00
|
|
|
import com.plannaplan.services.AssignmentService;
|
|
|
|
import com.plannaplan.services.ExchangeService;
|
|
|
|
import com.plannaplan.services.GroupService;
|
|
|
|
import com.plannaplan.services.UserService;
|
2021-01-06 14:34:25 +01:00
|
|
|
|
2021-01-06 14:50:34 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2021-01-06 14:34:25 +01:00
|
|
|
import org.springframework.http.HttpStatus;
|
2021-01-06 13:40:04 +01:00
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
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.RestController;
|
|
|
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
@RequestMapping("/api/" + App.API_VERSION + "/exchanges")
|
|
|
|
@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.")
|
|
|
|
public class ExchangeController {
|
2021-01-06 14:50:34 +01:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GroupService groupService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private AssignmentService assignmentService;
|
2021-01-06 14:34:25 +01:00
|
|
|
|
2021-01-06 14:50:34 +01:00
|
|
|
@Autowired
|
|
|
|
private ExchangeService exchangeService;
|
|
|
|
|
2021-01-06 13:40:04 +01:00
|
|
|
@PostMapping("/exchange")
|
|
|
|
@ApiOperation(value = "Return all courses")
|
2021-01-06 14:34:25 +01:00
|
|
|
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest){
|
|
|
|
|
2021-01-06 14:50:34 +01:00
|
|
|
final Long userId = exchangeRequest.get("user");
|
|
|
|
final Long assignmentId = exchangeRequest.get("assignment");
|
|
|
|
final Long groupId = exchangeRequest.get("group");
|
|
|
|
|
|
|
|
if(userId == null || assignmentId == null|| groupId == null ){
|
|
|
|
return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST);
|
2021-01-06 14:34:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 14:50:34 +01:00
|
|
|
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()){
|
|
|
|
return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.exchangeService.save(new Exchange(user.get(), assignment.get(), group.get()));
|
|
|
|
|
2021-01-06 14:34:25 +01:00
|
|
|
//System.out.println("KUPA");
|
|
|
|
|
2021-01-06 13:40:04 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|