I have a controller method as follow:
public class RoomsController {
@RequestMapping(method = RequestMethod.GET, path="/v1/rooms/{name}")
public ResponseEntity<?> getRoomInformation(@PathVariable String name, @RequestHeader("Auth-Token") String token){
try {
Room room = roomService.findByLogin(name);
User user = userService.findByBackendToken(token);
if(room == null || InstantHelper.biggerThanSixHours(room.getUpdatedAt()))
room = gitHubService.createOrUpdateRoom(name, user.getAccessToken());
String roomJson = new RoomFormatter(room).toJson();
joinRoom(room.getLogin(), token);
return new ResponseEntity<String>(roomJson, HttpStatus.OK);
} catch (ChathubBackendException e) {
log.error(e);
return new ResponseEntity<>("Error: "+e.getMessage(), HttpStatus.NOT_FOUND);
}
}
as seen, I have this line, for example: roomService.findByLogin(name);
but from what I understand, a Service does not return any value, it's just execute some action and raise an exception if something goes wrong. This is the first thing.
The second is related to the formatter/join part
String roomJson = new RoomFormatter(room).toJson();
joinRoom(room.getLogin(), token);
return new ResponseEntity<String>(roomJson, HttpStatus.OK);
I'm not really sure if the controller should handle this amount of responsibility like know if it's time for update the room information from API, explicit call the RoomFormatter
. I'm kinda lost here, because I don't really know where to put this. I think I should have a in-between layer that is not a service nor a repository, and this layer should know what to do with the room, format the JSON, and so on, but I don't know what it should be or if there is a pattern for this kind of things. Maybe it's ok to have this things in the controller...anyway. Any ideas are welcome!