Checkpoint: works first try with endpoint

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-06 14:50:34 +01:00
parent 3bcc35e75b
commit f24e62a335
Signed by: y0rune
GPG Key ID: F204C385F57EB348

View File

@ -5,10 +5,16 @@ import java.util.Optional;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.repositories.ExchangeRepository; import com.plannaplan.repositories.ExchangeRepository;
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; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
@ -26,17 +32,40 @@ import io.swagger.annotations.ApiOperation;
@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.")
public class ExchangeController { public class ExchangeController {
@Autowired
private UserService userService;
@Autowired
private GroupService groupService;
@Autowired
private AssignmentService assignmentService;
@Autowired
private ExchangeService exchangeService;
@PostMapping("/exchange") @PostMapping("/exchange")
@ApiOperation(value = "Return all courses") @ApiOperation(value = "Return all courses")
public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest){ public ResponseEntity<String> createExchange(@RequestBody Map<String, Long> exchangeRequest){
Long user = exchangeRequest.get("user");
Long assignment = exchangeRequest.get("assignment");
Long group = exchangeRequest.get("group");
if(user == null || assignment == null|| group == null ){ final Long userId = exchangeRequest.get("user");
return new ResponseEntity<>("Some of provided value missing", HttpStatus.BAD_REQUEST); 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);
} }
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()));
//System.out.println("KUPA"); //System.out.println("KUPA");
return null; return null;