Skip to content

Instantly share code, notes, and snippets.

@nicolopignatelli
Last active February 18, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolopignatelli/bab92cfe7dedd362da7bfc92767946e5 to your computer and use it in GitHub Desktop.
Save nicolopignatelli/bab92cfe7dedd362da7bfc92767946e5 to your computer and use it in GitHub Desktop.
package com.mywonderfulbnb.bnb.definition
interface Bnbs {
Optional<Bnb> load(BnbId);
void save(Bnb);
}
package com.mywonderfulbnb.bnb.definition
final class RoomName {
private String value;
public RoomName(String value) {
if (value.isEmpty()) {
throw new RoomNameCannotBeEmpty();
}
this.value = value;
}
}
final class RoomNameCannotBeEmpty extends Exception {}
package com.mywonderfulbnb.bnb.definition
final class Service {
private Bnbs Bnbs;
private ActivatedOwners activatedOwners;
public Service(Bnbs bnbs, ActivatedOwners activatedOwners) {
this.bnbs = bnbs;
this.activatedOwners = activatedOwners;
}
public addBnb(UUID ownerUuid, UUID bnbUuid, String nameStr) {
ownerId = new OwnerId(ownerUuid);
bnbId = new BnbId(bnbUuid);
name = new Name(nameStr);
assertOwnerIsActivated(ownerId);
bnb = new Bnb(bnbUuid, ownerId, name);
bnbs.save(bnb);
}
public addRoom(UUID ownerUuid, UUID bnbUuid, String roomNameStr) {
ownerId = new OwnerId(ownerUuid);
bnbId = new BnbId(bnbUuid);
roomName = new RoomName(roomNameStr);
bnb = loadBnb(bnbId)
bnb.addRoomByName(roomName);
bnbs.save(bnb);
}
private assertOwnerIsActivated(OwnerId ownerId) {
itExists = activatedOwners.existsOneWithId(ownerId);
if (!itExists) {
throw new ActivatedOwnerNotFound(ownerId);
}
}
private Bnb loadBnb(BnbId bnbid) {
maybeBnb = bnbs.load(bnbId);
if (!maybeBnb.isPresent()) {
throw new BnbNotFound(bnbId);
}
return maybeBnb.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment