Last active
February 18, 2018 18:26
-
-
Save nicolopignatelli/bb905bf55c890a3c8748493798a4b1e5 to your computer and use it in GitHub Desktop.
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
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