Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.mywonderfulbnb.bnb.definition
final class Service {
private Bnbs Bnbs;
private ActivatedOwners activatedOwners;
private Publisher publisher;
private Logger logger;
public Service(Bnbs bnbs, ActivatedOwners activatedOwners, Publisher publisher, Logger logger) {
this.bnbs = bnbs;
this.activatedOwners = activatedOwners;
this.publisher = publisher;
this.logger = logger;
}
public Result<ReadOnlyBnb> addBnb(UUID ownerUuid, UUID bnbUuid, String nameStr) {
result = Result<ReadOnlyBnb>::get(() -> {
ownerId = new OwnerId(ownerUuid);
bnbId = new BnbId(bnbUuid);
name = new Name(nameStr);
assertOwnerIsActivated(ownerId);
bnb = new Bnb(bnbUuid, ownerId, name);
bnbs.save(bnb);
return bnb;
})
.❌(exStack -> {
logger.error("Couldn't add B&B");
});
result
.✅(bnb -> {
publisher.publish(
new BnbWasAdded(ownerId, bnb)
);
logger.info("B&B successfully added");
})
.❌(exStack -> {
logger.error("B&B added with errors");
});
return result;
}
public Result<Room> addRoom(UUID bnbUuid, String roomNameStr) {
result = Result<Room>::get(() -> {
bnbId = new BnbId(bnbUuid);
roomName = new RoomName(roomNameStr);
bnb = loadBnb(bnbId)
room = bnb.addRoomByName(roomName);
bnbs.save(bnb);
return room;
}
.❌(exStack -> {
logger.error("Couldn't add Room to B&B");
});
result
.✅(bnb -> {
publisher.publish(
new RoomWasAdded(bnbId, room)
);
logger.info("Room successfully added");
})
.❌(exStack -> {
logger.error("Room added with errors");
});
return result;
}
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.