Last active
February 18, 2018 18:25
-
-
Save nicolopignatelli/bab92cfe7dedd362da7bfc92767946e5 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 | |
interface Bnbs { | |
Optional<Bnb> load(BnbId); | |
void save(Bnb); | |
} |
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 RoomName { | |
private String value; | |
public RoomName(String value) { | |
if (value.isEmpty()) { | |
throw new RoomNameCannotBeEmpty(); | |
} | |
this.value = value; | |
} | |
} | |
final class RoomNameCannotBeEmpty extends Exception {} |
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; | |
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