Integrated MQTT clients
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package be.naaturel.boardmateapi.controllers;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.services.MqttService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController("/broker")
|
||||
public class BrokerController {
|
||||
|
||||
private final MqttService service;
|
||||
|
||||
@Autowired
|
||||
public BrokerController(MqttService service){
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostMapping("/publish/{topic}")
|
||||
public ResponseEntity<ResponseBody<?>> publish(@PathVariable String topic, @RequestBody String message){
|
||||
ResponseBody<?> body = ResponseBody.createEmpty();
|
||||
try {
|
||||
service.subscribe(topic);
|
||||
service.publish(topic, message);
|
||||
body.setSuccess(true);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(body);
|
||||
} catch (ServiceException se){
|
||||
body.setMessage(se.getMessage());
|
||||
body.setSuccess(false);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package be.naaturel.boardmateapi.controllers;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.models.Game;
|
||||
import be.naaturel.boardmateapi.controllers.dtos.GameDto;
|
||||
import be.naaturel.boardmateapi.controllers.mappings.GameMapper;
|
||||
@@ -33,53 +34,57 @@ public class GameController {
|
||||
|
||||
@GetMapping("/games/{id}")
|
||||
public ResponseEntity<ResponseBody<GameDto>> retrieveGames(@PathVariable String id){
|
||||
ResponseBody<GameDto> response = ResponseBody.createEmpty();
|
||||
ResponseBody<GameDto> result = ResponseBody.createEmpty();
|
||||
try{
|
||||
Game g = service.retrieveGame(id);
|
||||
GameDto dto = GameMapper.toDto(g);
|
||||
response.setData(dto);
|
||||
response.setSuccess(true);
|
||||
result.setData(dto);
|
||||
result.setSuccess(true);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.OK)
|
||||
.body(response);
|
||||
.body(result);
|
||||
} catch (Exception e){
|
||||
response.setMessage(e.getMessage());
|
||||
result.setMessage(e.getMessage());
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(response);
|
||||
.body(result);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public ResponseEntity<ResponseBody<String>> CreateParty(@RequestBody GameDto game){
|
||||
ResponseBody<String> response = ResponseBody.createEmpty();
|
||||
ResponseBody<String> result = ResponseBody.createEmpty();
|
||||
try{
|
||||
Game model = GameMapper.toModel(game);
|
||||
String result = service.create(model);
|
||||
response.setData(result);
|
||||
response.setSuccess(true);
|
||||
String id = service.create(model);
|
||||
result.setData(id);
|
||||
result.setSuccess(true);
|
||||
return ResponseEntity.
|
||||
status(HttpStatus.OK)
|
||||
.body(response);
|
||||
.body(result);
|
||||
} catch (Exception e){
|
||||
response.setMessage(e.getMessage());
|
||||
result.setMessage(e.getMessage());
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(response);
|
||||
.body(result);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/moves/add/{gameId}")
|
||||
public ResponseEntity<?> AddMove(@PathVariable String gameId, @RequestBody String move){
|
||||
public ResponseEntity<ResponseBody<String>> AddMove(@PathVariable String gameId, @RequestBody String move){
|
||||
ResponseBody<String> result = ResponseBody.createEmpty();
|
||||
try{
|
||||
service.addMove(gameId, move);
|
||||
String gamedId = service.addMove(gameId, move);
|
||||
result.setSuccess(true);
|
||||
result.setData(gamedId);
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.OK)
|
||||
.build();
|
||||
} catch (Exception e){
|
||||
.body(result);
|
||||
} catch (ServiceException e){
|
||||
result.setMessage(e.getMessage());
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.build();
|
||||
.body(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user