Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
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;
}
}
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;
}
}
package com.mywonderfulbnb.messaging;
interface Publisher {
void publish(Message m);
}
package com.mywonderfulbnb.bnb.definition
interface ReadOnlyBnb {
BnbId getBnbId();
OwnerId getOwnerId();
Name getName();
Rooms getRooms();
}
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;
}
}
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
You can’t perform that action at this time.