Last active
February 18, 2018 18:25
-
-
Save nicolopignatelli/aa4c9e60e54e235042cb753c5d51bc37 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 Bnb implements ReadOnlyBnb { | |
private BnbId bnbId; | |
private OwnerId ownerId; | |
private Name name; | |
private Rooms rooms; | |
public Bnb(BnbId bnbId, OwnerId ownerId, Name name, Rooms rooms) { | |
this.bnbId = bnbId; | |
this.ownerId = ownerId; | |
this.name = name; | |
this.rooms = rooms; | |
} | |
public void addRoomByName(RoomName roomName) { | |
// check if room with that name already exists, then... | |
room = new Room(roomName); | |
rooms = rooms.add(room); | |
} | |
public BnbId getBnbId() { | |
return bnbId; | |
} | |
public OwnerId getOwnerId() { | |
return ownerId; | |
} | |
public Name getName() { | |
return name; | |
} | |
public Rooms getRooms() { | |
return rooms; | |
} | |
} |
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 BnbWasAdded implements Message { | |
private OwnerId ownerId; | |
private ReadOnlyBnb bnb; | |
public BnbWasAdded(OwnerId ownerId, ReadOnlyBnb bnb) { | |
this.ownerId = ownerId; | |
this.bnb = bnb; | |
} | |
public OwnerId getOwnerId() { | |
return ownerId; | |
} | |
public ReadOnyBnb getBnb() { | |
return 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.messaging; | |
interface Publisher { | |
void publish(Message m); | |
} |
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 ReadOnlyBnb { | |
BnbId getBnbId(); | |
OwnerId getOwnerId(); | |
Name getName(); | |
Rooms getRooms(); | |
} |
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 RoomWasAdded implements Message { | |
private BnbId bnbId; | |
private Room room; | |
public RoomWasAdded(BnbId bnbId, Room room) { | |
this.bnbId = bnbId; | |
this.room = room; | |
} | |
public BnbId getBnbId() { | |
return bnbId; | |
} | |
public Room getRoom() { | |
return room; | |
} | |
} |
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, Publisher publisher) { | |
this.bnbs = bnbs; | |
this.activatedOwners = activatedOwners; | |
tihs.publisher = publisher; | |
} | |
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); | |
publisher.publish( | |
new BnbWasAdded(ownerId, (ReadOnlyBnb) bnb) | |
); | |
} | |
public addRoom(UUID bnbUuid, String roomNameStr) { | |
bnbId = new BnbId(bnbUuid); | |
roomName = new RoomName(roomNameStr); | |
bnb = loadBnb(bnbId) | |
room = bnb.addRoomByName(roomName); | |
bnbs.save(bnb); | |
publisher.publish( | |
new RoomWasAdded(bnbId, room) | |
); | |
} | |
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