From f24e62a3358b2f37898b79203e1dba6834fcdab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 14:50:34 +0100 Subject: [PATCH] Checkpoint: works first try with endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeController.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 5341dcb..5c775cc 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -5,10 +5,16 @@ import java.util.Optional; import com.plannaplan.App; 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.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.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; @@ -25,18 +31,41 @@ import io.swagger.annotations.ApiOperation; @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") public class ExchangeController { + + @Autowired + private UserService userService; + + @Autowired + private GroupService groupService; + + @Autowired + private AssignmentService assignmentService; + @Autowired + private ExchangeService exchangeService; + @PostMapping("/exchange") @ApiOperation(value = "Return all courses") public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ - Long user = exchangeRequest.get("user"); - Long assignment = exchangeRequest.get("assignment"); - Long group = exchangeRequest.get("group"); - if(user == null || assignment == null|| group == null ){ - return new ResponseEntity<>("Some of provided value missing", HttpStatus.BAD_REQUEST); + 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); } + final Optional user = this.userService.getById(userId); + final Optional assignment = this.assignmentService.getById(assignmentId); + final Optional 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"); return null;