Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

fixed database not found error #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"cli": {
"analytics": false
},
"version": 1,
"newProjectRoot": "projects",
"projects": {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@tailwindcss/forms": "^0.3.2",
"@tailwindcss/typography": "^0.4.0",
"angular-svg-icon": "^13.0.0",
"appwrite": "^8.0.0",
"appwrite": "^9.0.1",
"rxjs": "^7.4.0",
"tslib": "^2.0.0",
"zone.js": "~0.11.4"
Expand Down
9 changes: 5 additions & 4 deletions src/app/components/todo/todo.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms';
import { Select, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { Todo } from 'src/app/models/Todo';
Expand All @@ -11,7 +11,6 @@ import { Account, AccountState, Todos, TodoState } from 'src/app/store';
styleUrls: ['./todo.component.css'],
})
export class TodoComponent implements OnInit {

@Select(TodoState.getTodos) todos$: Observable<Todo[]>;

addTodoForm: FormGroup;
Expand All @@ -21,8 +20,8 @@ export class TodoComponent implements OnInit {
content: ['', [Validators.required]],
});
}
ngOnInit() {

ngOnInit() {
this.store.dispatch(new Todos.Fetch());
}

Expand All @@ -31,6 +30,8 @@ export class TodoComponent implements OnInit {
content: this.addTodoForm.value.content,
isComplete: false,
} as Todo;

this.addTodoForm.reset({ content: '' });
const userId = this.store.selectSnapshot(AccountState.userId);
const read = [`user:${userId}`];
const write = read;
Expand Down
24 changes: 20 additions & 4 deletions src/app/helpers/api.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { Appwrite } from 'appwrite';
import { Client, Databases, Account } from 'appwrite';
import { Server } from '../utils/config';

export class Api {
private static sdk: Appwrite | null;
private static sdk: Client | null;
private static db: Databases | null;
private static acc: Account | null;

static provider() {
private static client() {
if (this.sdk) return this.sdk;
let client = new Appwrite();
let client = new Client();

client
.setEndpoint(Server.endpoint)
.setProject(Server.project)
.setLocale('en-US');
this.sdk = client;

return this.sdk;
}

static database() {
if (this.db) return this.db;
this.db = new Databases(this.client(), Server.databaseID);
return this.db;
}

static account() {
if (this.acc) return this.acc;
this.acc = new Account(this.client());
return this.acc;
}
}
4 changes: 2 additions & 2 deletions src/app/helpers/auth-guard.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class AuthGuard implements CanActivate {
constructor(private router: Router) {}

canActivate(): Promise<boolean> {
return Api.provider()
.account.getSession('current')
return Api.account()
.getSession('current')
.then((isAuthenticated) => {
if (!isAuthenticated) {
this.router.navigate(['/login']);
Expand Down
14 changes: 7 additions & 7 deletions src/app/store/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class AccountState {
) {
let { email, password } = action.payload;
try {
await Api.provider().account.createSession(email, password);
let account = await Api.provider().account.get();
await Api.account().createEmailSession(email, password);
let account = await Api.account().get();
patchState({
account: account,
});
Expand All @@ -94,13 +94,13 @@ export class AccountState {
) {
let { email, password, name } = action.payload;
try {
let account = await Api.provider().account.create(
let account = await Api.account().create(
'unique()',
email,
password,
name
);
let session = await Api.provider().account.createSession(email, password);
let session = await Api.account().createEmailSession(email, password);
patchState({
account,
session,
Expand All @@ -124,7 +124,7 @@ export class AccountState {
action: Account.FetchAccount
) {
try {
let account = await Api.provider().account.get();
let account = await Api.account().get();
patchState({
account: account,
});
Expand All @@ -146,13 +146,13 @@ export class AccountState {
action: Account.Logout
) {
try {
await Api.provider().account.deleteSession('current');
await Api.account().deleteSession('current');
patchState({
account: null,
session: null,
});
dispatch(new Account.Redirect({ path: '' }));
} catch (e: any) {
} catch (e: any) {
console.log('Error Loggin Out');
dispatch(
new GlobalActions.setAlert({
Expand Down
17 changes: 5 additions & 12 deletions src/app/store/todos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ export class TodoState {
action: Todos.Fetch
) {
try {
let todos = await Api.provider().database.listDocuments(
Server.collectionID
);
let todos = await Api.database().listDocuments(Server.collectionID);
setState({
todos: todos.documents,
});
Expand All @@ -86,7 +84,7 @@ export class TodoState {
) {
try {
let { data, read, write } = action.payload;
let todo = await Api.provider().database.createDocument(
let todo = await Api.database().createDocument(
Server.collectionID,
'unique()',
data,
Expand Down Expand Up @@ -116,17 +114,15 @@ export class TodoState {
) {
let { documentId, data, read, write } = action.payload;
try {
let updatedTodo = await Api.provider().database.updateDocument(
let updatedTodo = await Api.database().updateDocument(
Server.collectionID,
documentId,
data,
read,
write
);
let todoList = [...getState().todos];
const index = todoList.findIndex(
(todo) => todo.$id === updatedTodo.$id
);
const index = todoList.findIndex((todo) => todo.$id === updatedTodo.$id);
if (index !== -1) {
todoList[index] = updatedTodo;
patchState({
Expand All @@ -152,10 +148,7 @@ export class TodoState {
) {
let { documentId } = action.payload;
try {
await Api.provider().database.deleteDocument(
Server.collectionID,
documentId
);
await Api.database().deleteDocument(Server.collectionID, documentId);
let todos = getState().todos;
todos = todos.filter((todo) => todo.$id !== documentId);
patchState({
Expand Down
9 changes: 5 additions & 4 deletions src/app/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { environment } from '../../environments/environment';

export const Server = {
endpoint : environment.APP_ENDPOINT,
project: environment.APP_PROJECT,
collectionID : environment.APP_COLLECTION_ID
}
endpoint: environment.APP_ENDPOINT,
project: environment.APP_PROJECT_ID,
databaseID: environment.APP_DATABASE_ID,
collectionID: environment.APP_COLLECTION_ID,
};
7 changes: 4 additions & 3 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

export const environment = {
production: false,
APP_ENDPOINT: "https://demo.appwrite.io/v1",
APP_PROJECT: "6062f9c2c09ce",
APP_COLLECTION_ID:"606621a04837c"
APP_ENDPOINT: '',
APP_PROJECT_ID: '',
APP_DATABASE_ID: '',
APP_COLLECTION_ID: '',
};

/*
Expand Down