Skip to content

Instantly share code, notes, and snippets.

@nicolopignatelli nicolopignatelli/Bnbs.java
Last active Feb 18, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.