Skip to content

Commit

Permalink
Working on local inventory creation via ES, #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Sep 3, 2019
1 parent 5780f2b commit 12ef0be
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 43 deletions.
17 changes: 6 additions & 11 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExpressServer } from "./server";
import { log } from "console";
import { log, error } from "console";

/**
* The main Express server
Expand All @@ -9,13 +9,8 @@ import { log } from "console";
const server: ExpressServer = new ExpressServer();

// Make the server listen based on config
server
.start()
.then(() => {
// Log server started
log("Server started.");
})
.catch(() => {
// Log error
log("Couldn't start server.");
});
server.start().catch(err => {
// Log error
log("Couldn't start server:");
error(err);
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export class AddInventoryComponent {
*/
async onSubmit(formData: Inventory) {
try {
const inventory: Inventory = await this.inv.newInventory(formData);
const inventoryUuid: string = await this.inv.createInventory(
formData.name
);
this.oof = false;
this.router.navigate(["/inventories/" + inventory.id + "/things"]);
this.router.navigate(["/inventories/" + inventoryUuid + "/things"]);
} catch (err) {
this.oof = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { environment } from "../../../environments/environment";
})
export class EventSourcingService {
constructor(private api: HttpClient) {
console.log("Hello there");
console.log(v4());
// this.fetchInventoryEvents()
// this.getAllLocalEventStreams(); // TODO implement
}

/**
Expand All @@ -35,6 +35,11 @@ export class EventSourcingService {
EventSourcingService.eventLogs[inventoryUuid] = res;
}

/**
* Appends an event to its corresponding event log
*
* @param event The event to be appended to its log
*/
public async appendEventToInventoryStream(event: Event): Promise<void> {
try {
const res: Event[] = await this.api
Expand Down Expand Up @@ -233,7 +238,7 @@ interface Event {
/**
* Used to describe on which type of item an operation was performed on
*/
enum itemType {
export enum itemType {
INVENTORY = "inventory",
CATEGORY = "category",
THING = "thing",
Expand All @@ -245,7 +250,7 @@ enum itemType {
*
* (read is excluded from this list since it doesn't affect the data)
*/
enum crudType {
export enum crudType {
CREATE = "create",
UPDATE = "update",
DELETE = "delete"
Expand Down
110 changes: 84 additions & 26 deletions frontend/src/app/services/inventory/inventory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ import { Inventory } from "../../models/inventory/inventory";
import { environment } from "../../../environments/environment";
import { User } from "../../models/user/user";
import { InventoryUserAccess } from "../../models/inventory-user-access.enum";
import {
EventSourcingService,
crudType,
itemType
} from "../EventSourcing/event-sourcing.service";
import { v4 } from "uuid";
import { AuthService } from "../auth/auth.service";

@Injectable({
providedIn: "root"
})
export class InventoryService {
private baseUrl: string = environment.baseUrl;

constructor(private http: HttpClient) {}
constructor(
private http: HttpClient,
private ess: EventSourcingService,
private as: AuthService
) {}

async getInventories(): Promise<Inventory[]> {
const response = await this.http
Expand All @@ -27,37 +38,84 @@ export class InventoryService {
return response;
}

async newInventory(inventory: Inventory): Promise<Inventory> {
/* const adminIds: number[] = [];
const writeableIds: number[] = [];
const readableIds: number[] = [];
/**
* Creates an inventory by generating and issuing an appropriate event
*
* @param name The name of the new inventory
* @returns The uuid of the new inventory
*/
async createInventory(name: string): Promise<string> {
/**
* The uuid for the new inventory
*/
const newUuid = v4();

// Copy ids
if (admins) {
for (const admin of admins) {
adminIds.push(admin.id);
}
}
/**
* The uuid of the user
*/
const myUuid = (await this.as.getUser()).user.uuid;

if (writeables) {
for (const writable of writeables) {
writeableIds.push(writable.id);
}
}
/**
* The current date (and time)
*/
const currentDate = new Date();

// Append to the event log
await this.ess.appendEventToInventoryStream({
inventoryUuid: newUuid,
date: currentDate,
data: {
crudType: crudType.CREATE,
itemType: itemType.INVENTORY,
uuid: newUuid,
userUuid: myUuid,

if (readables) {
for (const readable of readables) {
readableIds.push(readable.id);
// Core data
inventoryData: {
name,
createdOn: currentDate,
ownerUuid: myUuid,
adminsUuids: [],
writeablesUuids: [],
readablesUuids: []
}
}
} */
});

// Add the inventories createdOn date
inventory.createdOn = new Date();
// Update the inventories projection
this.updateInventoriesProjection();

// Request & Response
return await this.http
.post<Inventory>(this.baseUrl + "/inventories", inventory)
.toPromise();
return newUuid;

// /* const adminIds: number[] = [];
// const writeableIds: number[] = [];
// const readableIds: number[] = [];
// // Copy ids
// if (admins) {
// for (const admin of admins) {
// adminIds.push(admin.id);
// }
// }
// if (writeables) {
// for (const writable of writeables) {
// writeableIds.push(writable.id);
// }
// }
// if (readables) {
// for (const readable of readables) {
// readableIds.push(readable.id);
// }
// } */
// // Add the inventories createdOn date
// inventory.createdOn = new Date();
// // Request & Response
// return await this.http
// .post<Inventory>(this.baseUrl + "/inventories", inventory)
// .toPromise();
}

updateInventoriesProjection() {
// throw new Error("Method not implemented."); // TODO implement inventories projection
}

async getInventory(inventoryId: number): Promise<Inventory> {
Expand Down

0 comments on commit 12ef0be

Please sign in to comment.