Last active
February 9, 2016 06:17
-
-
Save jmsalcido/baa1dd2b90e59a7e094c to your computer and use it in GitHub Desktop.
Stupid Controllers 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeController { | |
private final SomeControllerHelper someControllerHelper; | |
// public constructor with the dependency | |
@Autowired | |
public SomeController(SomeControllerHelper someControllerHelper) { | |
this.someControllerHelper = someControllerHelper | |
} | |
@RequestMapping(value = "/some-request", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) | |
public ResponseEntity<Response<SomeRequest> > getSomeRequest(HttpSession session) { | |
// Clean Code ;) | |
SomeRequest someRequest = this.someControllerHelper.someRequest(session); | |
return data(someRequest); | |
} | |
} | |
public class SomeControllerHelper { | |
// pretend all dependencies are here. | |
@RequestMapping(value = "/some-request", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) | |
public ResponseEntity<Response<SomeRequest> > getSomeRequest(HttpSession session) { | |
SomeRequest someRequest = this.someBusinessService.createSomeRequest(session.getId()); | |
for (String x: someRequest.getSome()) { | |
if (x.equals("someStringNotAsAConstant")) { | |
process(x); | |
} | |
} | |
User user = getUser(); | |
if (this.configurationService.getConfigurations().getBoolean("is_a_config_turned_on")) { | |
try { | |
someRequest.setSomeName(user.getName()); | |
someRequest.setSomeAddress(new Address()); | |
for (String line : user.getAddress().getAddressLines()) { | |
someRequest.getSomeAddress().addAddressLine(line); | |
} | |
someRequest.setCreditCard(user.getCreditCard()); | |
this.userService.save(someRequest); | |
} catch (Exception e) { | |
return this.data(someRequest, HttpStatus.BAD_REQUEST); | |
} | |
} | |
DifferentPaymentMethod diff = new DifferentPaymentMethod() | |
diff.setDifferentPaymentMethodName("Other Name"); | |
diff.setActivated(true); | |
someRequest.getPaymentMethod().setOtherPaymentMethod(diff); | |
return data(someRequest); | |
} | |
User getUser() { | |
return this.userService.getUser(); | |
} | |
void process(String someX) { | |
// do something with someX | |
// we use methods instead of adding logic on the controller method right? | |
this.userService.processSomeEx(someX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment